QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#619838 | #8635. 圆 | zfs732 | 100 ✓ | 51ms | 101120kb | C++14 | 2.3kb | 2024-10-07 15:35:22 | 2024-10-07 15:35:22 |
Judging History
answer
/*
* _|_|_|_|_| _|_|_|_| _|_|_| _|_|_|_|_| _|_|_| _|_|
* _| _| _| _| _| _| _|
* _| _|_|_| _|_| _| _|_| _|
* _| _| _| _| _| _|
* _|_|_|_|_| _| _|_|_| _| _|_|_| _|_|_|_|
*/
#include <bits/stdc++.h>
constexpr int pMod = 998244353;
template<class Tp>
Tp Add(Tp x) {
return x;
}
template<class Tp>
Tp Mul(Tp x) {
return x;
}
template<class Tp, class... Tps>
int Add(Tp x, Tps... y) {
return (x + Add(y...)) % pMod;
}
template<class Tp, class... Tps>
int Mul(Tp x, Tps... y) {
return int(1LL * x * Mul(y...) % pMod);
}
template<class Tp, class... Tps>
void AddC(Tp &x, Tps... y) {
x = Add(x, y...);
}
template<class Tp, class... Tps>
void MulC(Tp &x, Tps... y) {
x = Mul(x, y...);
}
int ExGcd(int a, int b, int &x, int &y) {
if (b == 0) return x = 1, y = 0, a;
int d = ExGcd(b, a % b, y, x);
return y -= a / b * x, d;
}
int Inv(int x) {
int ret, t, d = ExGcd(x, pMod, ret, t);
return d * Add(ret, pMod);
}
std::vector<int> fac, invFac;
void InitFac(int n) {
fac.resize(n + 1);
invFac.resize(n + 1);
fac[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = Mul(fac[i - 1], i);
invFac[n] = Inv(fac[n]);
for (int i = n; i >= 1; i--) invFac[i - 1] = Mul(invFac[i], i);
}
namespace Solution {
int Solve(int N) {
if (N == 3) {
return 1;
}
InitFac(N);
int ans = 0;
std::vector<std::vector<int>> dp(N, std::vector<int>(N));
dp[1][1] = 1;
for (int i = 1; i + 2 < N; i++) {
for (int j = 1; j <= i; j++) {
for (int k = 1; k <= 3; k++) {
if (i + k + 2 < N) {
AddC(dp[i + k][j + 1], dp[i][j]);
}
}
}
}
for (int l = 0; l < 3; l++) {
int p = N - 3 - l;
if (p <= 0) {
continue;
}
for (int j = 1; j <= p; j++) {
int v = Mul(dp[p][j], fac[j]);
MulC(v, invFac[N], fac[N - j - 1]);
AddC(ans, Mul(v, 3 - l, j + 1));
}
}
return Mul(ans, N);
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::cout << Solution::Solve(N) << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 0ms
memory: 3616kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 10
Accepted
time: 0ms
memory: 3616kb
input:
4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 10
Accepted
time: 0ms
memory: 3608kb
input:
6
output:
299473309
result:
ok 1 number(s): "299473309"
Test #4:
score: 10
Accepted
time: 0ms
memory: 3644kb
input:
10
output:
487238321
result:
ok 1 number(s): "487238321"
Test #5:
score: 10
Accepted
time: 0ms
memory: 3652kb
input:
100
output:
41620761
result:
ok 1 number(s): "41620761"
Test #6:
score: 10
Accepted
time: 1ms
memory: 3632kb
input:
200
output:
208771764
result:
ok 1 number(s): "208771764"
Test #7:
score: 10
Accepted
time: 1ms
memory: 4108kb
input:
500
output:
888621375
result:
ok 1 number(s): "888621375"
Test #8:
score: 10
Accepted
time: 47ms
memory: 93336kb
input:
4798
output:
319137015
result:
ok 1 number(s): "319137015"
Test #9:
score: 10
Accepted
time: 51ms
memory: 101000kb
input:
4999
output:
818467659
result:
ok 1 number(s): "818467659"
Test #10:
score: 10
Accepted
time: 49ms
memory: 101120kb
input:
5000
output:
142907477
result:
ok 1 number(s): "142907477"