QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#619408 | #8635. 圆 | December456 | 100 ✓ | 151ms | 1668kb | C++17 | 1.3kb | 2024-10-07 14:07:12 | 2024-10-07 14:07:12 |
Judging History
answer
#include <cstdio>
constexpr int MAXN = 5000 + 2;
constexpr int P = 998244353;
int n, f[MAXN], fac[MAXN];
int quickPower(int x, int k) {
int ret = 1;
for (; k; k >>= 1, x = (long long){x} * x % P) {
if (k & 1) {
ret = (long long){ret} * x % P;
}
}
return ret;
}
int inverse(int x) {
return quickPower(x, P - 2);
}
void add(int &x, int y) {
(x += y) >= P && (x -= P);
}
int solve(int x) {
int ret = 0;
for (int i = 1; i <= n; i ++) {
f[i] = 0;
}
f[x] = 1;
for (int i = n + x - 3; i <= n; i ++) {
add(ret, (long long){f[i]} * fac[n - 1] % P);
}
for (int i = 2; i <= n; i ++) {
for (int j = n; j > 2; j --) {
f[j] = ((long long){f[j - 1]} + f[j - 2] + f[j - 3]) % P * i % P;
}
f[2] = (long long){f[1]} * i % P;
f[1] = 0;
for (int j = n + x - 3; j <= n; j ++) {
add(ret, (long long){f[j]} * fac[n - i] % P);
}
}
return ret;
}
int main() {
scanf("%d", &n);
fac[0] = 1;
for (int i = 1; i <= n; i ++) {
fac[i] = (long long){fac[i - 1]} * i % P;
}
int ans = ((long long){solve(1)} + solve(2) + solve(3)) * inverse(fac[n]) % P;
printf("%d\n", (n + 1 - ans + P) % P);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 0ms
memory: 1616kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 10
Accepted
time: 0ms
memory: 1548kb
input:
4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 10
Accepted
time: 0ms
memory: 1588kb
input:
6
output:
299473309
result:
ok 1 number(s): "299473309"
Test #4:
score: 10
Accepted
time: 0ms
memory: 1620kb
input:
10
output:
487238321
result:
ok 1 number(s): "487238321"
Test #5:
score: 10
Accepted
time: 0ms
memory: 1536kb
input:
100
output:
41620761
result:
ok 1 number(s): "41620761"
Test #6:
score: 10
Accepted
time: 0ms
memory: 1640kb
input:
200
output:
208771764
result:
ok 1 number(s): "208771764"
Test #7:
score: 10
Accepted
time: 0ms
memory: 1644kb
input:
500
output:
888621375
result:
ok 1 number(s): "888621375"
Test #8:
score: 10
Accepted
time: 140ms
memory: 1564kb
input:
4798
output:
319137015
result:
ok 1 number(s): "319137015"
Test #9:
score: 10
Accepted
time: 151ms
memory: 1644kb
input:
4999
output:
818467659
result:
ok 1 number(s): "818467659"
Test #10:
score: 10
Accepted
time: 151ms
memory: 1668kb
input:
5000
output:
142907477
result:
ok 1 number(s): "142907477"