QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#774081 | #9782. NonZero PrefSuf Sums | ucup-team3555# | AC ✓ | 196ms | 31580kb | C++20 | 3.4kb | 2024-11-23 11:38:52 | 2024-11-23 11:38:52 |
Judging History
answer
/*
I know this sky loves you
いずれ全て
変わってしまったって
空は青いだろうよ
*/
# include <bits/stdc++.h>
# define int long long
const int N=105,INF=0x3f3f3f3f,V=100;
int mod;
inline int read(void){
int res,f=1;
char c;
while((c=getchar())<'0'||c>'9')
if(c=='-') f=-1;
res=c-48;
while((c=getchar())>='0'&&c<='9')
res=res*10+c-48;
return res*f;
}
inline int adc(int a,int b){return (a+b>=mod)?(a+b-mod):(a+b);}
inline int dec(int a,int b){return (a<b)?(a-b+mod):(a-b);}
inline int mul(int a,int b){return 1ll*a*b%mod;}
inline void add(int &a,int b){a=adc(a,b);}
inline void del(int &a,int b){a=dec(a,b);}
inline int qpow(int d,int p){
// printf("d = %d p = %d\n",d,p);
int ans=1;
while(p){
if(p&1) ans=1ll*ans*d%mod;
d=1ll*d*d%mod,p>>=1;
}
return ans;
}
int dp[N][N][N];
int n,m;
int fac[N],finv[N];
int g[N][N];
inline void init(void){
fac[0]=finv[0]=1;
for(int i=1;i<=V;++i) fac[i]=mul(i,fac[i-1]),finv[i]=qpow(fac[i],mod-2);
return;
}
inline int bi(int n,int m){
if(n<m||n<0||m<0) return 0;
return mul(mul(fac[n],finv[m]),finv[n-m]);
}
int pw_m[N];
// int arr[N][N][N]; // choosing i
int suming[N][N*N*2];
const int Z=10005;
int h[N][N][N];
signed main(void){
// freopen("in.txt","r",stdin);
n=read(),m=read(),mod=read();
pw_m[0]=1;
for(int i=1;i<=n;++i) pw_m[i]=mul(pw_m[i-1],m);
suming[0][Z]=1;
for(int i=1;i<=n;++i){
for(int s=-(i-1)*m;s<=(i-1)*m;++s){
for(int d=-m;d<=m;++d) add(suming[i][s+d+Z],suming[i-1][s+Z]);
}
}
// arr[0][0][0]=1;
init();
dp[0][0][0]=1;
// std::cout<<finv[1]<<"A"<<std::endl;
for(int v=1;v<=m;++v){
memcpy(dp[v],dp[v-1],sizeof(dp[v]));
for(int l=0;l<=n;++l){
for(int sum=0;sum<=n;++sum){
if(!dp[v-1][l][sum]) continue;
for(int k=1;l+k<=n&&sum+k*v<=n;++k){
add(dp[v][l+k][sum+k*v],mul(dp[v-1][l][sum],finv[k]));
add(h[v][l+k][sum+k*v],mul(dp[v-1][l][sum],finv[k]));
}
}
}
}
// printf("DEBUG = %d\n",dp[1][1][1]);
for(int v=0;v<=m;++v){
for(int l=0;l<=n;++l){
for(int sum=1;sum<=n;++sum) add(h[v][l][sum],h[v][l][sum-1]);
}
}
// printf("DEBUG = %d\n",dp[1][1][1]);
int ans=qpow(2*m+1,n);
// printf("ans = %d\n",ans);
del(ans,mul(n,2*m)); // from 1 positive (negative)
// printf("ans = %d\n",ans);
del(ans,suming[n][Z]); // from sum = 0
// printf("A = %d B = %d\n",mul(n,2*m),suming[n][Z]);
// printf("ans = %d\n",ans);
for(int zc=0;zc<=n;++zc){ // zero_counting
for(int v=1;v<=m;++v){
for(int po=1;po+zc<=n;++po){ // positive vs
for(int ne=n-po-zc;ne==n-po-zc;++ne){
for(int mx_ne=0;mx_ne<=m/v;++mx_ne){
if(ne==0) continue;
// IT IS VALID IFF: po >= mx_ne + sum + 2
// sum <= po - mx_ne - 2
// INVALID IFF: sum > po - mx_ne - 2
// printf("v = %d zc = %d po = %d ne = %d\n",v,zc,po,ne);
int ways=h[mx_ne][ne][po-1];
if(po-mx_ne-2>=0) del(ways,h[mx_ne][ne][po-mx_ne-2]);
// printf("lim = %d [%d,%d]\n",lim,slim+1,po-1);
ways=mul(ways,fac[ne]);
// ways=dec(pw_m[ne],ways);
// printf("ways = %d\n",ways);
int op=mul(mul(fac[n],finv[zc]),mul(finv[po],finv[ne]));
del(ans,mul(mul(2,op),ways)); // positive & negative
// calculating len = ne
}
}
}
}
}
printf("%lld",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7904kb
input:
2 1 998244353
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 21ms
memory: 16412kb
input:
69 42 696969697
output:
378553557
result:
ok single line: '378553557'
Test #3:
score: 0
Accepted
time: 1ms
memory: 7848kb
input:
2 1 998244353
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 25ms
memory: 16456kb
input:
69 42 696969697
output:
378553557
result:
ok single line: '378553557'
Test #5:
score: 0
Accepted
time: 46ms
memory: 22576kb
input:
61 75 677323601
output:
34613998
result:
ok single line: '34613998'
Test #6:
score: 0
Accepted
time: 2ms
memory: 8144kb
input:
13 14 670577333
output:
41465431
result:
ok single line: '41465431'
Test #7:
score: 0
Accepted
time: 0ms
memory: 8060kb
input:
14 6 987686347
output:
37536510
result:
ok single line: '37536510'
Test #8:
score: 0
Accepted
time: 0ms
memory: 10180kb
input:
15 12 196428923
output:
29322522
result:
ok single line: '29322522'
Test #9:
score: 0
Accepted
time: 0ms
memory: 8800kb
input:
68 7 786815587
output:
149281835
result:
ok single line: '149281835'
Test #10:
score: 0
Accepted
time: 2ms
memory: 9912kb
input:
3 2 503002109
output:
82
result:
ok single line: '82'
Test #11:
score: 0
Accepted
time: 2ms
memory: 9980kb
input:
13 5 756093197
output:
415698676
result:
ok single line: '415698676'
Test #12:
score: 0
Accepted
time: 1ms
memory: 8052kb
input:
2 3 646574611
output:
30
result:
ok single line: '30'
Test #13:
score: 0
Accepted
time: 18ms
memory: 17492kb
input:
39 68 120037189
output:
43217507
result:
ok single line: '43217507'
Test #14:
score: 0
Accepted
time: 0ms
memory: 10048kb
input:
13 4 423132517
output:
360231790
result:
ok single line: '360231790'
Test #15:
score: 0
Accepted
time: 2ms
memory: 10052kb
input:
14 3 309713387
output:
94215386
result:
ok single line: '94215386'
Test #16:
score: 0
Accepted
time: 3ms
memory: 18416kb
input:
24 77 886983941
output:
211636479
result:
ok single line: '211636479'
Test #17:
score: 0
Accepted
time: 1ms
memory: 7872kb
input:
3 3 504388063
output:
270
result:
ok single line: '270'
Test #18:
score: 0
Accepted
time: 1ms
memory: 7940kb
input:
3 1 936205423
output:
8
result:
ok single line: '8'
Test #19:
score: 0
Accepted
time: 1ms
memory: 7864kb
input:
3 3 295983139
output:
270
result:
ok single line: '270'
Test #20:
score: 0
Accepted
time: 0ms
memory: 10200kb
input:
10 15 446169107
output:
149884328
result:
ok single line: '149884328'
Test #21:
score: 0
Accepted
time: 4ms
memory: 10884kb
input:
37 18 833753929
output:
592917251
result:
ok single line: '592917251'
Test #22:
score: 0
Accepted
time: 0ms
memory: 7968kb
input:
11 3 998773403
output:
860630017
result:
ok single line: '860630017'
Test #23:
score: 0
Accepted
time: 6ms
memory: 17532kb
input:
14 85 688036639
output:
347188409
result:
ok single line: '347188409'
Test #24:
score: 0
Accepted
time: 0ms
memory: 7900kb
input:
3 3 844621907
output:
270
result:
ok single line: '270'
Test #25:
score: 0
Accepted
time: 1ms
memory: 7960kb
input:
3 4 204335891
output:
620
result:
ok single line: '620'
Test #26:
score: 0
Accepted
time: 1ms
memory: 7964kb
input:
7 8 113007667
output:
58946097
result:
ok single line: '58946097'
Test #27:
score: 0
Accepted
time: 1ms
memory: 7860kb
input:
4 1 637268377
output:
22
result:
ok single line: '22'
Test #28:
score: 0
Accepted
time: 0ms
memory: 10172kb
input:
11 14 391637237
output:
303270280
result:
ok single line: '303270280'
Test #29:
score: 0
Accepted
time: 2ms
memory: 9964kb
input:
3 2 208286231
output:
82
result:
ok single line: '82'
Test #30:
score: 0
Accepted
time: 2ms
memory: 9996kb
input:
2 11 662696483
output:
462
result:
ok single line: '462'
Test #31:
score: 0
Accepted
time: 0ms
memory: 15408kb
input:
19 55 974135299
output:
887460557
result:
ok single line: '887460557'
Test #32:
score: 0
Accepted
time: 0ms
memory: 8012kb
input:
6 8 417027509
output:
23351024
result:
ok single line: '23351024'
Test #33:
score: 0
Accepted
time: 2ms
memory: 10260kb
input:
8 13 624006587
output:
353008442
result:
ok single line: '353008442'
Test #34:
score: 0
Accepted
time: 0ms
memory: 8172kb
input:
10 10 740294671
output:
79436611
result:
ok single line: '79436611'
Test #35:
score: 0
Accepted
time: 1ms
memory: 8128kb
input:
11 10 394088657
output:
161476458
result:
ok single line: '161476458'
Test #36:
score: 0
Accepted
time: 2ms
memory: 10344kb
input:
9 27 562853573
output:
135252259
result:
ok single line: '135252259'
Test #37:
score: 0
Accepted
time: 1ms
memory: 7908kb
input:
8 3 829129009
output:
5349034
result:
ok single line: '5349034'
Test #38:
score: 0
Accepted
time: 14ms
memory: 15516kb
input:
51 49 924010279
output:
815049368
result:
ok single line: '815049368'
Test #39:
score: 0
Accepted
time: 0ms
memory: 10012kb
input:
12 2 308466749
output:
223013998
result:
ok single line: '223013998'
Test #40:
score: 0
Accepted
time: 1ms
memory: 10020kb
input:
7 4 567557693
output:
4502296
result:
ok single line: '4502296'
Test #41:
score: 0
Accepted
time: 19ms
memory: 19952kb
input:
36 93 943780729
output:
13599465
result:
ok single line: '13599465'
Test #42:
score: 0
Accepted
time: 0ms
memory: 7892kb
input:
2 1 828681127
output:
2
result:
ok single line: '2'
Test #43:
score: 0
Accepted
time: 1ms
memory: 7856kb
input:
3 3 534160729
output:
270
result:
ok single line: '270'
Test #44:
score: 0
Accepted
time: 2ms
memory: 10068kb
input:
7 12 920925433
output:
453086694
result:
ok single line: '453086694'
Test #45:
score: 0
Accepted
time: 1ms
memory: 7856kb
input:
3 2 440546987
output:
82
result:
ok single line: '82'
Test #46:
score: 0
Accepted
time: 6ms
memory: 9624kb
input:
90 9 291269963
output:
72560304
result:
ok single line: '72560304'
Test #47:
score: 0
Accepted
time: 3ms
memory: 10536kb
input:
38 10 867575113
output:
165530481
result:
ok single line: '165530481'
Test #48:
score: 0
Accepted
time: 11ms
memory: 12424kb
input:
48 37 152663531
output:
135425620
result:
ok single line: '135425620'
Test #49:
score: 0
Accepted
time: 1ms
memory: 10236kb
input:
15 15 991731803
output:
102703562
result:
ok single line: '102703562'
Test #50:
score: 0
Accepted
time: 196ms
memory: 31580kb
input:
100 100 696969697
output:
313377809
result:
ok single line: '313377809'
Extra Test:
score: 0
Extra Test Passed