QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#99857 | #6333. Festivals in JOI Kingdom 2 | chenshi | 100 ✓ | 3127ms | 5044kb | C++ | 3.4kb | 2023-04-23 21:01:27 | 2023-04-23 21:01:30 |
Judging History
answer
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define poly vector<int>
const int o=40010;
int n,p,fac[o],inv[o],f[o][2],a[o][2],b[o][2],c[o][2],ans=1;
inline int fix(int x){return x+(x>>31&p);}
poly add(poly a,poly b){
if(b.size()>a.size()) a.resize(b.size());
for(int i=b.size();i--;) a[i]=fix(a[i]+b[i]-p);
return a;
}
poly sub(poly a,poly b){
if(b.size()>a.size()) a.resize(b.size());
for(int i=b.size();i--;) a[i]=fix(a[i]-b[i]);
return a;
}
poly Karatsuba(poly a,poly b){
if(a.empty()||b.empty()) return poly();
int sa=a.size(),sb=b.size();
if(min(sa,sb)<20){
poly res(sa+sb-1);
for(int i=0;i<sa;++i) for(int j=0;j<sb;++j) res[i+j]=(res[i+j]+a[i]*1ll*b[j])%p;
return res;
}
int md=min(max(sa,sb)/2,min(sa,sb));
poly al=poly(a.begin(),a.begin()+md),ar=poly(a.begin()+md,a.end());
poly bl=poly(b.begin(),b.begin()+md),br=poly(b.begin()+md,b.end());
poly t1=Karatsuba(al,bl),t2=Karatsuba(ar,br),t3=Karatsuba(add(al,ar),add(bl,br));
t3=sub(t3,add(t1,t2));
poly res(sa+sb-1);
for(int i=t1.size();i--;) res[i]=fix(res[i]+t1[i]-p);
for(int i=t2.size();i--;) res[i+md+md]=fix(res[i+md+md]+t2[i]-p);
for(int i=t3.size();i--;) res[i+md]=fix(res[i+md]+t3[i]-p);
return res;
}
inline void slv(int l,int r){
if(l==r){
for(int i=0;i<2;++i) f[l][i]=(f[l][i]+(a[l][i]*1ll*l+b[l][i])%p*l+c[l][i])%p;
return;
}
int md=l+r>>1;
slv(l,md);
poly A(md-l+1),B(r-l),C;
for(int i=l+md+1;i<=md+r;++i) B[i-(l+md+1)]=((i>=5)?fac[i-5]:0);
reverse(B.begin(),B.end());
for(int i=l;i<=md;++i) A[i-l]=f[i][0]*1ll*((i>1)?inv[i*2-3]:0)%p;
C=Karatsuba(A,B);
for(int i=md+1;i<=r;++i) a[i][0]=fix(a[i][0]+C[md+r-l-i]-p);
for(int i=l;i<=md;++i) A[i-l]=A[i-l]*(p-1ll-2*i)%p;
C=Karatsuba(A,B);
for(int i=md+1;i<=r;++i) b[i][0]=fix(b[i][0]+C[md+r-l-i]-p);
for(int i=l;i<=md;++i) A[i-l]=f[i][0]*1ll*((i>1)?inv[i*2-3]:0)%p*i%p*(i+1)%p;
C=Karatsuba(A,B);
for(int i=md+1;i<=r;++i) c[i][0]=fix(c[i][0]+C[md+r-l-i]-p);
for(int i=l+md+1;i<=md+r;++i) B[i-(l+md+1)]=((i>=5)?fac[i-4]:0);
reverse(B.begin(),B.end());
for(int i=l;i<=md;++i) A[i-l]=f[i][0]*1ll*((i>1)?inv[i*2-3]:0)%p;
C=Karatsuba(A,B);
for(int i=md+1;i<=r;++i) b[i][1]=fix(b[i][1]+C[md+r-l-i]-p);
for(int i=l;i<=md;++i) A[i-l]=A[i-l]*1ll*(p-i)%p;
C=Karatsuba(A,B);
for(int i=md+1;i<=r;++i) c[i][1]=fix(c[i][1]+C[md+r-l-i]-p);
for(int i=l+md+1;i<=md+r;++i) B[i-(l+md+1)]=((i>=4)?fac[i-4]:0);
reverse(B.begin(),B.end());
for(int i=l;i<=md;++i) A[i-l]=f[i][1]*1ll*inv[i*2-2]%p;
C=Karatsuba(A,B);
for(int i=md+1;i<=r;++i) b[i][0]=fix(b[i][0]+C[md+r-l-i]-p);
for(int i=l;i<=md;++i) A[i-l]=A[i-l]*1ll*(p-1ll-i)%p;
C=Karatsuba(A,B);
for(int i=md+1;i<=r;++i) c[i][0]=fix(c[i][0]+C[md+r-l-i]-p);
for(int i=l+md+1;i<=md+r;++i) B[i-(l+md+1)]=((i>=3)?fac[i-3]:0);
reverse(B.begin(),B.end());
for(int i=l;i<=md;++i) A[i-l]=f[i][1]*1ll*inv[i*2-2]%p;
C=Karatsuba(A,B);
for(int i=md+1;i<=r;++i) c[i][1]=fix(c[i][1]+C[md+r-l-i]-p);
slv(md+1,r);
}
int main(){
scanf("%d%d",&n,&p);inv[1]=1;
for(int i=2;i<=n*2;++i) inv[i]=p-p/i*1ll*inv[p%i]%p;
for(int i=fac[0]=inv[0]=1;i<=n*2;++i) fac[i]=fac[i-1]*1ll*i%p,inv[i]=inv[i-1]*1ll*inv[i]%p;
f[1][1]=f[2][0]=1;
slv(1,n);
for(int i=1;i<=n;++i) ans=ans*(2*i-1ll)%p;
for(int i=2;i<=n;++i) ans=(ans+(p-f[i][0])*(n-i+1ll)%p*fac[n+i-3]%p*inv[i*2-3])%p;
for(int i=1;i<=n;++i) ans=(ans+(p-f[i][1])*1ll*fac[n+i-2]%p*inv[i*2-2])%p;
printf("%d",ans);
return 0;
}
詳細信息
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 2ms
memory: 3092kb
input:
1 194903119
output:
0
result:
ok 1 number(s): "0"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3100kb
input:
2 933234047
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 2ms
memory: 3168kb
input:
3 277793111
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 2ms
memory: 3216kb
input:
4 355321177
output:
28
result:
ok 1 number(s): "28"
Test #5:
score: 0
Accepted
time: 2ms
memory: 3112kb
input:
5 306636893
output:
358
result:
ok 1 number(s): "358"
Subtask #2:
score: 5
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 5
Accepted
time: 2ms
memory: 3120kb
input:
8 361605653
output:
1236922
result:
ok 1 number(s): "1236922"
Test #7:
score: 0
Accepted
time: 2ms
memory: 3168kb
input:
8 995512643
output:
1236922
result:
ok 1 number(s): "1236922"
Test #8:
score: 0
Accepted
time: 2ms
memory: 3104kb
input:
8 101102801
output:
1236922
result:
ok 1 number(s): "1236922"
Test #9:
score: 0
Accepted
time: 2ms
memory: 3012kb
input:
6 458322727
output:
4894
result:
ok 1 number(s): "4894"
Test #10:
score: 0
Accepted
time: 2ms
memory: 3040kb
input:
7 721691819
output:
73884
result:
ok 1 number(s): "73884"
Test #11:
score: 0
Accepted
time: 2ms
memory: 3120kb
input:
7 370629137
output:
73884
result:
ok 1 number(s): "73884"
Subtask #3:
score: 27
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #12:
score: 27
Accepted
time: 0ms
memory: 3036kb
input:
30 779092367
output:
686412377
result:
ok 1 number(s): "686412377"
Test #13:
score: 0
Accepted
time: 2ms
memory: 3080kb
input:
29 963995171
output:
128570082
result:
ok 1 number(s): "128570082"
Test #14:
score: 0
Accepted
time: 1ms
memory: 3212kb
input:
18 666092701
output:
185922458
result:
ok 1 number(s): "185922458"
Test #15:
score: 0
Accepted
time: 2ms
memory: 3140kb
input:
14 671243719
output:
623913899
result:
ok 1 number(s): "623913899"
Subtask #4:
score: 14
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #16:
score: 14
Accepted
time: 6ms
memory: 3184kb
input:
300 463478027
output:
89265245
result:
ok 1 number(s): "89265245"
Test #17:
score: 0
Accepted
time: 9ms
memory: 3148kb
input:
296 567610679
output:
406342763
result:
ok 1 number(s): "406342763"
Test #18:
score: 0
Accepted
time: 3ms
memory: 3244kb
input:
297 609090359
output:
128986577
result:
ok 1 number(s): "128986577"
Test #19:
score: 0
Accepted
time: 3ms
memory: 3108kb
input:
48 759569383
output:
635573072
result:
ok 1 number(s): "635573072"
Test #20:
score: 0
Accepted
time: 3ms
memory: 3172kb
input:
99 298873033
output:
285340078
result:
ok 1 number(s): "285340078"
Subtask #5:
score: 36
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #21:
score: 36
Accepted
time: 149ms
memory: 3468kb
input:
3000 752129633
output:
33058561
result:
ok 1 number(s): "33058561"
Test #22:
score: 0
Accepted
time: 152ms
memory: 3444kb
input:
2993 491173567
output:
343277625
result:
ok 1 number(s): "343277625"
Test #23:
score: 0
Accepted
time: 151ms
memory: 3468kb
input:
2993 783095563
output:
776085006
result:
ok 1 number(s): "776085006"
Test #24:
score: 0
Accepted
time: 3ms
memory: 3080kb
input:
327 399783431
output:
163535283
result:
ok 1 number(s): "163535283"
Test #25:
score: 0
Accepted
time: 40ms
memory: 3328kb
input:
1233 857060207
output:
422139845
result:
ok 1 number(s): "422139845"
Test #26:
score: 0
Accepted
time: 31ms
memory: 3248kb
input:
1114 600227447
output:
598509129
result:
ok 1 number(s): "598509129"
Subtask #6:
score: 13
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #27:
score: 13
Accepted
time: 3127ms
memory: 4928kb
input:
20000 221054167
output:
39809956
result:
ok 1 number(s): "39809956"
Test #28:
score: 0
Accepted
time: 3098ms
memory: 5044kb
input:
19997 823974001
output:
267537750
result:
ok 1 number(s): "267537750"
Test #29:
score: 0
Accepted
time: 3107ms
memory: 4964kb
input:
19991 501689843
output:
16527909
result:
ok 1 number(s): "16527909"
Test #30:
score: 0
Accepted
time: 1783ms
memory: 4284kb
input:
14344 925452091
output:
212324779
result:
ok 1 number(s): "212324779"
Test #31:
score: 0
Accepted
time: 451ms
memory: 3672kb
input:
6098 507350869
output:
310480789
result:
ok 1 number(s): "310480789"
Test #32:
score: 0
Accepted
time: 397ms
memory: 3488kb
input:
5564 406069759
output:
105694337
result:
ok 1 number(s): "105694337"
Extra Test:
score: 0
Extra Test Passed