QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#118003 | #6333. Festivals in JOI Kingdom 2 | Unique_Hanpi | 100 ✓ | 875ms | 29028kb | C++14 | 3.7kb | 2023-07-02 20:44:26 | 2023-07-02 20:44:29 |
Judging History
answer
#include <bits/stdc++.h>
#define lowbit(x) (x & -x)
#define eb emplace_back
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef vector<int> poly;
const int N = 4e4+5;
struct FastMod {
ll p, m;
FastMod(ll pp): p(pp), m((1ll << 62) / pp) { }
ll operator ()(ll x) {
ll r = x - p * ((__int128) x * m >> 62);
return r >= p ? r - p : r;
}
} F(2);
int n, Mod, ans = 1;
int f[N][2];
ll Bfac[N], Bifac[N], *fac = Bfac + 2, *ifac = Bifac + 2;
inline ll qpow(int a, int b) {
ll base = a, ans = 1;
while (b) {
if (b & 1) ans = ans * base % Mod;
base = base * base % Mod;
b >>= 1;
}
return ans;
}
inline void Add(int &a, int b) { a += b; if (a >= Mod) a -= Mod; }
inline void Add(ll &a, int b) { a += b; if (a >= Mod) a -= Mod; }
ll A[N], B[N], C[N], r0[15][N], r1[15][N], r2[15][N], x[15][N], y[15][N];
inline int mul(int d, ll *a, ll *b, int n, ll *g) {
if (!n) return 0;
for (int i = 0; i < 2 * n - 1; i++) g[i] = 0;
// if (1) {
if (n <= 16) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
g[i + j] = F(g[i + j] + a[i] * b[j]);
return 2 * n - 1;
}
int mid = (n + 1) / 2;
int l0 = mul(d + 1, a, b, mid, r0[d]);
int l1 = mul(d + 1, a + mid, b + mid, n - mid, r1[d]);
for (int i = 0; i < l0; i++) Add(g[i + mid], Mod - r0[d][i]), Add(g[i], r0[d][i]);
for (int i = 0; i < l1; i++) Add(g[i + mid], Mod - r1[d][i]), Add(g[i + 2 * mid], r1[d][i]);
for (int i = 0; i < mid; i++) {
x[d][i] = a[i], y[d][i] = b[i];
if (mid + i < n) Add(x[d][i], a[mid + i]), Add(y[d][i], b[mid + i]);
}
int l2 = mul(d + 1, x[d], y[d], mid, r2[d]);
for (int i = 0; i < l2; i++) Add(g[i + mid], r2[d][i]);
return 2 * n - 1;
}
inline ll c0(int p, int q, ll j) { return F(p * q * j * j + (p + q - p * q) * j); }
inline ll c1(int p, int q, ll i) { return F(p * q * i * i + (p * q - p - q) * i + (1 - p) * (1 - q) + Mod); }
void solve(int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
solve(l, mid);
for (int p = 0; p < 2; p++)
for (int q = 0; q < 2; q++) {
for (int i = 0; i < r - l; i++) B[i] = fac[mid - 2 + l - q + i - p], A[i] = 0;
if (p * q || (p + q - p * q)) {
for (int i = l; i <= mid; i++) A[mid - i] = F(f[i][p] * ifac[2 * i - 2 - p]);
mul(0, A, B, r - l, C);
for (int i = mid - l; i <= r - l - 1; i++) {
int j = i + 1 + l - q;
f[j + q][q] = F(f[j + q][q] + c0(p, q, j) * C[i]);
}
}
if (p * q || (p * q - p - q) || (1 - p) * (1 - q)) {
for (int i = l; i <= mid; i++) A[mid - i] = F(F(f[i][p] * ifac[2 * i - 2 - p]) * c1(p, q, i));
mul(0, A, B, r - l, C);
for (int i = mid - l; i <= r - l - 1; i++) Add(f[i + 1 + l][q], C[i]);
}
if (p * q) {
for (int i = l; i <= mid; i++) A[mid - i] = F(F(f[i][p] * ifac[2 * i - 2 - p]) * i);
mul(0, A, B, r - l, C);
for (int i = mid - l; i <= r - l - 1; i++) {
int j = i + 1 + l - q;
Add(f[j + q][q], Mod - F(C[i] * 2 * j));
}
}
}
solve(mid + 1, r);
}
int main() {
scanf("%d%d", &n, &Mod);
F = FastMod(Mod);
fac[0] = 1;
for (int i = 1; i <= 2 * n; i++) fac[i] = fac[i - 1] * i % Mod;
ifac[2 * n] = qpow(fac[2 * n], Mod - 2);
for (int i = 2 * n; i; i--) ifac[i - 1] = ifac[i] * i % Mod;
f[1][0] = f[2][1] = 1;
solve(1, n);
for (int i = 3; i <= 2 * n; i += 2) ans = (ll) ans * i % Mod;
for (int i = 1; i <= n; i++)
for (int p = 0; p < 2; p++) {
ll t = f[i][p];
if (p) t = t * (n - i + 1) % Mod;
Add(ans, Mod - t * fac[n + i - 3 + !p] % Mod * ifac[2 * i - 3 + !p] % Mod);
}
printf("%d", ans);
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: 3668kb
input:
1 194903119
output:
0
result:
ok 1 number(s): "0"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5948kb
input:
2 933234047
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 5792kb
input:
3 277793111
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 5704kb
input:
4 355321177
output:
28
result:
ok 1 number(s): "28"
Test #5:
score: 0
Accepted
time: 0ms
memory: 5808kb
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: 0ms
memory: 5908kb
input:
8 361605653
output:
1236922
result:
ok 1 number(s): "1236922"
Test #7:
score: 0
Accepted
time: 1ms
memory: 5764kb
input:
8 995512643
output:
1236922
result:
ok 1 number(s): "1236922"
Test #8:
score: 0
Accepted
time: 1ms
memory: 5804kb
input:
8 101102801
output:
1236922
result:
ok 1 number(s): "1236922"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
6 458322727
output:
4894
result:
ok 1 number(s): "4894"
Test #10:
score: 0
Accepted
time: 1ms
memory: 5724kb
input:
7 721691819
output:
73884
result:
ok 1 number(s): "73884"
Test #11:
score: 0
Accepted
time: 1ms
memory: 5716kb
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: 3ms
memory: 13908kb
input:
30 779092367
output:
686412377
result:
ok 1 number(s): "686412377"
Test #13:
score: 0
Accepted
time: 3ms
memory: 14148kb
input:
29 963995171
output:
128570082
result:
ok 1 number(s): "128570082"
Test #14:
score: 0
Accepted
time: 1ms
memory: 13968kb
input:
18 666092701
output:
185922458
result:
ok 1 number(s): "185922458"
Test #15:
score: 0
Accepted
time: 1ms
memory: 5968kb
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: 1ms
memory: 20140kb
input:
300 463478027
output:
89265245
result:
ok 1 number(s): "89265245"
Test #17:
score: 0
Accepted
time: 4ms
memory: 20300kb
input:
296 567610679
output:
406342763
result:
ok 1 number(s): "406342763"
Test #18:
score: 0
Accepted
time: 4ms
memory: 20312kb
input:
297 609090359
output:
128986577
result:
ok 1 number(s): "128986577"
Test #19:
score: 0
Accepted
time: 3ms
memory: 16016kb
input:
48 759569383
output:
635573072
result:
ok 1 number(s): "635573072"
Test #20:
score: 0
Accepted
time: 4ms
memory: 19992kb
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: 41ms
memory: 26452kb
input:
3000 752129633
output:
33058561
result:
ok 1 number(s): "33058561"
Test #22:
score: 0
Accepted
time: 46ms
memory: 24380kb
input:
2993 491173567
output:
343277625
result:
ok 1 number(s): "343277625"
Test #23:
score: 0
Accepted
time: 42ms
memory: 26328kb
input:
2993 783095563
output:
776085006
result:
ok 1 number(s): "776085006"
Test #24:
score: 0
Accepted
time: 1ms
memory: 22132kb
input:
327 399783431
output:
163535283
result:
ok 1 number(s): "163535283"
Test #25:
score: 0
Accepted
time: 9ms
memory: 22192kb
input:
1233 857060207
output:
422139845
result:
ok 1 number(s): "422139845"
Test #26:
score: 0
Accepted
time: 8ms
memory: 24164kb
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: 875ms
memory: 29028kb
input:
20000 221054167
output:
39809956
result:
ok 1 number(s): "39809956"
Test #28:
score: 0
Accepted
time: 868ms
memory: 28788kb
input:
19997 823974001
output:
267537750
result:
ok 1 number(s): "267537750"
Test #29:
score: 0
Accepted
time: 871ms
memory: 28620kb
input:
19991 501689843
output:
16527909
result:
ok 1 number(s): "16527909"
Test #30:
score: 0
Accepted
time: 499ms
memory: 26800kb
input:
14344 925452091
output:
212324779
result:
ok 1 number(s): "212324779"
Test #31:
score: 0
Accepted
time: 129ms
memory: 28300kb
input:
6098 507350869
output:
310480789
result:
ok 1 number(s): "310480789"
Test #32:
score: 0
Accepted
time: 112ms
memory: 24520kb
input:
5564 406069759
output:
105694337
result:
ok 1 number(s): "105694337"
Extra Test:
score: 0
Extra Test Passed