QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#232879 | #6410. Classical DP Problem | ana_valeria | WA | 0ms | 3720kb | C++14 | 1.9kb | 2023-10-31 00:09:07 | 2023-10-31 00:09:08 |
Judging History
answer
#include <iostream>
#include <algorithm>
#define MAX 5000
#define MOD 998244353
using namespace std;
///ifstream cin ("c.in");
///ofstream cout ("c.out");
int a[MAX + 10], aux[MAX + 10], dp[MAX + 10][MAX + 10], n;
int findK()
{
int k = 0;
for (int i = 1; i <= n + 1 && k == 0; i++)
if (a[i] < i)
k = i - 1;
return k;
}
int findX(int k)
{
return a[k + 1];
}
int kFact(int k)
{
int p = 1;
for (int i = 1; i <= k; i++)
p = 1LL * p * i % MOD;
return p;
}
int DP(int k)
{
int x = findX(k);
for (int i = 0; i <= k; i++)
for (int j = 0; j <= x; j++)
dp[i][j] = 0;
dp[0][x] = 1;
for (int i = 0; i < k; i++)
for (int j = x; j >= 0; j--)
{
dp[i + 1][j] = (dp[i + 1][j] + 1LL * (a[i + 1] - j) * dp[i][j] % MOD) % MOD;
if (j > 0)
dp[i + 1][j - 1] = (dp[i + 1][j - 1] + 1LL * j * dp[i][j] % MOD) % MOD;
}
return dp[k][0];
}
void flip()
{
int length = 1, dim = 0, last, first;
for (int i = 2; i <= n; i++)
if (a[i - 1] == a[i])
length++;
else
{
last = i - 1;
first = last - length + 1;
dim = dim + length;
for (int j = first; j <= last; j++)
aux[j] = dim;
length = 1;
}
last = n;
first = last - length + 1;
dim = dim + length;
for (int j = first; j <= last; j++)
aux[j] = dim;
n = a[1];
for (int i = n; i >= 1; i--)
a[n - i + 1] = aux[i];
/*for (int j = 1; j <= n; j++)
cout << a[j] << ' ';*/
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
reverse(a + 1, a + n + 1);
int k = findK();
cout << k << ' ';
int ans = DP(k);
flip();
ans = (ans + DP(k) - kFact(k) + MOD) % MOD;
cout << ans;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3720kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3692kb
input:
2 1 1
output:
1 1
result:
wrong answer 2nd numbers differ - expected: '2', found: '1'