QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#143039 | #6410. Classical DP Problem | training4usaco | WA | 12ms | 199464kb | C++17 | 1.5kb | 2023-08-20 13:35:36 | 2023-08-20 13:35:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 5e3 + 5;
const int MOD = 998244353;
int n, k;
int a[MAXN], b[MAXN];
int dp[MAXN][MAXN];
int fact[MAXN];
signed main() {
cin.tie(0)->sync_with_stdio(false);
fact[0] = 1;
for(int i = 1; i < MAXN; ++i) {
fact[i] = fact[i - 1] * i; fact[i] %= MOD;
}
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
reverse(a + 1, a + 1 + n);
int idx = 0, val = n;
for(int i = n; i >= 2; --i) {
if(a[i - 1] > a[i]) {
for(int j = idx + 1; j <= a[i]; ++j) b[j] = val;
idx = a[i]; val = i - 1;
}
}
for(int i = idx + 1; i <= a[1]; ++i) b[i] = val;
for(int i = 1; i <= n; ++i) {
if(i <= a[i]) k = i;
else break;
}
dp[0][0] = 1;
for(int i = 0; i < k; ++i) {
for(int j = 0; j <= a[k + 1]; ++j) {
dp[i + 1][j] += ((a[i + 1] - (a[k + 1] - j)) % MOD * dp[i][j]) % MOD; dp[i + 1][j] %= MOD;
dp[i + 1][j + 1] += ((a[k + 1] - j) * dp[i][j]) % MOD; dp[i + 1][j + 1] %= MOD;
}
}
int ans = dp[k][a[k + 1]] % MOD;
memset(dp, 0, sizeof(dp));
dp[0][0] = 1; swap(a, b);
for(int i = 0; i < k; ++i) {
for(int j = 0; j <= a[k + 1]; ++j) {
dp[i + 1][j] += ((a[i + 1] - (a[k + 1] - j)) % MOD * dp[i][j]) % MOD; dp[i + 1][j] %= MOD;
dp[i + 1][j + 1] += ((a[k + 1] - j) * dp[i][j]) % MOD; dp[i + 1][j + 1] %= MOD;
}
}
ans += dp[k][a[k + 1]] % MOD; ans %= MOD;
ans -= fact[k]; ans %= MOD;
cout << k << " " << ans << endl;
}
详细
Test #1:
score: 100
Accepted
time: 11ms
memory: 199200kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 199248kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: 0
Accepted
time: 4ms
memory: 199304kb
input:
2 1 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #4:
score: 0
Accepted
time: 8ms
memory: 199248kb
input:
2 2 2
output:
2 6
result:
ok 2 number(s): "2 6"
Test #5:
score: 0
Accepted
time: 3ms
memory: 199456kb
input:
3 1 1 1
output:
1 3
result:
ok 2 number(s): "1 3"
Test #6:
score: 0
Accepted
time: 0ms
memory: 199288kb
input:
3 2 2 2
output:
2 9
result:
ok 2 number(s): "2 9"
Test #7:
score: 0
Accepted
time: 1ms
memory: 199308kb
input:
3 3 3 3
output:
3 48
result:
ok 2 number(s): "3 48"
Test #8:
score: 0
Accepted
time: 1ms
memory: 199464kb
input:
5 1 1 3 3 4
output:
3 47
result:
ok 2 number(s): "3 47"
Test #9:
score: 0
Accepted
time: 8ms
memory: 199308kb
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: 199200kb
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 -12054489
result:
wrong answer 2nd numbers differ - expected: '986189864', found: '-12054489'