QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#155662 | #6410. Classical DP Problem | Forever_Young# | WA | 18ms | 105408kb | C++14 | 1.7kb | 2023-09-01 23:51:24 | 2023-09-01 23:51:25 |
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);
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: 18ms
memory: 105192kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 8ms
memory: 103884kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: 0
Accepted
time: 18ms
memory: 104432kb
input:
2 1 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #4:
score: 0
Accepted
time: 7ms
memory: 104536kb
input:
2 2 2
output:
2 6
result:
ok 2 number(s): "2 6"
Test #5:
score: 0
Accepted
time: 7ms
memory: 104440kb
input:
3 1 1 1
output:
1 3
result:
ok 2 number(s): "1 3"
Test #6:
score: 0
Accepted
time: 12ms
memory: 105408kb
input:
3 2 2 2
output:
2 9
result:
ok 2 number(s): "2 9"
Test #7:
score: 0
Accepted
time: 8ms
memory: 104916kb
input:
3 3 3 3
output:
3 48
result:
ok 2 number(s): "3 48"
Test #8:
score: 0
Accepted
time: 12ms
memory: 103684kb
input:
5 1 1 3 3 4
output:
3 47
result:
ok 2 number(s): "3 47"
Test #9:
score: 0
Accepted
time: 12ms
memory: 103632kb
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: -100
Wrong Answer
time: 12ms
memory: 104708kb
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 974135375
result:
wrong answer 2nd numbers differ - expected: '986189864', found: '974135375'