QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#21674#2842. 好图计数houzhiyuanakioiAC ✓354ms19712kbC++3.1kb2022-03-07 22:19:512022-05-08 03:54:28

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-08 03:54:28]
  • 评测
  • 测评结果:AC
  • 用时:354ms
  • 内存:19712kb
  • [2022-03-07 22:19:51]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int p;
int qow(int a,int n){
	int ans=1;
	for(;n;n>>=1){
		if(n&1) ans=((long long)ans*a)%p;
		a=((long long)a*a)%p;
	}
	return ans;
}
inline int inverse(int a){
	return qow(a,p-2);
}
int f[110000];
int g[110000];
int h[210000];
const int C=(1<<15);
struct cp{
	double x,y;
	inline cp() {}
	inline cp(double xx,double yy) :x(xx),y(yy) {}
	inline cp operator +(const cp &b) const{return cp(x+b.x,y+b.y);}
	inline cp operator -(const cp &b) const{return cp(x-b.x,y-b.y);}
	inline cp operator *(const cp &b) const{return cp(x*b.x-y*b.y,x*b.y+y*b.x);}
	inline cp operator *(const double &b) const{return cp(x*b,y*b);}
	inline cp operator ~() const{return cp(x,-y);}
}x2[1<<20],y2[1<<20],z2[1<<20],w2[1<<20];
const double pi=acos(-1);
int rev[1<<20];
cp cc[1<<20][2];
int k,N;
double inv;
void fft(cp *a,int type){
	for(int i=0;i<N;i++){
		if(rev[i]>i) swap(a[i],a[rev[i]]);
	}
	for(int i=0;i<k;i++){
		for(int j=0;j<N;j+=(1<<(i+1))){
			for(int l=j;l<j+(1<<i);l++){
				cp x=a[l]+cc[(l-j)<<(k-1-i)][type]*a[l+(1<<i)],y=a[l]-cc[(l-j)<<(k-1-i)][type]*a[l+(1<<i)];
				a[l]=x;
				a[l+(1<<i)]=y;
			}
		}
	}
	if(type) {
		for(int i=0;i<N;i++) a[i]=a[i]*inv;
	}
} 
void multiply(int n,int m){
	for(k=1;;k++){
		if((1<<k)>n+m) break;
	}
	N=(1<<k);
	inv=1.0/N;
	for(int i=0;i<N;i++){
		rev[i]=0;
		for(int j=0;j<k;j++){
			if((i>>j)&1) rev[i]^=(1<<(k-1-j));
		}
	}
	for(int i=0;i<N;i++) cc[i][0]=cp(cos((2*pi*i)/N),sin((2*pi*i)/N));
	for(int i=0;i<N;i++) cc[i][1]=cp(cos((2*pi*i)/N),-sin((2*pi*i)/N));
	for(int i=0;i<=n;i++) x2[i]=cp(f[i]/C,f[i]%C);
	for(int i=n+1;i<N;i++) x2[i]=cp(0,0);
	fft(x2,0);
	for(int i=0;i<=m;i++) y2[i]=cp(g[i]/C,g[i]%C);
	for(int i=m+1;i<N;i++) y2[i]=cp(0,0);
	fft(y2,0);
	for(int i=0;i<N;i++) z2[i]=x2[i]*y2[i];
	fft(z2,1);
	w2[0]=(~x2[0])*y2[0];
	for(int i=1;i<N;i++) w2[i]=(~x2[N-i])*y2[i];
	fft(w2,1);
	for(int i=0;i<=n+m;i++){
		long long xxx,yyy,zzz;
		yyy=z2[i].y+0.4;
		yyy%=p;
		xxx=(z2[i].x+w2[i].x)/2+0.4;
		xxx%=p;
		zzz=(w2[i].x-z2[i].x)/2+0.4;
		zzz%=p;
		h[i]=(xxx*C*C+yyy*C+zzz)%p;
	}
}
int ff[30000]={};
int gg[30000]={}; 
void cdx(int l,int r){
	if(l==r){
		ff[l]=(ff[l]+1ll*(gg[l]+gg[l-1]))%p;
		ff[l]=((long long)inverse(l)*ff[l])%p;
		int u=23333/l;
		for(int i=1;i<=u;i++) gg[i*l]=(gg[i*l]+(long long)ff[l]*l)%p;
		return;
	}
	int mid=(l+r)/2;
	cdx(l,mid);
	if(l==2){
		for(int i=0;i<=mid;i++) f[i]=ff[i];
		for(int i=0;i<=mid;i++) g[i]=gg[i];
		multiply(mid,mid);
		for(int i=mid+1;i<=r;i++) ff[i]=(ff[i]+2ll*h[i])%p;
		cdx(mid+1,r);
		return;
	}
	for(int i=0;i<=r-l;i++) f[i]=gg[i];
	for(int i=0;i<=mid-l;i++) g[i]=ff[i+l];
	multiply(r-l,mid-l);
	for(int i=mid+1;i<=r;i++) ff[i]=(ff[i]+2ll*h[i-l])%p; 
	for(int i=0;i<=mid-l;i++) f[i]=gg[i+l];
	for(int i=0;i<=r-l;i++) g[i]=ff[i];
	multiply(mid-l,r-l);
	for(int i=mid+1;i<=r;i++) ff[i]=(ff[i]+2ll*h[i-l])%p;
	cdx(mid+1,r);
}
int main(){
	int _;
	scanf("%d%d",&_,&p);
	for(int i=1;i<=23333;i++) gg[i]=1; 
	cdx(2,23333);
	while(_--){
		int n;
		scanf("%d",&n);
		printf("%d\n",n>1 ? (2*ff[n])%p : 1);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 349ms
memory: 17652kb

input:

232 1050896131
15469
609
20
15343
18812
23
22505
19565
15762
9798
20897
20183
15
20680
3775
14113
1515
5
315
21711
7173
15222
11404
4
213
908
15283
3543
6196
369
18445
757
16449
478
1
40
42
21363
11
274
114
8679
8947
32
5465
26
894
5548
6026
22489
28
9
16568
7978
18
17
12354
25
27
12536
396
14838
88...

output:

904884594
194566965
513477502
435654809
13743016
841498901
116748751
50475529
201864344
802796940
873892703
555525537
1399068
511258868
31299504
828000970
683790199
24
677059510
76969605
404621991
338423543
437772976
10
625958623
921733698
226157111
1004031589
537747794
158830321
899619684
386333313...

result:

ok 232 lines

Test #2:

score: 0
Accepted
time: 344ms
memory: 17592kb

input:

233 1039495487
996
20153
1897
640
8030
135
27
5842
9169
16
20950
622
39
22314
45
296
334
18228
1
9741
22684
103
231
14970
4010
807
8433
3340
630
420
3762
189
8673
10782
2146
16421
402
46
864
4
619
21029
8122
10
36
24
21245
624
610
24
8519
22721
761
954
9934
12360
2364
20064
15568
721
5
19213
374
25
...

output:

413646369
77614340
556229064
454000244
956466355
460439371
1008436831
338754280
297159814
4507352
711008575
834547901
532381771
86932832
237326772
293901148
740650737
546036454
1
72976268
281367349
42787913
395808034
857353980
323037324
302296795
1011216075
135644599
146498118
406499606
658674439
49...

result:

ok 233 lines

Test #3:

score: 0
Accepted
time: 339ms
memory: 17476kb

input:

233 1047056147
19822
3347
14845
10430
8824
22472
7830
10332
28
14
43
4
893
10677
3663
9947
647
46
4556
411
8892
22189
388
33
423
15
49
9617
23
433
5015
50
766
269
3
15582
4679
14424
13755
35
882
10228
2469
11769
2695
34
890
14917
6560
610
6015
14991
7353
9
8584
29
31
1132
21583
12
20551
4329
21716
8...

output:

816447558
851254164
242778290
135635073
460403411
732213650
884156671
34157397
241915703
437502
663201127
10
931783124
891002319
383916977
266781753
848802426
571688641
543690388
1038485319
45622417
702209420
690724196
537243141
548672815
1399068
531822557
985027355
906778629
928246296
42904322
6457...

result:

ok 233 lines

Test #4:

score: 0
Accepted
time: 346ms
memory: 17668kb

input:

233 742281629
18651
495
23035
14518
15695
2917
20556
2682
22456
12270
11460
2577
12647
653
962
37
20070
20
5402
7885
4484
3687
12
4
1082
20
42
10035
12581
1331
7005
26
16201
146
24
507
992
10
15906
18046
34
17570
15622
774
407
43
6733
21878
18346
30
12087
2
13329
35
392
29
847
45
47
5032
313
297
198...

output:

727483615
217941062
599115095
88109933
419227715
233089898
683544764
31866962
555326371
371883549
731433758
361979816
116907831
189018524
29369012
41666597
589145089
513477502
279331170
26931550
275528195
697683535
43930
10
478026441
513477502
662017173
431159689
470721893
589765928
591847852
158137...

result:

ok 233 lines

Test #5:

score: 0
Accepted
time: 354ms
memory: 19712kb

input:

233 1048617289
13658
958
614
891
14136
18635
508
14
532
25
12698
222
14549
38
12810
20
21940
16980
22810
867
21983
240
6388
3248
179
16161
748
9289
16391
19
18
337
2
31
238
10560
149
9656
11327
2270
692
823
15669
848
640
365
43
6780
19428
8109
42
2060
28
48
47
13
22397
2760
1283
748
19436
20925
40
3...

output:

639094733
127608567
555698107
855855524
664463368
875999091
124344882
437502
868480210
94399629
97084282
258348214
224468024
32055110
66051021
513477502
880002302
884363398
421062345
797948097
1032233011
372319645
857611381
621772967
724980662
191796343
292994030
415700909
265771008
156047204
476334...

result:

ok 233 lines

Test #6:

score: 0
Accepted
time: 336ms
memory: 17592kb

input:

233 595370473
7
17481
759
23322
8182
8
752
9
6350
346
4004
10578
21
20276
25
1560
5
7906
6143
999
20
18434
6854
910
46
7919
15702
13201
14326
7579
4
510
16180
16
11789
4619
15427
2282
10952
3
22556
32
17879
20173
27
661
3986
150
856
564
23
824
13
35
19
49
6379
42
4277
39
10581
491
13416
6599
1335
11...

output:

180
374696635
526512525
336568702
76066309
522
466374748
1532
231996850
260745249
193612591
106228566
505564782
559467678
389574590
207615409
24
213671401
103463445
350706290
513477502
67423546
121533717
93084649
85567650
424514003
210496888
417881844
361645729
940723
10
285862697
12715288
4507352
4...

result:

ok 233 lines

Test #7:

score: 0
Accepted
time: 336ms
memory: 17544kb

input:

229 543131363
18466
11
8306
5463
390
2462
23239
616
41
924
246
45
8094
22126
6355
934
5809
37
22486
2712
16365
717
313
21949
9287
10394
9997
10110
18468
3367
7021
8719
9105
8
753
10152
3
16631
79
39
105
47
14037
18584
930
22757
3025
21559
21131
16382
12360
2
7300
9055
13011
23
49
21447
998
13993
33
...

output:

115524505
14136
72776043
41812739
218491411
9176000
289743420
431748772
42457976
538810574
357589534
44026986
526802376
467419023
312869448
517841532
333118527
260983441
14153458
439538518
291263928
80136981
435672803
251210718
210135553
301068246
34958700
140521940
298973098
341350135
231948309
473...

result:

ok 229 lines

Test #8:

score: 0
Accepted
time: 344ms
memory: 16560kb

input:

233 769707013
631
23298
34
11554
50
3
8685
2990
8387
632
103
45
9828
662
666
976
8237
15944
9
49
23
1473
875
42
8192
15965
25
2759
402
65
4984
402
4823
172
926
20640
10087
6610
558
12792
2
831
11
6682
17
3317
18734
410
2570
19
3945
41
975
37
21325
18819
3978
46
790
11302
597
12688
48
15630
907
10241...

output:

600506559
678047101
374257535
663925283
131906535
4
503787948
552763329
624199358
672550208
516698901
624830013
146691446
25230705
211826225
514422195
91189176
521021395
1532
268882628
233764816
430753786
291259500
210889898
499879088
182046154
178639617
468763394
78616678
72094266
265841154
7861667...

result:

ok 233 lines

Test #9:

score: 0
Accepted
time: 335ms
memory: 16528kb

input:

229 674266093
44
8932
494
972
11
17418
13586
17
10478
8857
15
14665
14425
769
17912
274
16059
7159
3800
7567
1924
191
22636
79
9901
21291
862
539
17554
20560
9311
23
18
593
476
16434
4079
13308
2701
6132
17283
8
14667
31
255
14823
21500
772
14126
34
18269
21408
352
50
10589
843
8664
7
3112
10130
776...

output:

328720159
529756233
546073938
513620563
14136
71337505
169430903
14611576
586324440
132636078
1399068
189492705
374377177
665226196
625053559
192455955
347175763
648736289
129056735
389678707
269692167
543828879
395585103
1232453
79116070
71791185
113862981
547580058
106211466
610140391
662824985
50...

result:

ok 229 lines

Test #10:

score: 0
Accepted
time: 352ms
memory: 17668kb

input:

233 580816793
33
34
610
7386
21093
8998
763
31
21428
19077
4641
19393
307
10452
4592
12320
3552
29
19428
374
4
139
4
2765
659
2485
22369
18247
12602
19724
9472
22153
14157
13336
168
1863
10
364
8078
7
37
4902
25
22227
13
693
14734
9785
505
5109
639
677
17422
847
882
22
39
860
21
11569
671
11325
42
4...

output:

31467305
340092602
136341883
251657712
75385613
501858565
133044156
297567987
44338888
225487367
236760992
79571543
229128523
375745650
161917329
284851359
206744112
179553436
171219412
540471851
10
270671742
10
377739318
214988101
211420210
12847405
107697279
500077762
561758592
229615440
458945001...

result:

ok 233 lines