QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#838675 | #9561. 树数叔术 | Crying | 100 ✓ | 50ms | 4016kb | C++14 | 1.5kb | 2024-12-31 17:54:22 | 2024-12-31 17:54:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 160; int mod;
int addv(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
int subv(int x,int y){return (x>=y)?(x-y):(x-y+mod);}
void add(int& x,int y){x = addv(x,y);}
void sub(int& x,int y){x = subv(x,y);}
//
int n,m,C[N][N];
int f[2][N][N],g[N][N];
int main(){
cin>>n>>m>>mod;
m++; if(m > n){cout<<"0\n"; return 0;}
for(int i=0;i<=n;i++){
C[i][0] = 1;
for(int j=1;j<=i;j++)C[i][j] = addv(C[i-1][j-1],C[i-1][j]);
}
//
f[1][1][0] = 1;
for(int i=2;i<=m;i++){
int q = i&1,p = q^1;
memset(f[q],0,sizeof f[q]);
memset(g,0,sizeof g);
for(int x=1;x<=n;x++)for(int y=0;y<=x;y++){
int w = f[p][x][y]; if(!w)continue;
sub(f[q][x][y],w);
//case1. 拓展一个新点
add(f[q][x+1][y],1ll*w*x%mod);
add(f[q][x+2][y+1],1ll*w*(x-1)%mod);
//case2. 在当前的树上修改
for(int c=0;c<=y;c++){
int nw = 1ll*w*C[y][c]%mod;
add(f[q][x][y-c],nw);
add(g[x][y-c],nw);
if(x==1)continue;
}
}
for(int x=1;x<=n;x++)for(int y=0;y<=x;y++){
int w = g[x][y]; if(!w)continue;
for(int z=1;x+z<=n;z++){
add(f[q][x+z][y],1ll*w*C[x+z-2][z]%mod);
}
}
}
int ans = f[m&1][n][0];
for(int i=2;i<=n;i++)ans = 1ll*ans*i%mod;
cout<<ans<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 3472kb
input:
1 1 624295285
output:
0
result:
ok single line: '0'
Test #2:
score: 5
Accepted
time: 0ms
memory: 3484kb
input:
4 684813415 564954712
output:
0
result:
ok single line: '0'
Test #3:
score: 5
Accepted
time: 1ms
memory: 3844kb
input:
4 2 844826878
output:
24
result:
ok single line: '24'
Test #4:
score: 5
Accepted
time: 0ms
memory: 3548kb
input:
4 17724073 252218682
output:
0
result:
ok single line: '0'
Test #5:
score: 5
Accepted
time: 1ms
memory: 3928kb
input:
4 3 697681963
output:
384
result:
ok single line: '384'
Subtask #2:
score: 15
Accepted
Test #6:
score: 15
Accepted
time: 1ms
memory: 3840kb
input:
6 4 956647977
output:
238320
result:
ok single line: '238320'
Test #7:
score: 15
Accepted
time: 0ms
memory: 3644kb
input:
6 450615260 491361886
output:
0
result:
ok single line: '0'
Test #8:
score: 15
Accepted
time: 1ms
memory: 3928kb
input:
6 5 339344353
output:
933120
result:
ok single line: '933120'
Test #9:
score: 15
Accepted
time: 1ms
memory: 3852kb
input:
6 3 842228619
output:
23760
result:
ok single line: '23760'
Test #10:
score: 15
Accepted
time: 1ms
memory: 3844kb
input:
6 5 331699652
output:
933120
result:
ok single line: '933120'
Subtask #3:
score: 30
Accepted
Test #11:
score: 30
Accepted
time: 1ms
memory: 3940kb
input:
48 26 424594716
output:
362283012
result:
ok single line: '362283012'
Test #12:
score: 30
Accepted
time: 0ms
memory: 3544kb
input:
49 1000000000 738885247
output:
0
result:
ok single line: '0'
Test #13:
score: 30
Accepted
time: 1ms
memory: 3808kb
input:
48 39 688951620
output:
598399200
result:
ok single line: '598399200'
Test #14:
score: 30
Accepted
time: 0ms
memory: 3608kb
input:
50 476039414 292870080
output:
0
result:
ok single line: '0'
Test #15:
score: 30
Accepted
time: 1ms
memory: 4016kb
input:
50 48 245196368
output:
123576912
result:
ok single line: '123576912'
Subtask #4:
score: 50
Accepted
Test #16:
score: 50
Accepted
time: 0ms
memory: 3628kb
input:
150 526250070 197316869
output:
0
result:
ok single line: '0'
Test #17:
score: 50
Accepted
time: 48ms
memory: 3936kb
input:
149 116 671784452
output:
18945228
result:
ok single line: '18945228'
Test #18:
score: 50
Accepted
time: 45ms
memory: 3928kb
input:
146 144 906402626
output:
438777234
result:
ok single line: '438777234'
Test #19:
score: 50
Accepted
time: 46ms
memory: 4016kb
input:
147 143 705666477
output:
70408701
result:
ok single line: '70408701'
Test #20:
score: 50
Accepted
time: 50ms
memory: 3928kb
input:
150 147 453481175
output:
336290325
result:
ok single line: '336290325'