QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#475028 | #8721. 括号序列 | Andyqian7 | AC ✓ | 471ms | 111392kb | C++20 | 4.8kb | 2024-07-13 10:34:45 | 2024-07-13 10:34:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define iter(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) iter(i, 0, a)
#define rep1(i, a) iter(i, 1, (a) + 1)
const int MOD = 998244353;
int fac[524300], ifac[524300], inv[524300], f[524300], g[524300];
namespace poly
{
const int MOD = 998244353, IMAG = 86583718, NTTG = 3;
int rev[524300], minv[524300], w[20][2][524300];
int qpow(int b, int e)
{
int re = 1;
while (e)
{
if (e & 1)
re = 1ll * re * b % MOD;
b = 1ll * b * b % MOD;
e >>= 1;
}
return re;
}
void constructrev(int n)
{
for (int i = 1, j = 0; i < n; i++)
{
int bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
rev[i] = j;
}
}
void constructroot(int n)
{
minv[1] = 1;
iter(i, 2, n + 1)
minv[i] = 1ll * (MOD - MOD / i) * minv[MOD % i] % MOD;
for (int l = 1; (1 << l) <= n; l++)
rep(inv, 2)
{
int re = inv ? qpow(minv[NTTG], (MOD - 1) >> l) : qpow(NTTG, (MOD - 1) >> l);
w[l][inv][0] = 1;
rep1(i, (1 << (l - 1)) - 1) w[l][inv][i] = 1ll * w[l][inv][i - 1] * re % MOD;
}
}
void ntt(int *v, int n, bool inv)
{
rep(i, n) if (i < rev[i]) swap(v[i], v[rev[i]]);
for (int l = 1; (1 << l) <= n; l++)
for (int i = 0; i < n; i += (1 << l))
{
int p = i + (1 << (l - 1));
iter(j, i, p)
{
int a = v[j], b = 1ll * v[j + (1 << (l - 1))] * w[l][inv][j - i] % MOD;
v[j] = (a + b >= MOD ? a + b - MOD : a + b);
v[j + (1 << (l - 1))] = (a < b ? a + MOD - b : a - b);
}
}
if (inv)
rep(i, n) v[i] = 1ll * v[i] * minv[n] % MOD;
}
void cfn(int *a, int as, int *o)
{
static int tmp[524300];
if (as == 1)
{
tmp[0] = a[0];
o[0] = qpow(a[0], MOD - 2);
return;
}
cfn(a, (as + 1) / 2, o);
int le = 0;
while ((1 << le) < (as << 1))
le++;
constructrev(1 << le);
rep(i, as) tmp[i] = a[i];
iter(i, as, 1 << le) tmp[i] = o[i] = 0;
ntt(tmp, 1 << le, 0);
ntt(o, 1 << le, 0);
rep(i, 1 << le) o[i] = 1ll * (MOD + (2 - 1ll * tmp[i] * o[i]) % MOD) * o[i] % MOD;
ntt(o, 1 << le, 1);
iter(i, as, 1 << le) o[i] = 0;
}
void init(int n) { constructroot(n); }
void solve(int as, int *o)
{
static int H1i[524300], H[524300], dg[524300], H1[524300], tmp[524300], tmp2[524300];
if (as == 1)
{
o[0] = 1;
return;
}
solve((as + 1) / 2, o);
int le = 0;
while ((1 << le) < (as << 1))
le++;
constructrev(1 << le);
rep(i, as) tmp[i] = tmp2[i] = o[i];
tmp2[0]++;
iter(i, as, 1 << le) tmp[i] = tmp2[i] = 0;
ntt(tmp, 1 << le, 0), ntt(tmp2, 1 << le, 0);
rep(i, 1 << le) H1i[i] = 1ll * tmp[i] * tmp[i] % MOD * tmp2[i] % MOD;
ntt(H1i, 1 << le, 1);
rep(i, as) dg[i] = 1ll * o[i + 1] * (i + 1) % MOD;
cfn(H1i, as, H1);
rep(i, as) tmp[i] = H1[i];
iter(i, as, 1 << le) dg[i] = tmp[i] = 0;
ntt(dg, 1 << le, 0), ntt(tmp, 1 << le, 0);
rep(i, 1 << le) H[i] = 1ll * dg[i] * tmp[i] % MOD;
ntt(H, 1 << le, 1);
for (int i = as - 2; ~i; i--)
H[i + 1] = 1ll * H[i] * inv[i + 1] % MOD;
H[0] = 0, H[1]--;
if (H[1] < 0)
H[1] += MOD;
iter(i, as, 1 << le) H[i] = H1i[i] = 0;
ntt(H, 1 << le, 0), ntt(H1i, 1 << le, 0);
rep(i, 1 << le) tmp[i] = 1ll * H[i] * H1i[i] % MOD;
ntt(tmp, 1 << le, 1);
rep(i, as)
{
o[i] -= tmp[i];
if (o[i] < 0)
o[i] += MOD;
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
fac[0] = ifac[0] = fac[1] = ifac[1] = inv[0] = inv[1] = 1;
int n = 2.5e5 + 10;
iter(i, 2, n + 2)
{
inv[i] = 1ll * (MOD - MOD / i) * inv[MOD % i] % MOD;
fac[i] = 1ll * fac[i - 1] * i % MOD;
ifac[i] = 1ll * ifac[i - 1] * inv[i] % MOD;
}
int th = 1;
while (th < (n << 1))
th <<= 1;
poly::init(th);
cin >> n;
poly::solve(n + 1, g);
for (int i = n; ~i; i--)
g[i + 1] = 1ll * g[i] * inv[i + 1] % MOD;
rep(i, n + 1) g[i] = MOD - g[i];
g[0] = 1;
poly::cfn(g, n + 1, f);
cout << 1ll * f[n] * fac[n] % MOD;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 9ms
memory: 110136kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 7ms
memory: 110184kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 10ms
memory: 110208kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 110080kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 4ms
memory: 110120kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 7ms
memory: 110060kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 8ms
memory: 110144kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 17ms
memory: 110132kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 7ms
memory: 110128kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 8ms
memory: 110128kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 12ms
memory: 110084kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 15ms
memory: 110144kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 107ms
memory: 110268kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 212ms
memory: 110076kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: 0
Accepted
time: 468ms
memory: 111392kb
input:
131072
output:
842796122
result:
ok 1 number(s): "842796122"
Test #16:
score: 0
Accepted
time: 216ms
memory: 110084kb
input:
131071
output:
981021531
result:
ok 1 number(s): "981021531"
Test #17:
score: 0
Accepted
time: 216ms
memory: 110144kb
input:
131070
output:
480197639
result:
ok 1 number(s): "480197639"
Test #18:
score: 0
Accepted
time: 467ms
memory: 110452kb
input:
131074
output:
383000585
result:
ok 1 number(s): "383000585"
Test #19:
score: 0
Accepted
time: 465ms
memory: 111156kb
input:
131073
output:
316664839
result:
ok 1 number(s): "316664839"
Test #20:
score: 0
Accepted
time: 471ms
memory: 110632kb
input:
250000
output:
119658643
result:
ok 1 number(s): "119658643"
Test #21:
score: 0
Accepted
time: 460ms
memory: 110192kb
input:
249999
output:
78110138
result:
ok 1 number(s): "78110138"
Test #22:
score: 0
Accepted
time: 471ms
memory: 111048kb
input:
249998
output:
297253469
result:
ok 1 number(s): "297253469"
Extra Test:
score: 0
Extra Test Passed