QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#507061 | #8635. 圆 | LGMaster | 40 | 0ms | 3964kb | C++14 | 1.3kb | 2024-08-06 09:42:09 | 2024-08-06 09:42:10 |
Judging History
answer
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <assert.h>
using namespace std;
#define LL long long
#define MOD 998244353
#define MAXN 5000
int n;
int f[MAXN+1][4];
int fac[MAXN+1], fac_inv[MAXN+1];
int safe_add(int a, int b) {
return a + b >= MOD ? a+b-MOD : a+b;
}
int qpow(LL a, LL b) {
LL ans = 1;
while (b) ans = ans*(1+(a-1)*(b&1))%MOD, a = a*a%MOD, b>>=1;
return ans;
}
void prepare(int n) {
fac[0] = 1;
for (int i = 1; i<=n; i++) fac[i] = 1ll*fac[i-1]*i%MOD;
fac_inv[n] = qpow(fac[n], MOD-2);
for (int i = n-1; i>=0; i--) fac_inv[i] = 1ll*fac_inv[i+1]*(i+1)%MOD;
}
signed main() {
scanf("%d", &n);
assert(n >= 3 && n <= 10);
prepare(n);
for (int i = 0; i<=n; i++) for (int j = 0; j<=3; j++) f[i][j] = 0;
int cur = 0;
f[0][cur] = 1;
for (int i = 1; i<=n; i++) {
cur = (cur-1+4)&3;
f[0][cur] = 0;
for (int j = 1; j<=n; j++) {
f[j][cur] = safe_add(safe_add(f[j-1][(cur+1)&3], f[j-1][(cur+2)&3]), f[j-1][(cur+3)&3]);
}
}
LL ans = 0;
for (int i = 1; i<=n; i++) {
LL cnt = (1ll*f[i][cur]*fac[i-1]%MOD - 1ll*f[i-1][cur]*fac[i-2]%MOD*(n-i+1)%MOD+MOD)%MOD;
ans = (ans + 1ll*cnt*fac_inv[n-1]%MOD*fac[n-i]%MOD*i)%MOD;
}
printf("%lld\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 0ms
memory: 3896kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 10
Accepted
time: 0ms
memory: 3780kb
input:
4
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 10
Accepted
time: 0ms
memory: 3964kb
input:
6
output:
299473309
result:
ok 1 number(s): "299473309"
Test #4:
score: 10
Accepted
time: 0ms
memory: 3832kb
input:
10
output:
487238321
result:
ok 1 number(s): "487238321"
Test #5:
score: 0
Runtime Error
input:
100
output:
result:
Test #6:
score: 0
Runtime Error
input:
200
output:
result:
Test #7:
score: 0
Runtime Error
input:
500
output:
result:
Test #8:
score: 0
Runtime Error
input:
4798
output:
result:
Test #9:
score: 0
Runtime Error
input:
4999
output:
result:
Test #10:
score: 0
Runtime Error
input:
5000