QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#475334 | #8721. 括号序列 | Andyqian7 | AC ✓ | 453ms | 108980kb | C++20 | 4.7kb | 2024-07-13 14:07:37 | 2024-07-13 14:07:37 |
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];
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] = o[i];
iter(i, as, 1 << le) tmp[i] = 0;
ntt(tmp, 1 << le, 0);
rep(i, 1 << le) H1i[i] = 1ll * tmp[i] * tmp[i] % MOD * (tmp[i] + 1) % 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,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 12ms
memory: 108084kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 8ms
memory: 108064kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 8ms
memory: 108032kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 8ms
memory: 108008kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 8ms
memory: 108088kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 8ms
memory: 108092kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 8ms
memory: 108012kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 7ms
memory: 108136kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 8ms
memory: 108080kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 4ms
memory: 108068kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 11ms
memory: 108084kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 11ms
memory: 108012kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 99ms
memory: 108080kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 205ms
memory: 107324kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: 0
Accepted
time: 444ms
memory: 108648kb
input:
131072
output:
842796122
result:
ok 1 number(s): "842796122"
Test #16:
score: 0
Accepted
time: 202ms
memory: 107096kb
input:
131071
output:
981021531
result:
ok 1 number(s): "981021531"
Test #17:
score: 0
Accepted
time: 205ms
memory: 108004kb
input:
131070
output:
480197639
result:
ok 1 number(s): "480197639"
Test #18:
score: 0
Accepted
time: 442ms
memory: 108480kb
input:
131074
output:
383000585
result:
ok 1 number(s): "383000585"
Test #19:
score: 0
Accepted
time: 453ms
memory: 108184kb
input:
131073
output:
316664839
result:
ok 1 number(s): "316664839"
Test #20:
score: 0
Accepted
time: 441ms
memory: 108632kb
input:
250000
output:
119658643
result:
ok 1 number(s): "119658643"
Test #21:
score: 0
Accepted
time: 442ms
memory: 108980kb
input:
249999
output:
78110138
result:
ok 1 number(s): "78110138"
Test #22:
score: 0
Accepted
time: 441ms
memory: 108548kb
input:
249998
output:
297253469
result:
ok 1 number(s): "297253469"
Extra Test:
score: 0
Extra Test Passed