QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#764217 | #6333. Festivals in JOI Kingdom 2 | KiharaTouma | 100 ✓ | 3211ms | 4960kb | C++23 | 1.9kb | 2024-11-20 07:40:35 | 2024-11-20 07:40:36 |
Judging History
answer
//qoj6333
#include <bits/stdc++.h>
using namespace std;
const int N = 40010;
int n;
typedef long long ll;
ll P, f[N][2], fac[N], inv[N];
struct Mod{
ll m, p;
void init(int pp){
m = ((__int128)1 << 64) / pp;
p = pp;
}
ll operator ()(ll x){
return x - ((__int128(x) * m) >> 64) * p;
}
} mod;
void chk(ll &x){
while(x >= P){
x -= P;
}
}
ll qp(ll x, ll y){
ll ans = 1;
while(y){
if(y & 1){
ans = mod(ans * x);
}
x = mod(x * x);
y >>= 1;
}
return ans;
}
ll C(int n, int m){
return mod(mod(fac[n] * inv[m]) * inv[n-m]);
}
int main(){
scanf("%d%lld", &n, &P);
mod.init(P);
fac[0] = 1;
for(int i = 1; i < N; ++ i){
fac[i] = mod(fac[i-1] * i);
}
inv[N-1] = qp(fac[N-1], P-2);
for(int i = N-2; i >= 0; -- i){
inv[i] = mod(inv[i+1] * (i+1));
}
f[2][0] = f[1][1] = 1;
for(int i = 1; i < n; ++ i){
for(int j = 0; i + j <= n; ++ j){
f[i+j+2][0] += mod(mod(f[i][0] * fac[i+i+j-3]) * mod(inv[i+i-3] * (j + 1) * (j + 2)));
f[i+j+1][1] += mod(mod(f[i][0] * fac[i+i+j-3]) * mod(inv[i+i-3] * (j + 1)));
f[i+j+2][0] += mod(mod(f[i][1] * fac[i+i+j-2]) * mod(inv[i+i-2] * (j + 1)));
f[i+j+1][1] += mod(mod(f[i][1] * fac[i+i+j-2]) * mod(inv[i+i-2]));
chk(f[i+j+2][0]);
chk(f[i+j+1][1]);
}
}
ll ans = 1, sum = 0;
for(int i = n + n - 1; i >= 1; i -= 2){
ans = mod(ans * i);
}
for(int i = n; i >= 1; -- i){
int j = n - i;
sum += mod(mod(f[i][0] * fac[i+i+j-3]) * mod(inv[i+i-3] * (j + 1)));
sum += mod(f[i][1] * mod(fac[i+i+j-2] * inv[i+i-2]));
chk(sum);
}
ans = ans + P - sum;
printf("%lld\n", ans % P);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 1ms
memory: 4440kb
input:
1 194903119
output:
0
result:
ok 1 number(s): "0"
Test #2:
score: 5
Accepted
time: 0ms
memory: 4372kb
input:
2 933234047
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 5
Accepted
time: 0ms
memory: 4380kb
input:
3 277793111
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 5
Accepted
time: 1ms
memory: 4372kb
input:
4 355321177
output:
28
result:
ok 1 number(s): "28"
Test #5:
score: 5
Accepted
time: 1ms
memory: 4396kb
input:
5 306636893
output:
358
result:
ok 1 number(s): "358"
Subtask #2:
score: 5
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 5
Accepted
time: 1ms
memory: 4488kb
input:
8 361605653
output:
1236922
result:
ok 1 number(s): "1236922"
Test #7:
score: 5
Accepted
time: 1ms
memory: 4440kb
input:
8 995512643
output:
1236922
result:
ok 1 number(s): "1236922"
Test #8:
score: 5
Accepted
time: 1ms
memory: 4728kb
input:
8 101102801
output:
1236922
result:
ok 1 number(s): "1236922"
Test #9:
score: 5
Accepted
time: 1ms
memory: 4724kb
input:
6 458322727
output:
4894
result:
ok 1 number(s): "4894"
Test #10:
score: 5
Accepted
time: 1ms
memory: 4504kb
input:
7 721691819
output:
73884
result:
ok 1 number(s): "73884"
Test #11:
score: 5
Accepted
time: 1ms
memory: 4720kb
input:
7 370629137
output:
73884
result:
ok 1 number(s): "73884"
Subtask #3:
score: 27
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #12:
score: 27
Accepted
time: 0ms
memory: 4508kb
input:
30 779092367
output:
686412377
result:
ok 1 number(s): "686412377"
Test #13:
score: 27
Accepted
time: 1ms
memory: 4428kb
input:
29 963995171
output:
128570082
result:
ok 1 number(s): "128570082"
Test #14:
score: 27
Accepted
time: 0ms
memory: 4424kb
input:
18 666092701
output:
185922458
result:
ok 1 number(s): "185922458"
Test #15:
score: 27
Accepted
time: 1ms
memory: 4380kb
input:
14 671243719
output:
623913899
result:
ok 1 number(s): "623913899"
Subtask #4:
score: 14
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #16:
score: 14
Accepted
time: 2ms
memory: 4476kb
input:
300 463478027
output:
89265245
result:
ok 1 number(s): "89265245"
Test #17:
score: 14
Accepted
time: 2ms
memory: 4420kb
input:
296 567610679
output:
406342763
result:
ok 1 number(s): "406342763"
Test #18:
score: 14
Accepted
time: 0ms
memory: 4504kb
input:
297 609090359
output:
128986577
result:
ok 1 number(s): "128986577"
Test #19:
score: 14
Accepted
time: 0ms
memory: 4732kb
input:
48 759569383
output:
635573072
result:
ok 1 number(s): "635573072"
Test #20:
score: 14
Accepted
time: 0ms
memory: 4732kb
input:
99 298873033
output:
285340078
result:
ok 1 number(s): "285340078"
Subtask #5:
score: 36
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #21:
score: 36
Accepted
time: 74ms
memory: 4420kb
input:
3000 752129633
output:
33058561
result:
ok 1 number(s): "33058561"
Test #22:
score: 36
Accepted
time: 73ms
memory: 4768kb
input:
2993 491173567
output:
343277625
result:
ok 1 number(s): "343277625"
Test #23:
score: 36
Accepted
time: 73ms
memory: 4768kb
input:
2993 783095563
output:
776085006
result:
ok 1 number(s): "776085006"
Test #24:
score: 36
Accepted
time: 2ms
memory: 4476kb
input:
327 399783431
output:
163535283
result:
ok 1 number(s): "163535283"
Test #25:
score: 36
Accepted
time: 14ms
memory: 4388kb
input:
1233 857060207
output:
422139845
result:
ok 1 number(s): "422139845"
Test #26:
score: 36
Accepted
time: 7ms
memory: 4444kb
input:
1114 600227447
output:
598509129
result:
ok 1 number(s): "598509129"
Subtask #6:
score: 13
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #27:
score: 13
Accepted
time: 3210ms
memory: 4684kb
input:
20000 221054167
output:
39809956
result:
ok 1 number(s): "39809956"
Test #28:
score: 13
Accepted
time: 3211ms
memory: 4736kb
input:
19997 823974001
output:
267537750
result:
ok 1 number(s): "267537750"
Test #29:
score: 13
Accepted
time: 3205ms
memory: 4804kb
input:
19991 501689843
output:
16527909
result:
ok 1 number(s): "16527909"
Test #30:
score: 13
Accepted
time: 1652ms
memory: 4960kb
input:
14344 925452091
output:
212324779
result:
ok 1 number(s): "212324779"
Test #31:
score: 13
Accepted
time: 300ms
memory: 4784kb
input:
6098 507350869
output:
310480789
result:
ok 1 number(s): "310480789"
Test #32:
score: 13
Accepted
time: 245ms
memory: 4808kb
input:
5564 406069759
output:
105694337
result:
ok 1 number(s): "105694337"
Extra Test:
score: 0
Extra Test Passed