QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620221 | #8635. 圆 | raywu | 100 ✓ | 40ms | 198424kb | C++14 | 1.2kb | 2024-10-07 17:02:39 | 2024-10-07 17:02:59 |
Judging History
answer
#include <bits/stdc++.h>
#define _for(i, a, b) for (int i = (a); i <= (b); i ++ )
#define ll long long
using namespace std;
const int N = 5005, P = 998244353;
int n; ll ans, g[N], fac[N], f[N][N];
inline void Add(ll & x, ll y) { (x += y % P) %= P; }
inline void Mul(ll & x, ll y) { (x *= y % P) %= P; }
inline ll pw(ll a, int b) {
ll res = 1;
while (b) {
if (b & 1) Mul(res, a);
Mul(a, a), b >>= 1;
}
return res;
}
int main() {
ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n, n -- , fac[0] = fac[1] = f[0][0] = f[1][1] = f[1][2] = f[1][3] = 1;
if (n == 2) { cout << "1\n"; return 0; }
if (n == 3) { cout << "2\n"; return 0; }
_for (i, 2, n) {
fac[i] = fac[i - 1] * i % P;
_for (j, i, min(n, 3 * i)) {
f[i][j] = f[i - 1][j - 1];
if (j > 2) Add(f[i][j], f[i - 1][j - 2]);
if (j > 3) Add(f[i][j], f[i - 1][j - 3]);
}
}
_for (i, 1, n) {
g[i] = (f[i][n - 1] + f[i][n - 2] + f[i - 1][n - 1] + f[i - 1][n - 2] + f[i - 1][n - 3]) % P;
Add(ans, (g[i] * fac[i] % P * fac[n - i] % P - g[i - 1] * fac[i - 1] % P * fac[n - i + 1] % P + P) % P * i % P);
}
cout << (ans * pw(fac[n], P - 2) % P + P + 1) % P << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 1ms
memory: 3648kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 10
Accepted
time: 0ms
memory: 3680kb
input:
4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 10
Accepted
time: 0ms
memory: 3624kb
input:
6
output:
299473309
result:
ok 1 number(s): "299473309"
Test #4:
score: 10
Accepted
time: 0ms
memory: 3692kb
input:
10
output:
487238321
result:
ok 1 number(s): "487238321"
Test #5:
score: 10
Accepted
time: 1ms
memory: 7744kb
input:
100
output:
41620761
result:
ok 1 number(s): "41620761"
Test #6:
score: 10
Accepted
time: 2ms
memory: 12100kb
input:
200
output:
208771764
result:
ok 1 number(s): "208771764"
Test #7:
score: 10
Accepted
time: 0ms
memory: 24132kb
input:
500
output:
888621375
result:
ok 1 number(s): "888621375"
Test #8:
score: 10
Accepted
time: 27ms
memory: 192180kb
input:
4798
output:
319137015
result:
ok 1 number(s): "319137015"
Test #9:
score: 10
Accepted
time: 40ms
memory: 196512kb
input:
4999
output:
818467659
result:
ok 1 number(s): "818467659"
Test #10:
score: 10
Accepted
time: 32ms
memory: 198424kb
input:
5000
output:
142907477
result:
ok 1 number(s): "142907477"