QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#22573 | #2842. 好图计数 | blackswallow# | AC ✓ | 297ms | 15664kb | C++14 | 4.4kb | 2022-03-09 21:10:54 | 2022-04-30 01:22:04 |
Judging History
answer
#include<cstdio>
#include<cmath>
#include<numeric>
#include<cassert>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstdlib>
using namespace std;
int read();
typedef long long ll;
#define fr(i,l,r) for(int i=(l);i<=(r);++i)
#define rf(i,l,r) for(int i=(l);i>=(r);--i)
#define fo(i,l,r) for(int i=(l);i<(r);++i)
#define foredge(i,u,v) for(int i=fir[u],v;v=to[i],i;i=nxt[i])
#define filein(File) freopen(File".in","r",stdin)
#define fileout(File) freopen(File".out","w",stdout)
#define reset(f,n) memset(f,0,sizeof(ll)*(n))
#define copy(f,g,n) memcpy(f,g,sizeof(ll)*(n))
const int N = 1 << 16 | 5;
int MOD;
ll inv[N];
ll qpow(ll a,ll x) {
ll z=1;
for(;x;x>>=1,a=a*a%MOD)
if(x&1) z=z*a%MOD;
return z;
}
namespace Poly {
const double Pi = acos(-1);
struct cp {
double x,y;
inline cp(double x = 0,double y = 0) : x(x),y(y) {}
};
cp operator + (const cp &a,const cp &b) {
return cp(a.x + b.x,a.y + b.y);
}
cp operator - (const cp &a,const cp &b) {
return cp(a.x - b.x,a.y - b.y);
}
cp operator * (const cp &a,const cp &b) {
return cp(a.x*b.x - a.y*b.y,a.y*b.x + a.x*b.y);
}
cp conj(const cp &a) {
return cp(a.x,-a.y);
}
cp ome[N]; int rv[N];
int calc(int x) {int res = 1;while(res < x) res <<= 1;return res;}
void prepare(int lim) {
ome[0] = cp(1,0);
for(int i = 1;i < lim;++i) {
rv[i] = (rv[i >> 1] >> 1) | ((i&1) * (lim >> 1));
ome[i] = cp(cos(2*Pi*i/lim),sin(2*Pi*i/lim));
}
}
void FFT(cp *F,int &lim) {
for(int i = 1;i < lim;++i)
if(i < rv[i]) swap(F[i],F[rv[i]]);
for(int len = 1,tmp = lim >> 1;len < lim;len <<= 1,tmp >>= 1)
for(int st = 0;st < lim;st += len << 1) {
cp *l = F+st,*r = l+len,res,*o = ome;
for(int d = 0;d < len;++d) {
res = *r * *o;
*r = *l-res;*l = *l+res;
++l,++r,o += tmp;
}
}
}
void MUL(ll *x,ll *y,ll *z,int lim) {
static cp A[N],B[N],Da[N],Db[N],Dc[N],Dd[N];
prepare(lim);
for(int i = 0;i < lim;++i)
x[i] = (x[i] + MOD) % MOD,y[i] = (y[i] + MOD) % MOD;
for(int i = 0;i < lim;++i)
A[i] = cp(x[i] & 32767,x[i] >> 15),B[i] = cp(y[i] & 32767,y[i] >> 15);
FFT(A,lim),FFT(B,lim);
for(int i = 0;i < lim;++i) {
int j = lim-i&lim-1;
cp da = (A[i] + conj(A[j])) * cp(0.5,0);
cp db = (A[i] - conj(A[j])) * cp(0,-0.5);
cp dc = (B[i] + conj(B[j])) * cp(0.5,0);
cp dd = (B[i] - conj(B[j])) * cp(0,-0.5);
Da[j] = da * dc;
Db[j] = da * dd;
Dc[j] = db * dc;
Dd[j] = db * dd;
}
for(int i = 0;i < lim;++i)
A[i] = Da[i] + Db[i] * cp(0,1),B[i] = Dc[i] + Dd[i] * cp(0,1);
FFT(A,lim),FFT(B,lim);
for(int i = 0;i < lim;++i) {
ll da = (ll)(A[i].x / lim + 0.5) % MOD;
ll db = (ll)(A[i].y / lim + 0.5) % MOD;
ll dc = (ll)(B[i].x / lim + 0.5) % MOD;
ll dd = (ll)(B[i].y / lim + 0.5) % MOD;
z[i] = (da + (db + dc << 15) % MOD + (dd << 30) % MOD) % MOD;
}
}
void getinv(ll *f,ll *g,int n) {
static ll t[N];
reset(g,n); g[0]=qpow(f[0],MOD-2);
for(int lim=2;lim<=n;lim<<=1) {
MUL(f,g,t,lim); reset(t,lim/2); t[0]=1;
fo(i,lim/2,lim) t[i]=-t[i];
MUL(g,t,t,lim); copy(g+lim/2,t+lim/2,lim/2);
}
}
void getln(ll *f,ll *g,int n) {
static ll t[N];
getinv(f,g,n); reset(g+n,n);
fo(i,0,n) t[i]=f[i+1]*(i+1)%MOD;
reset(t+n,n);
MUL(t,g,g,n<<1); rf(i,n-1,1) g[i]=g[i-1]*inv[i]%MOD;
g[0]=0;
}
void getexp(ll *f,ll *g,int n) {
static ll t[N];
reset(g,n); g[0]=1;
for(int lim=2;lim<=n;lim<<=1) {
getln(g,t,lim);
fo(i,0,lim) t[i]=(f[i]-t[i])%MOD; ++t[0];
MUL(t,g,t,lim);
copy(g+lim/2,t+lim/2,lim/2);
}
}
void gnewton(ll *f,int n) {
static ll t[N],h[N],g[N];
f[1]=1;
for(int lim=4;lim<=n;lim<<=1) {
fo(i,0,lim) h[i]=0;
++f[1];
fo(i,1,lim/2) for(int j=1;i*j<lim;++j)
(h[i*j]+=f[i]*inv[j]%MOD*inv[2])%=MOD;
--f[1];
getexp(h,t,lim);
fo(i,0,lim) g[i]=2*f[i]%MOD;
g[0]+=2;
fo(i,0,lim) {
(g[i]-=2*t[i])%=MOD;
h[i]=-t[i];
}
h[0]+=2;
getinv(h,t,lim);
fo(i,lim,lim*2) g[i]=t[i]=0;
MUL(g,t,h,lim<<1);
fo(i,lim/2,lim) f[i]=-h[i];
}
}
}
int n;
ll f[N];
int main() {
int t=read(); MOD=read();
n=1<<15;
inv[1]=1; fr(i,2,n) inv[i]=inv[MOD%i]*(MOD-MOD/i)%MOD;
Poly::gnewton(f,n);
while(t--) printf("%lld\n",(f[read()]+MOD)%MOD);
return 0;
}
inline int read() {
static int x,c,f;x=0;f=1;
do c=getchar(),c=='-'&&(f=-1); while(!isdigit(c));
do x=x*10+(c&15),c=getchar(); while(isdigit(c));
return x*f;
}
详细
Test #1:
score: 100
Accepted
time: 288ms
memory: 15328kb
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: 297ms
memory: 15620kb
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: 283ms
memory: 15204kb
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: 285ms
memory: 15664kb
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: 275ms
memory: 15456kb
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: 279ms
memory: 15396kb
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: 284ms
memory: 15276kb
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: 284ms
memory: 15320kb
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: 295ms
memory: 15360kb
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: 284ms
memory: 15356kb
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