QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#168006 | #6410. Classical DP Problem | xuzhihaodedie | WA | 2ms | 7680kb | C++20 | 1.5kb | 2023-09-07 19:45:17 | 2023-09-07 19:45:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define x first
//#define y second
#define PII pair<int,int>
const int N=1e5+10;
const int mod=998244353;
int a[N],b[N];
int dp[5005][5005];
int dp1[5005][5005];
int qpow(int a,int b) {
if(b==0) return 1;
if(b&1) return a*qpow(a,b-1)%mod;
else {
int mul=qpow(a,b/2);
return mul*mul%mod;
}
}
void add(int &x,int y) {
x+=y;
if(x>=mod) x-=mod;
}
void solve() {
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
reverse(a+1,a+n+1);
int k=0;
int res=1;
for(int i=1;i<=n;i++) {
if(a[i]>=i) {
k=i;
}
}
for(int i=1;i<=k;i++) res=res*i%mod;
//cout<<k<<" ";
int t=0;
if(k==n) t=a[k];
else t=a[k+1];
dp[0][0]=1;
for(int i=0;i<k;i++) {
for(int j=0;j<=t;j++) {
if(j+1<=t) add(dp[i+1][j+1],(t-j)*dp[i][j]%mod);
add(dp[i+1][j],(j+a[i+1]-t)*dp[i][j]%mod);
}
}
int ret=dp[k][t];
for(int i=1;i<=n;i++) {
for(int j=1;j<=a[i];j++) {
b[j]++;
}
}
for(int i=1;i<=n;i++) a[i]=b[i];
dp1[0][0]=1;
if(k==n) t=a[k];
else t=a[k+1];
for(int i=0;i<k;i++) {
for(int j=0;j<=t;j++) {
if(j+1<=t) add(dp1[i+1][j+1],(t-j)*dp1[i][j]%mod);
add(dp1[i+1][j],(j+a[i+1]-t)*dp1[i][j]%mod);
}
}
//cout<<ret<<" "<<dp1[k][t]<<endl;
int ans=(ret+dp1[k][t]-res+mod)%mod;
cout<<k<<" "<<ans<<endl;
}
signed main() {
int T=1;
//cin>>T;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(T--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7540kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 7540kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: 0
Accepted
time: 2ms
memory: 7504kb
input:
2 1 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 7680kb
input:
2 2 2
output:
2 2
result:
wrong answer 2nd numbers differ - expected: '6', found: '2'