QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#5911#1303. 斐波那契Qingyu✨100 ✓127ms14684kbC++172.8kb2021-02-06 18:36:562021-12-19 07:08:33

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2021-12-19 07:08:33]
  • Judged
  • Verdict: 100
  • Time: 127ms
  • Memory: 14684kb
  • [2021-02-06 18:36:56]
  • Submitted

answer

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll x,ll y)
{
	if(!y) return x;
	return gcd(y,x%y);
}
ll lcm(ll x,ll y)
{
	return x/gcd(x,y)*y;
}
void exgcd(ll a,ll b,ll &x,ll &y)
{
	if(!b)
	{
		x=1,y=0;
		return;
	}
	exgcd(b,a%b,y,x);
	y-=(a/b)*x;
}
ll getinv(ll x,ll mod)
{
	ll ans1,ans2;
	exgcd(x,mod,ans1,ans2);
	return (ans1%mod+mod)%mod;
}
struct pt
{
	ll x,l;
	pt(ll x=0,ll l=0):x(x),l(l){} 
};
pt operator*(pt a,pt b)
{
	if(a.x==-1||b.x==-1) return pt(-1,-1);
	ll ans1,ans2,g=gcd(a.l,b.l),mod;
	mod=a.l/g;
	if((a.x-b.x)%g!=0) return pt(-1,-1); 
	exgcd(a.l,b.l,ans2,ans1);
	ans1=ans1*((a.x-b.x)/g)%mod;
	ans1=(ans1%mod+mod)%mod;
	return pt(ans1*b.l+b.x,lcm(a.l,b.l));
}
int n,m,a[105][2],b[105],at,c[105],ct,rc[200005],f[105][100005],vis[200005];
void solve(int x)
{
	for(int i=2;i<=x;i++)
		if(x%i==0)
		{
			a[++at][0]=i;
			a[at][1]=1;
			b[at]=0;
			while(x%i==0)
			{
				b[at]++;
				a[at][1]*=i;
				c[++ct]=a[at][1];
				x/=i;
			}
		}
	if(x!=1)
	{
		a[++at][0]=x;
		a[at][1]=x;
		b[at]=1;
		c[++ct]=x;
	}
}
int dfs(int u,int x)
{
	if(f[x][u]!=-2) return f[x][u];
	if(vis[u]==1) return f[x][u]=-1;
	vis[u]=1;
	int g=gcd(u,c[x]);
	if(g!=1)
	{
		int v=(1+u)%c[x];
		int w=1ll*(u+v)*getinv(v,c[x])%c[x];
		int q=dfs(w,x);
		vis[u]=0;
		if(q==-1) return f[x][u]=-1;
		f[x][u]=q+2;
		return f[x][u];
	}
	int v=1ll*(1+u)*getinv(u,c[x])%c[x];
	int q=dfs(v,x);
	vis[u]=0;
	if(q==-1) return f[x][u]=-1;
	f[x][u]=q+1;
	return f[x][u];
}
void getans(int x)
{
	for(int i=0;i<c[x];i++)
		f[x][i]=-2,vis[i]=0;
	f[x][0]=1;
	for(int i=1;i<c[x];i++)
		dfs(i,x);
/*	printf("getans:x=%d,c=%d\n",x,c[x]);
	for(int i=0;i<c[x];i++)
		printf("%d ",f[x][i]);
	printf("\n");*/
}
pt solve3(int x,int y,int z)
{
	if(!x) return pt(0,f[z][1]+1);
	if(!y) return pt(1,f[z][1]+1);
	int g=gcd(x,c[z]),fl=0;
	if(g!=1)
	{
		int tmp=(x+y)%c[z];
		x=y;
		y=tmp;
		fl=1;
	}
	y=1ll*y*getinv(x,c[z])%c[z];
	if(f[z][y]==-1) return pt(-1,-1);
	return pt(f[z][y]+fl,f[z][1]+1);
}
pt solve2(int x,int y,int z)
{
	x%=a[z][1],y%=a[z][1];
//	printf("solve2:x=%d,y=%d,z=%d\n",x,y,z);
	if(x==0&&y==0) return pt(0,1);
	int g=gcd(gcd(x,y),a[z][1]);
	return solve3(x/g,y/g,rc[a[z][1]/g]);
}
int main()
{
	scanf("%d%d",&n,&m);
	solve(m);
	/*for(int i=1;i<=ct;i++)
		fprintf(stderr,"%d ",c[i]);
	fprintf(stderr,"\n");*/
	for(int i=1;i<=ct;i++)
		rc[c[i]]=i;
//	printf("---\n");
	for(int i=1;i<=ct;i++)
		getans(i);
	for(int i=1;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		pt ans;
		ans=pt(0,1);
		for(int j=1;j<=at;j++)
		{
			ans=ans*solve2(x,y,j);
	//		printf("x=%d,y=%d,j=%d,solve2=%lld,%lld\n",x,y,j,solve2(x,y,j).x,solve2(x,y,j).l);
		}
		printf("%lld\n",ans.x);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 2ms
memory: 1648kb

input:

969 953
283 22
14 309
296 278
686 299
600 76
808 942
914 842
832 412
172 905
81 603
115 900
709 870
203 110
413 682
476 109
59 773
932 661
635 191
692 557
246 787
619 265
708 127
477 644
870 893
392 301
942 908
60 23
0 388
520 642
869 239
269 125
60 570
491 650
337 303
391 918
377 916
174 129
778 490
272 619
448 825
511 370
152 331
169 621
405 114
321 850
897 864
729 610
937 452
764 599
877 428
747 896
377 935
311 545
136 271
160 155
692 157
252 320
901 879
472 269
289 584
650 528
44 63
239 402
...

output:

-1
8
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
0
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
17
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
47
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
22
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
29
-1
20
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
3
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 969 numbers

Test #2:

score: 10
Accepted
time: 2ms
memory: 3740kb

input:

910 731
464 171
586 246
443 392
510 566
506 676
701 6
8 314
607 582
152 726
421 38
220 283
457 318
640 39
476 558
135 645
322 617
232 646
175 304
305 358
719 605
500 583
603 426
615 694
620 103
535 440
0 369
200 150
379 357
285 619
684 381
288 627
519 190
624 48
666 292
4 633
572 321
582 504
707 186
53 641
680 125
557 725
326 583
621 238
576 661
258 545
387 146
405 532
635 687
216 192
21 282
279 384
243 578
430 650
333 270
419 507
554 277
62 622
673 36
536 463
680 40
426 534
59 577
259 342
162 5...

output:

-1
98
170
315
296
276
170
-1
166
371
-1
-1
124
288
89
-1
37
-1
74
205
282
-1
56
-1
-1
0
274
127
-1
-1
313
-1
-1
80
116
-1
-1
373
-1
198
285
167
181
359
-1
-1
-1
-1
326
-1
227
127
-1
213
-1
-1
-1
-1
3
81
-1
268
-1
286
366
-1
270
314
-1
-1
-1
-1
-1
381
-1
-1
361
144
22
16
192
134
-1
-1
35
-1
187
45
142
334
-1
-1
357
-1
-1
113
-1
-1
-1
298
243
109
-1
-1
-1
-1
31
-1
-1
-1
364
297
223
188
225
358
-1
-1
242
231
54
-1
285
-1
-1
174
-1
-1
-1
-1
189
296
-1
15
-1
-1
-1
104
191
379
-1
-1
94
302
-1
85
226
-...

result:

ok 910 numbers

Test #3:

score: 10
Accepted
time: 109ms
memory: 7396kb

input:

93871 87133
34463 6856
84498 33902
46483 36855
2548 34168
64537 50633
49043 26878
57344 429
84531 50101
71032 55600
38963 23766
59075 19835
12397 81624
41154 58415
39586 39836
44721 17024
1461 66202
60445 42070
11480 50358
44751 63581
52923 72576
47597 65067
45696 53137
47331 63043
39086 62783
85082 1084
43498 9616
68638 33854
59290 16841
33326 24120
77107 46283
18039 65378
52240 64902
60910 52130
28865 73335
8954 5810
59961 52278
29009 28121
33545 62026
29275 11365
81788 6334
48773 66071
64222 ...

output:

-1
-1
-1
-1
6882
798
20658
26433
-1
43006
-1
-1
-1
30697
22272
18657
-1
-1
25579
30312
42690
3673
26394
41199
-1
-1
32245
3207
-1
-1
-1
-1
16219
-1
26246
-1
22530
-1
25089
-1
42618
32610
35226
-1
39535
-1
22939
17782
7001
6015
-1
-1
-1
2548
-1
21358
34910
29089
-1
31040
6655
32572
33929
-1
1108
-1
273
-1
31168
19547
-1
-1
36381
-1
42506
-1
-1
14823
-1
-1
37115
-1
10436
27963
35063
6753
8018
-1
-1
-1
-1
-1
-1
-1
-1
37810
19283
-1
-1
-1
33452
-1
-1
41825
15308
-1
-1
-1
19078
-1
514
-1
-1
-1
6391
9...

result:

ok 93871 numbers

Test #4:

score: 10
Accepted
time: 97ms
memory: 7240kb

input:

94750 82837
75773 18293
55103 30435
28881 50086
1698 15822
36938 74666
60855 2162
18626 67201
53869 30085
21505 14938
48386 82762
7862 74898
12314 12084
25982 11146
45258 43835
61392 75785
70608 37335
37410 31830
25357 44338
58053 29057
14554 64141
30736 70571
56295 68896
74722 75617
56345 41813
55932 43431
55403 75131
47159 72981
24362 78632
30130 69347
52740 56900
2136 63865
11277 73514
61086 74080
14412 32902
1352 943
34858 65536
28778 48140
27832 57936
11239 21994
64818 65498
71164 54619
421...

output:

5660
21397
-1
36330
-1
14852
-1
-1
27008
8302
29877
-1
35479
-1
32156
-1
-1
-1
-1
-1
-1
-1
-1
19271
37422
-1
-1
-1
17902
-1
-1
-1
-1
12390
11869
38194
-1
-1
5770
-1
-1
12088
23415
-1
-1
-1
-1
26309
-1
-1
31805
-1
-1
-1
-1
-1
-1
33448
41013
-1
-1
-1
38861
-1
32172
-1
38344
4318
-1
10470
14645
28198
-1
-1
-1
-1
-1
-1
21150
-1
-1
23372
39644
-1
-1
-1
7503
25894
-1
-1
27270
18125
-1
10379
7807
25390
25414
6386
8835
39744
-1
-1
33056
-1
-1
-1
32370
-1
-1
-1
8017
6732
-1
30237
-1
-1
-1
-1
-1
7380
-1
-...

result:

ok 94750 numbers

Test #5:

score: 10
Accepted
time: 101ms
memory: 2036kb

input:

98826 63091
58627 56509
58453 33207
60944 49620
17900 26980
40343 47047
24828 28739
25234 17661
36154 20093
8248 24993
44591 47793
21092 62958
26204 23509
56499 35877
52841 54422
18061 29726
39410 30464
58656 30248
15501 22150
29724 36482
23531 13862
41941 31029
4244 11348
29708 20879
21829 31080
6870 48571
16135 54913
52559 57691
623 35802
6637 21593
22710 36620
33920 15118
19088 12603
54283 43714
18456 45310
44949 42049
42544 14281
27949 25680
33217 20972
26362 21889
775 57769
58364 25055
8033...

output:

-1
24862
-1
26638
-1
-1
13737
-1
-1
9284
-1
-1
9255
32541
-1
196
9861
-1
9298
-1
-1
16332
-1
15241
-1
3008
21354
-1
31805
-1
-1
-1
10964
7597
11073
18747
-1
7153
-1
29231
25930
18356
3332
15248
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
17625
-1
-1
5432
30521
31476
-1
34731
1414
-1
-1
32341
-1
33836
5128
5093
-1
27152
-1
11944
17684
-1
2193
21877
13923
-1
-1
-1
16145
20224
12485
-1
-1
-1
-1
34441
-1
2665
-1
26979
-1
-1
20203
-1
10016
-1
31734
7998
6775
-1
29397
-1
-1
-1
-1
7160
2938
-1
-1
-1
29179
-1
1...

result:

ok 98826 numbers

Test #6:

score: 10
Accepted
time: 103ms
memory: 6648kb

input:

94904 72317
42710 8125
62671 30616
34925 8668
66844 45649
18599 14193
4959 64618
4316 51257
53818 16448
58927 48953
5127 3894
17738 22827
49098 59014
18732 65585
35733 42854
2535 4065
36204 46153
69745 30298
60844 30597
46819 29398
57969 69152
2753 27603
33914 55280
34867 47757
37016 51169
32491 29999
40700 58652
23235 32079
16327 31699
26025 65695
23872 14297
49983 31595
29793 27252
53581 51393
51485 41600
2012 1401
70764 61752
50707 58199
61314 22103
30076 64148
53478 27094
46665 13450
47144 6...

output:

-1
28592
-1
22918
-1
23077
-1
34866
6902
34059
6239
8808
-1
-1
-1
19776
8972
6211
-1
-1
14343
-1
-1
22960
-1
22531
-1
18095
-1
-1
32338
38751
-1
-1
32341
-1
-1
-1
-1
37781
-1
37930
26904
-1
-1
39327
6892
-1
27328
-1
6411
5382
3956
-1
36408
-1
9837
-1
-1
-1
-1
13637
10686
24299
10449
-1
20412
-1
3248
-1
-1
-1
-1
18156
7589
-1
-1
-1
-1
-1
34349
12524
10361
26877
-1
-1
27913
27572
-1
34191
18188
18388
26562
-1
-1
-1
5396
-1
39741
5468
2694
-1
-1
4793
20234
-1
32456
-1
28524
-1
-1
16419
-1
-1
22526
...

result:

ok 94904 numbers

Test #7:

score: 10
Accepted
time: 118ms
memory: 6776kb

input:

90340 93750
25077 80075
20576 27740
86004 67166
69747 35762
2894 40438
54541 84405
7607 27619
6944 87099
37825 83901
53800 30877
10634 19942
36858 63817
68293 31604
7939 30706
2998 12892
81531 43155
8805 34720
90248 22218
23280 54826
68166 16723
462 61571
16283 10029
1144 7572
16657 52984
32835 37045
3366 80445
6694 88156
57035 70768
4455 6993
8355 40215
33962 88363
86344 46139
56793 53689
25754 93329
86115 61504
59966 54086
93278 10004
14501 30601
15097 76542
44926 53659
59469 49628
51041 83486...

output:

88076
31831
28192
43864
19798
61661
66518
109989
173525
140175
-1
123552
-1
21667
43667
37511
23092
28829
45360
-1
-1
-1
-1
139003
848
9366
4827
69490
2270
-1
173802
107034
-1
175599
138520
3139
-1
87074
110269
146607
64228
158539
172191
-1
-1
50216
63044
47581
173311
7051
13613
-1
94974
173495
62311
40417
38933
67309
150311
-1
22510
152509
127771
12275
54226
6590
53875
123970
11604
55049
32623
17228
2353
8283
155645
-1
136382
155387
15331
161662
56063
6353
127162
9902
96808
135130
-1
33911
-1
4...

result:

ok 90340 numbers

Test #8:

score: 10
Accepted
time: 127ms
memory: 14684kb

input:

96931 65536
55412 32374
47450 33421
6908 53354
29289 9129
26519 23435
57277 9022
1789 28510
7494 29883
35449 31837
32202 47301
7274 32886
33635 4532
8387 50060
10236 51220
9611 58356
62936 48933
9053 45817
61451 47452
3561 24921
51967 60640
13426 64454
29936 59405
29262 46950
28272 17908
44906 3667
62180 13017
45251 11291
28934 49331
47341 43585
54113 2992
63632 40123
6360 11951
52966 61042
48415 1988
55870 3525
28991 52407
55421 26886
42007 61317
55191 15099
23254 21214
13850 42846
55682 52902
...

output:

6927
-1
4113
2063
-1
-1
-1
-1
-1
-1
5510
-1
-1
-1
-1
5118
-1
-1
25163
3817
-1
26556
-1
-1
34503
-1
449
-1
-1
41149
30396
32922
-1
-1
22521
10289
-1
-1
-1
-1
-1
-1
8223
2063
9597
-1
-1
-1
20173
42315
34228
-1
-1
7476
32574
11576
41157
-1
-1
-1
-1
-1
-1
-1
45380
2836
-1
31905
47044
22040
-1
-1
19926
-1
22799
-1
-1
6857
7144
14393
4793
-1
28831
-1
-1
-1
-1
3304
21649
43022
-1
-1
-1
-1
44595
-1
26393
23823
-1
27875
-1
-1
-1
-1
11023
8430
-1
16502
-1
13058
-1
156
5362
-1
-1
-1
39435
17202
-1
23566
13...

result:

ok 96931 numbers

Test #9:

score: 10
Accepted
time: 102ms
memory: 5944kb

input:

94443 100000
82328 34403
89824 24111
30471 49907
21703 53495
38285 87602
50116 19021
17399 14054
86225 22003
56583 51529
77592 83931
17364 94726
12123 77005
31239 22400
10701 69218
73408 75851
52759 49483
91247 41361
85395 83467
35708 68879
43476 49593
64575 81685
27804 29599
90408 34889
3333 28189
81593 56164
24953 61511
76071 47956
51609 67938
9565 35192
63226 89260
18232 20648
18531 59633
21740 23210
15504 75638
22141 27774
94611 47442
1287 18547
92014 32368
49079 48303
50362 74799
40816 6276...

output:

10674
31872
-1
28931
4960
-1
7564
-1
-1
-1
2907
34196
1201
-1
74808
-1
-1
25145
-1
-1
-1
-1
-1
-1
-1
61178
-1
73138
10015
-1
16322
-1
1767
7158
-1
-1
-1
13003
28913
32673
-1
-1
-1
10294
-1
672
-1
1254
-1
71461
4873
32423
-1
9307
-1
30252
-1
-1
46483
-1
-1
-1
72724
-1
-1
2486
-1
-1
68449
-1
-1
30599
-1
-1
6479
-1
-1
-1
517
-1
-1
-1
31232
1202
33547
-1
12791
10788
44948
-1
-1
-1
8720
-1
37119
73597
-1
-1
28348
36769
-1
-1
-1
-1
11060
-1
29839
-1
-1
4809
-1
-1
46625
369
40042
154
-1
74312
-1
-1
671...

result:

ok 94443 numbers

Test #10:

score: 10
Accepted
time: 93ms
memory: 7820kb

input:

91887 84480
42722 69562
40284 76507
13244 47864
33818 54658
47229 21878
13646 3378
45069 47239
34371 54031
73400 82625
61669 38082
77996 45694
52807 248
15487 50270
48414 13793
44454 55748
40003 48562
78591 67924
58165 65827
64328 55163
45716 24049
4005 20199
30375 35678
65566 75531
75736 53932
59423 21148
6451 18341
50423 61974
49898 58646
34905 74296
46803 71504
62686 31364
50920 40102
60032 58817
12742 79857
11552 47224
84271 81679
40194 45434
22153 21855
59724 12376
54108 46151
39562 20458
6...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
587
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
771
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 91887 numbers