QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#883657 | #6410. Classical DP Problem | xy3000 | WA | 1ms | 3584kb | C++14 | 2.8kb | 2025-02-05 17:50:38 | 2025-02-05 17:50:39 |
Judging History
answer
#include <bits/stdc++.h>
#define scanf(...) assert(scanf(__VA_ARGS__))
#define INF 0x3f3f3f3f
using namespace std;
using ll = long long;
namespace FastIO {
static char buf[100000], *p1 = buf, *p2 = buf;
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++)
inline ll read() {
ll res = 0;
int w = 0, c = gc;
for (; !isdigit(c); c = gc) {
((c == '-') && (w = 1));
}
for (; isdigit(c); c = gc) {
res = (res << 1) + (res << 3) + (c ^ 48);
}
return (w ? -res : res);
}
inline char readC() {
int c = gc;
while (c == '\n' || c == '\r' || c == ' ') {
c = gc;
}
return c;
}
inline string readS() {
string res = "";
char c = gc;
for (; (c == '\n' || c == '\r' || c == ' ' || c == EOF); c = gc);
for (; !(c == '\n' || c == '\r' || c == ' ' || c == EOF); c = gc) {
res += c;
}
return res;
}
inline double readF() {
double res = 0, tmp = 0.1;
int w = 0;
char c = gc;
for (; !isdigit(c); c = gc) {
((c == '-') && (w = 1));
}
for (; isdigit(c); c = gc) {
res = (res * 10) + (c ^ 48);
}
if (c == '.') {
c = gc;
for (; isdigit(c); c = gc) {
res = res + tmp * (c ^ 48);
tmp *= 0.1;
}
}
return (w ? -res : res);
}
inline void write(ll x, char c = '\n') {
((x < 0) && (putchar('-'), x *= -1));
static int sta[50], top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) {
putchar(sta[--top] + 48);
}
putchar(c);
}
};
using namespace FastIO;
const int N = 5e3 + 5, P = 998244353;
int n, a[N];
int dp[N][N];
int reduce(int x) {
return (x >= P ? x - P : x);
}
int main() {
#ifdef LOCAL
assert(freopen("test.in", "r", stdin));
assert(freopen("test.out", "w", stdout));
#endif
n = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
}
int m = n;
for (int i = n; i >= 1; --i) {
if (a[i] < n - i + 1) {
m = n - i;
break;
}
}
write(m, ' ');
int ans = 0;
int c = a[n - m + 1] - a[n - m];
dp[0][c] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= c; ++j) {
dp[i][j] = (1ll * dp[i - 1][j] * (a[n - m + i] - j) + 1ll * dp[i - 1][j + 1] * (j + 1)) % P;
}
}
dp[0][c] = 0;
ans = reduce(ans + dp[m][0]);
c = m;
for (int i = n - m + 1; i <= n; ++i) {
if (a[i] ^ a[n - m + 1]) {
break;
}
--c;
}
int k = 0;
dp[0][c] = 1;
for (int i = 1; i <= m; ++i) {
while (k <= n && a[n - k] > m - i) {
++k;
}
for (int j = 0; j <= c; ++j) {
dp[i][j] = (1ll * dp[i - 1][j] * (k - j) + 1ll * dp[i - 1][j + 1] * (j + 1)) % P;
}
// cout << i << ' ' << k << '\n';
}
ans = reduce(ans + dp[m][0]);
int tmp = 1;
for (int i = 1; i <= m; ++i) {
tmp = 1ll * tmp * i % P;
}
ans = reduce(ans - tmp + P);
write(ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3456kb
input:
2 1 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
2 2 2
output:
2 6
result:
ok 2 number(s): "2 6"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
3 1 1 1
output:
1 3
result:
ok 2 number(s): "1 3"
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 3584kb
input:
3 2 2 2
output:
2 11
result:
wrong answer 2nd numbers differ - expected: '9', found: '11'