QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417096 | #8721. 括号序列 | zhoukangyang# | AC ✓ | 1230ms | 33856kb | C++14 | 8.8kb | 2024-05-22 14:17:51 | 2024-05-22 14:17:52 |
Judging History
answer
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int >
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
#define pb emplace_back
using namespace std;
const int mod = 998244353, _G = 3, N = (1 << 21), inv2 = (mod + 1) / 2;
#define add(a, b) (a + b >= mod ? a + b - mod : a + b)
#define dec(a, b) (a < b ? a - b + mod : a - b)
int qpow(int x, int y = mod - 2) {
int res = 1;
for(; y; x = (ll) x * x % mod, y >>= 1) if(y & 1) res = (ll) res * x % mod;
return res;
}
int fac[N + 1], ifac[N + 1], inv[N + 1];
void init(int x) {
fac[0] = ifac[0] = inv[1] = 1;
L(i, 2, x) inv[i] = (ll) inv[mod % i] * (mod - mod / i) % mod;
L(i, 1, x) fac[i] = (ll) fac[i - 1] * i % mod, ifac[i] = (ll) ifac[i - 1] * inv[i] % mod;
}
int C(int x, int y) {
return y < 0 || x < y ? 0 : (ll) fac[x] * ifac[y] % mod * ifac[x - y] % mod;
}
inline int sgn(int x) {
return (x & 1) ? mod - 1 : 1;
}
int rt[N], Lim;
void Pinit(int x) {
for(Lim = 1; Lim <= x; Lim <<= 1) ;
for(int i = 1; i < Lim; i <<= 1) {
int sG = qpow (_G, (mod - 1) / (i << 1));
rt[i] = 1;
L(j, i + 1, i * 2 - 1) rt[j] = (ll) rt[j - 1] * sG % mod;
}
}
struct poly {
vector<int> a;
int size() { return sz(a); }
int & operator [] (int x) { return a[x]; }
int v(int x) { return x < 0 || x >= sz(a) ? 0 : a[x]; }
void clear() { vector<int> ().swap(a); }
void rs(int x = 0) { a.resize(x); }
poly (int n = 0) { rs(n); }
poly (vector<int> o) { a = o; }
poly (const poly &o) { a = o.a; }
poly Rs(int x = 0) { vi res = a; res.resize(x); return res; }
inline void dif() {
int n = sz(a);
for (int l = n >> 1; l >= 1; l >>= 1)
for(int j = 0; j < n; j += l << 1)
for(int k = 0, *w = rt + l; k < l; k++, w++) {
int x = a[j + k], y = a[j + k + l];
a[j + k] = add(x, y);
a[j + k + l] = (ll) * w * dec(x, y) % mod;
}
}
void dit () {
int n = sz(a);
for(int i = 2; i <= n; i <<= 1)
for(int j = 0, l = (i >> 1); j < n; j += i)
for(int k = 0, *w = rt + l; k < l; k++, w++) {
int pa = a[j + k], pb = (ll) a[j + k + l] * *w % mod;
a[j + k] = add(pa, pb), a[j + k + l] = dec(pa, pb);
}
reverse(a.begin() + 1, a.end());
for(int i = 0, iv = qpow(n); i < n; i++) a[i] = (ll) a[i] * iv % mod;
}
friend poly operator * (poly aa, poly bb) {
if(!sz(aa) || !sz(bb)) return {};
int lim, all = sz(aa) + sz(bb) - 1;
for(lim = 1; lim < all; lim <<= 1);
aa.rs(lim), bb.rs(lim), aa.dif(), bb.dif();
L(i, 0, lim - 1) aa[i] = (ll) aa[i] * bb[i] % mod;
aa.dit(), aa.a.resize(all);
return aa;
}
poly Inv() {
poly res, f, g;
res.rs(1), res[0] = qpow(a[0]);
for(int m = 1, pn; m < sz(a); m <<= 1) {
pn = m << 1, f = res, g.rs(pn), f.rs(pn);
for(int i = 0; i < pn; i++) g[i] = (*this).v(i);
f.dif(), g.dif();
for(int i = 0; i < pn; i++) g[i] = (ll) f[i] * g[i] % mod;
g.dit();
for(int i = 0; i < m; i++) g[i] = 0;
g.dif();
for(int i = 0; i < pn; i++) g[i] = (ll) f[i] * g[i] % mod;
g.dit(), res.rs(pn);
for(int i = m; i < min(pn, sz(a)); i++) res[i] = (mod - g[i]) % mod;
}
return res.rs(sz(a)), res;
}
poly Shift (int x) {
poly zm (sz(a) + x);
L(i, max(-x, 0), sz(a) - 1) zm[i + x] = a[i];
return zm;
}
friend poly operator * (poly aa, int bb) {
poly res(sz(aa));
L(i, 0, sz(aa) - 1) res[i] = (ll) aa[i] * bb % mod;
return res;
}
friend poly operator + (poly aa, poly bb) {
vector<int> res(max(sz(aa), sz(bb)));
L(i, 0, sz(res) - 1) res[i] = add(aa.v(i), bb.v(i));
return poly(res);
}
friend poly operator - (poly aa, poly bb) {
vector<int> res(max(sz(aa), sz(bb)));
L(i, 0, sz(res) - 1) res[i] = dec(aa.v(i), bb.v(i));
return poly(res);
}
poly & operator += (poly o) {
rs(max(sz(a), sz(o)));
L(i, 0, sz(a) - 1) (a[i] += o.v(i)) %= mod;
return (*this);
}
poly & operator -= (poly o) {
rs(max(sz(a), sz(o)));
L(i, 0, sz(a) - 1) (a[i] += mod - o.v(i)) %= mod;
return (*this);
}
poly & operator *= (poly o) {
return (*this) = (*this) * o;
}
poly Integ() {
if(!sz(a)) return poly();
poly res(sz(a) + 1);
L(i, 1, sz(a)) res[i] = (ll) a[i - 1] * inv[i] % mod;
return res;
}
poly Deriv() {
if(!sz(a)) return poly();
poly res(sz(a) - 1);
L(i, 1, sz(a) - 1) res[i - 1] = (ll) a[i] * i % mod;
return res;
}
poly Ln() {
poly g = ((*this).Inv() * (*this).Deriv()).Integ();
return g.rs(sz(a)), g;
}
poly Exp() {
poly res(1), f;
res[0] = 1;
for(int m = 1, pn; m < sz(a); m <<= 1) {
pn = min(m << 1, sz(a)), f.rs(pn), res.rs(pn);
for(int i = 0; i < pn; i++) f[i] = (*this).v(i);
f -= res.Ln(), (f[0] += 1) %= mod, res *= f, res.rs(pn);
}
return res.rs(sz(a)), res;
}
poly pow(int x, int rx = -1) { // x : the power % mod; rx : the power % (mod - 1)
if(rx == -1) rx = x;
int cnt = 0;
while (a[cnt] == 0 && cnt < sz(a)) cnt += 1;
poly res = (*this);
L(i, cnt, sz(a) - 1) res[i - cnt] = res[i];
L(i, sz(a) - cnt, sz(a) - 1) res[i] = 0;
int c = res[0], w = qpow (res[0]);
L(i, 0, sz(res) - 1) res[i] = (ll) res[i] * w % mod;
res = res.Ln();
L(i, 0, sz(res) - 1) res[i] = (ll) res[i] * x % mod;
res = res.Exp();
c = qpow (c, rx);
L(i, 0, sz(res) - 1) res[i] = (ll) res[i] * c % mod;
if((ll) cnt * x > sz(a)) L(i, 0, sz(a) - 1) res[i] = 0;
else if(cnt) {
R(i, sz(a) - cnt * x - 1, 0) res[i + cnt * x] = res[i];
L(i, 0, cnt * x - 1) res[i] = 0;
}
return res;
}
poly sqrt(int rt = 1) {
poly res(1), f;
res[0] = rt;
for(int m = 1, pn; m < sz(a); m <<= 1) {
pn = min(m << 1, sz(a)), f.rs(pn);
for(int i = 0; i < pn; i++) f[i] = (*this).v(i);
f += res * res, f.rs(pn), res.rs(pn), res = f * res.Inv(), res.rs(pn);
for(int i = 0; i < pn; i++) res[i] = (ll) res[i] * inv2 % mod;
}
return res;
}
void Rev() {
reverse(a.begin(), a.end());
}
friend pair < poly, poly > div (poly f, poly g) { /* f / g = first, f % g = second */
f.rs(max(sz(f), sz(g))), f.Rev(), g.Rev();
int n = sz(f), m = sz(g);
poly A = g.Rs(n - m + 1).Inv(), t;
A *= f.Rs(n - m + 1), A.rs(n - m + 1), A.Rev(), g.Rev(), f.Rev(), t = f - A * g, t.rs(m - 1);
return make_pair(A, t);
}
} ;
int n;
int a[N], b[N], c[N];
poly Mult(poly aa, poly bb, int len) {
int lim, all = len;
for(lim = 1; lim < all; lim <<= 1);
aa.rs(lim), bb.rs(lim), aa.dif(), bb.dif();
L(i, 0, lim - 1) aa[i] = (ll) aa[i] * bb[i] % mod;
aa.dit(), aa.a.resize(all);
return aa;
}
void dc(int l, int r) {
if(l == r) {
if(l == 0) return ;
int i = l;
(a[i] += (ll) c[i] * 2 % mod) %= mod;
// L(j, 1, i - 1) {
// (a[i] += (ll) c[j] * a[i - j] % mod) %= mod;
// }
// (a[i] += c[i]) %= mod;
a[i] = (ll)a[i] * inv[i] % mod;
return;
}
int mid = (l + r) >> 1;
dc(l, mid);
{
poly A(r - l + 1), B(mid - l + 1);
L(i, 0, r - l) if(i <= mid)
A[i] = (ll) a[i] * ((l == 0 ? 1 : 2)) % mod;
L(i, 0, mid - l)
B[i] = a[i + l];
A = Mult(A, B, r - l + 2);
L(j, 0, r - l - 1) if(mid + 1 <= l + j + 1 && l + j + 1 <= r) {
(c[l + j + 1] += A[j]) %= mod;
}
}
{
poly A(r - l + 1), B(mid - l + 1);
L(i, 1, r - l) if(i <= mid)
A[i] = c[i];
L(i, 0, mid - l)
B[i] = a[i + l];
A = Mult(A, B, r - l + 2);
L(j, 0, r - l) if(mid + 1 <= l + j && l + j <= r) {
(a[l + j] += A[j]) %= mod;
}
}
if(l) {
poly A(r - l + 1), B(mid - l + 1);
L(i, 1, r - l) if(i <= mid)
A[i] = a[i];
L(i, 0, mid - l)
B[i] = c[i + l];
A = Mult(A, B, r - l + 2);
L(j, 0, r - l) if(mid + 1 <= l + j && l + j <= r) {
(a[l + j] += A[j]) %= mod;
}
}
dc(mid + 1, r);
}
void dc2(int l, int r) {
if(l == r) {
if(l == 0) return ;
int i = l;
(b[i] += c[i]) %= mod;
b[i] = (ll)b[i] * inv[i] % mod;
return;
}
int mid = (l + r) >> 1;
dc2(l, mid);
{
poly A(r - l + 1), B(mid - l + 1);
L(i, 0, r - l) if(i <= mid)
A[i] = (ll) b[i] * ((l == 0 ? 1 : 2)) % mod;
L(i, 0, mid - l)
B[i] = b[i + l];
A = Mult(A, B, r - l + 2);
L(j, 0, r - l - 1) if(mid + 1 <= l + j + 1 && l + j + 1 <= r) {
(c[l + j + 1] += A[j]) %= mod;
}
}
{
poly A(r - l + 1), B(mid - l + 1);
L(i, 1, r - l)
A[i] = a[i];
L(i, 0, mid - l)
B[i] = c[i + l];
A = Mult(A, B, r - l + 2);
L(j, 0, r - l) if(mid + 1 <= l + j && l + j <= r) {
(b[l + j] += A[j]) %= mod;
}
}
dc2(mid + 1, r);
}
int main() {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
//n=2.5e5;
// n=100;
cin >> n;
init(1 << 19);
Pinit(1 << 19);
a[0] = 1;
dc(0, n);
b[0] = 1;
L(i, 0, n) c[i] = 0;
dc2(0, n);
cout << (ll) b[n] * fac[n] % mod << '\n';
return 0;
}
/*
100
423669705
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 9ms
memory: 26332kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 8ms
memory: 26104kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 6ms
memory: 26108kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 6ms
memory: 26112kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 4ms
memory: 26404kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 8ms
memory: 26172kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 13ms
memory: 26116kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 12ms
memory: 26176kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 5ms
memory: 26376kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 6ms
memory: 26176kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 6ms
memory: 26408kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 10ms
memory: 26216kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 246ms
memory: 27260kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 450ms
memory: 28004kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: 0
Accepted
time: 960ms
memory: 30592kb
input:
131072
output:
842796122
result:
ok 1 number(s): "842796122"
Test #16:
score: 0
Accepted
time: 970ms
memory: 30380kb
input:
131071
output:
981021531
result:
ok 1 number(s): "981021531"
Test #17:
score: 0
Accepted
time: 887ms
memory: 29316kb
input:
131070
output:
480197639
result:
ok 1 number(s): "480197639"
Test #18:
score: 0
Accepted
time: 993ms
memory: 30436kb
input:
131074
output:
383000585
result:
ok 1 number(s): "383000585"
Test #19:
score: 0
Accepted
time: 982ms
memory: 30544kb
input:
131073
output:
316664839
result:
ok 1 number(s): "316664839"
Test #20:
score: 0
Accepted
time: 1226ms
memory: 33856kb
input:
250000
output:
119658643
result:
ok 1 number(s): "119658643"
Test #21:
score: 0
Accepted
time: 1230ms
memory: 33436kb
input:
249999
output:
78110138
result:
ok 1 number(s): "78110138"
Test #22:
score: 0
Accepted
time: 1225ms
memory: 32732kb
input:
249998
output:
297253469
result:
ok 1 number(s): "297253469"
Extra Test:
score: 0
Extra Test Passed