QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#155671 | #6410. Classical DP Problem | Forever_Young# | TL | 20ms | 105348kb | C++14 | 1.8kb | 2023-09-02 00:09:30 | 2023-09-02 00:09:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,a[5050],r,dp[5050][5050],res,fac[5050],ifac[5050],g[5050][5050];
const int mo=998244353;
int ksm(int x,int y){
int res=1;
while (y){
if (y&1) res=1ll*res*x%mo;
x=1ll*x*x%mo;
y/=2;
}
return res;
}
int c(int x,int y){//x>=y
if (x<y) return 0;
return 1ll*fac[x]*ifac[y]%mo*ifac[x-y]%mo;
}
int main(){
//freopen("D.in","r",stdin);
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
for (int i=n;i;i--){
if (r>=a[i]) break;
r++;
}
dp[n][a[n-r]]=1;
for (int i=n;i>n-r;i--)
for (int j=0;j<=a[n-r];j++){
//printf("%d %d %d\n",i,j,dp[0][i][j]);
dp[i-1][j]=(dp[i-1][j]+1ll*dp[i][j]*(a[i]-j))%mo;
if (j)
dp[i-1][j-1]=(dp[i-1][j-1]+1ll*dp[i][j]*j)%mo;
}
res=dp[n-r][0];
memset(dp,0,sizeof(dp));
//printf("%d\n",res);
dp[n][a[n-r+1]]=1;
for (int i=n;i>n-r;i--)
for (int j=0;j<=a[n-r+1];j++){
//printf("%d %d %d\n",i,j,dp[0][i][j]);
dp[i-1][j]=(dp[i-1][j]+1ll*dp[i][j]*(a[i]-j))%mo;
if (j)
dp[i-1][j-1]=(dp[i-1][j-1]+1ll*dp[i][j]*j)%mo;
}
res=(res-dp[n-r][0]+mo)%mo;
memset(dp,0,sizeof(dp));
//printf("%d\n",res);
if (r==a[n-r+1]){
fac[0]=1;
for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mo;
ifac[n]=ksm(fac[n],mo-2);
for (int i=n;i;i--) ifac[i-1]=1ll*ifac[i]*i%mo;
g[1][0]=1;
for (int i=1;i<=n;i++){
//0
//for (int j=0;j<=r;j++)
//printf("%d %d %d\n",i,j,g[i][j]);
if (a[i]<=r){
for (int j=0;j<=r;j++) g[i+1][j]=(g[i+1][j]+g[i][j])%mo;
}
//1
for (int j=0;j<r;j++){
if (g[i][j]==0) continue;
for (int k=j+1;k<=r&&k<=a[i];k++){
g[i+1][k]=(g[i+1][k]+1ll*g[i][j]*c(min(a[i],r)-j,k-j))%mo;
}
}
}
//printf("%d\n",g[n+1][r]);
res=(res+g[n+1][r])%mo;
}
printf("%d %d\n",r,res);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 12ms
memory: 103968kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 12ms
memory: 104924kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: 0
Accepted
time: 10ms
memory: 105024kb
input:
2 1 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #4:
score: 0
Accepted
time: 7ms
memory: 104432kb
input:
2 2 2
output:
2 6
result:
ok 2 number(s): "2 6"
Test #5:
score: 0
Accepted
time: 11ms
memory: 103576kb
input:
3 1 1 1
output:
1 3
result:
ok 2 number(s): "1 3"
Test #6:
score: 0
Accepted
time: 12ms
memory: 103428kb
input:
3 2 2 2
output:
2 9
result:
ok 2 number(s): "2 9"
Test #7:
score: 0
Accepted
time: 12ms
memory: 105160kb
input:
3 3 3 3
output:
3 48
result:
ok 2 number(s): "3 48"
Test #8:
score: 0
Accepted
time: 8ms
memory: 104780kb
input:
5 1 1 3 3 4
output:
3 47
result:
ok 2 number(s): "3 47"
Test #9:
score: 0
Accepted
time: 15ms
memory: 105348kb
input:
10 2 4 5 5 5 5 6 8 8 10
output:
5 864
result:
ok 2 number(s): "5 864"
Test #10:
score: 0
Accepted
time: 12ms
memory: 103928kb
input:
30 6 8 9 9 9 10 13 14 15 15 16 17 17 18 20 22 22 23 23 24 24 25 25 25 27 28 28 29 29 30
output:
17 986189864
result:
ok 2 number(s): "17 986189864"
Test #11:
score: 0
Accepted
time: 7ms
memory: 103920kb
input:
123 1 1 1 2 2 3 3 6 6 7 7 7 8 8 9 9 10 10 10 11 12 12 12 13 14 14 14 14 16 17 17 17 17 17 18 19 20 20 21 21 22 22 22 23 23 23 25 25 26 27 27 28 28 28 28 29 29 30 31 31 31 32 33 33 33 34 35 35 35 36 37 37 38 39 39 39 39 40 41 41 42 42 42 43 44 48 48 50 52 53 55 56 57 57 57 58 65 68 71 74 75 76 76 82 ...
output:
42 287179924
result:
ok 2 number(s): "42 287179924"
Test #12:
score: 0
Accepted
time: 8ms
memory: 104200kb
input:
1234 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 6 6 7 7 7 7 7 7 7 8 8 8 8 9 9 10 10 10 11 11 11 11 11 12 13 13 14 14 15 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 19 19 19 19 19 19 19 20 20 20 21 21 21 21 21 22 22 22 23 23 23 23 23 23 23 23 23 24 24 24 24 24 24 24 24 24 24 25 25 25 25 25 26 26 26 26 ...
output:
239 98119841
result:
ok 2 number(s): "239 98119841"
Test #13:
score: 0
Accepted
time: 20ms
memory: 103596kb
input:
2345 1 1 2 2 2 7 7 9 9 9 9 15 17 19 19 22 23 24 25 29 29 29 30 31 32 33 35 37 39 41 42 42 43 43 44 46 46 46 47 48 48 50 51 51 52 53 53 54 55 56 57 58 58 60 61 63 63 64 65 65 65 66 67 67 67 69 69 69 70 71 72 72 73 73 74 75 75 77 77 79 83 85 86 88 90 90 91 93 94 97 99 104 106 107 108 108 109 109 110 1...
output:
1239 588926916
result:
ok 2 number(s): "1239 588926916"
Test #14:
score: -100
Time Limit Exceeded
input:
3456 4 7 8 8 9 19 20 21 22 23 23 27 29 29 32 32 33 43 45 50 52 52 55 58 58 58 60 62 66 67 68 69 71 74 74 76 77 79 82 82 87 87 88 91 93 95 96 97 99 102 104 106 107 108 121 121 123 126 127 131 137 138 139 142 145 147 152 156 157 159 161 165 166 170 170 172 174 175 178 182 183 185 186 189 190 195 195 1...