QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#123153 | #6410. Classical DP Problem | chenxinyang2006 | WA | 25ms | 101348kb | C++14 | 1.7kb | 2023-07-11 19:54:22 | 2023-07-11 19:54:25 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i = (j);i <= (k);i++)
#define per(i,j,k) for(int i = (j);i >= (k);i--)
#define mod 998244353
using namespace std;
inline void add(int &x,int y){
x += y;
if(x >= mod) x -= mod;
}
inline void sub(int &x,int y){
x -= y;
if(x < 0) x += mod;
}
template <class P>
void chkmax(P &x,P y){
if(x < y) x = y;
}
int T,n;
int a[5005],b[5005];
int f[5005],g[5005],S[5005][5005];
int pos;
int spcalc(){
fill(b + 1,b + n + 1,0);
rep(i,1,n) b[a[i]]++;
per(i,n - 1,1) b[i] += b[i + 1];
fill(g,g + n + 1,0);
g[0] = 1;
int k = 0;
rep(i,1,n) if(a[i] > n - pos) k++;
rep(i,1,n - pos){
rep(j,0,i){
add(f[j + 1],g[j]);
add(f[j],1ll * g[j] * (b[i] - k) % mod);
}
rep(j,0,i){
g[j] = f[j];
f[j] = 0;
}
}
int ans = 0,tmp = 1;
rep(j,0,n) add(ans,1ll * S[j][k] * g[j] % mod);
rep(i,1,k) ans = ans * i % mod;
rep(i,1,n - pos) tmp = tmp * i % mod;
sub(ans,tmp);
return ans;
}
void solve(){
scanf("%d",&n);
rep(i,1,n) scanf("%d",&a[i]);
fill(g,g + n + 1,0);
g[0] = 1;
pos = 0;
rep(i,1,n) if(a[i] <= n - i) chkmax(pos,i);
printf("%d ",n - pos);
rep(i,pos + 1,n){
rep(j,0,i){
add(f[j + 1],g[j]);
add(f[j],1ll * g[j] * (a[i] - a[pos]) % mod);
}
rep(j,0,i){
g[j] = f[j];
f[j] = 0;
}
}
int ans = 0;
rep(j,0,n) add(ans,1ll * S[j][a[pos]] * g[j] % mod);
rep(j,1,a[pos]) ans = 1ll * ans * j % mod;
if(a[pos + 1] == n - pos) add(ans,spcalc());
printf("%d\n",ans);
}
int main(){
S[0][0] = 1;
rep(i,1,5000){
rep(j,1,i) S[i][j] = (S[i - 1][j - 1] + 1ll * j * S[i - 1][j]) % mod;
}
// scanf("%d",&T);
solve();
return 0;
}
//g++ D.cpp -o D -Wall -Wshadow -O2 -std=c++14 -Wno-unused-result
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 99936kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 11ms
memory: 101248kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: 0
Accepted
time: 25ms
memory: 101348kb
input:
2 1 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #4:
score: 0
Accepted
time: 7ms
memory: 100308kb
input:
2 2 2
output:
2 6
result:
ok 2 number(s): "2 6"
Test #5:
score: 0
Accepted
time: 10ms
memory: 100256kb
input:
3 1 1 1
output:
1 3
result:
ok 2 number(s): "1 3"
Test #6:
score: 0
Accepted
time: 15ms
memory: 100552kb
input:
3 2 2 2
output:
2 9
result:
ok 2 number(s): "2 9"
Test #7:
score: 0
Accepted
time: 20ms
memory: 100192kb
input:
3 3 3 3
output:
3 48
result:
ok 2 number(s): "3 48"
Test #8:
score: 0
Accepted
time: 4ms
memory: 100200kb
input:
5 1 1 3 3 4
output:
3 47
result:
ok 2 number(s): "3 47"
Test #9:
score: 0
Accepted
time: 19ms
memory: 100388kb
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: 13ms
memory: 100688kb
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: -100
Wrong Answer
time: 4ms
memory: 100596kb
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 216205656
result:
wrong answer 2nd numbers differ - expected: '287179924', found: '216205656'