QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#114051 | #6410. Classical DP Problem | SoyTony | WA | 1ms | 3672kb | C++14 | 1.4kb | 2023-06-20 19:31:22 | 2023-06-20 19:31:25 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=5005;
const int mod=998244353;
inline int read(){
int x=0,w=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c<='9'&&c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
return x*w;
}
int n,k;
int a[maxn];
int b[maxn];
int f[maxn][maxn];
int ans;
int main(){
n=read();
for(int i=1;i<=n;++i){
a[n-i+1]=read();
++b[1];
if(a[n-i+1]<n) --b[a[n-i+1]+1];
}
for(int i=1;i<=n;++i) b[i]+=b[i-1];
for(int i=1;i<=n;++i){
if(i>a[i]){
k=i-1;
break;
}
}
f[0][0]=1;
for(int i=0;i<k;++i){
for(int j=0;j<=a[k+1];++j){
f[i+1][j+1]=(f[i+1][j+1]+1ll*(a[k+1]-j)*f[i][j]%mod)%mod;
f[i+1][j]=(f[i+1][j]+1ll*(a[i+1]-(a[k+1]-j))*f[i][j]%mod)%mod;
}
}
ans=(ans+f[k][a[k+1]])%mod;
for(int i=0;i<=n;++i){
for(int j=0;j<=n;++j){
f[i][j]=0;
}
}
f[0][0]=1;
for(int i=0;i<k;++i){
for(int j=0;j<=b[k+1];++j){
f[i+1][j+1]=(f[i+1][j+1]+1ll*(b[k+1]-j)*f[i][j]%mod)%mod;
f[i+1][j]=(f[i+1][j]+1ll*(b[i+1]-(b[k+1]-j))*f[i][j]%mod)%mod;
}
}
ans=(ans+f[k][b[k+1]])%mod;
int fact=1;
for(int i=1;i<=k;++i) fact=1ll*fact*i%mod;
ans=(ans-fact+mod)%mod;
printf("%d %d\n",k,ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3672kb
input:
1 1
output:
0 998244352
result:
wrong answer 1st numbers differ - expected: '1', found: '0'