QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#472332 | #8721. 括号序列 | Andyqian7 | TL | 1112ms | 24236kb | C++17 | 9.8kb | 2024-07-11 15:42:30 | 2024-07-11 15:42:31 |
Judging History
answer
#include <vector>
#include <iostream>
#include <cassert>
#include <functional>
using namespace std;
constexpr int P = 998244353;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x)
{
if (x < 0)
{
x += P;
}
if (x >= P)
{
x -= P;
}
return x;
}
template <class T>
T power(T a, int b)
{
T res = 1;
for (; b; b /= 2, a *= a)
{
if (b % 2)
{
res *= a;
}
}
return res;
}
int fac[524300], ifac[524300], inv[524300];
struct Z
{
int x;
Z(int x = 0) : x(norm(x)) {}
int val() const
{
return x;
}
Z operator-() const
{
return Z(norm(P - x));
}
Z inv() const
{
assert(x != 0);
return ::inv[this->val()];
}
Z &operator*=(const Z &rhs)
{
x = i64(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs)
{
x = norm(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs)
{
x = norm(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs)
{
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs)
{
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs)
{
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs)
{
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs)
{
Z res = lhs;
res /= rhs;
return res;
}
friend std::istream &operator>>(std::istream &is, Z &a)
{
i64 v;
is >> v;
a = Z(v);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Z &a)
{
return os << a.val();
}
};
std::vector<int> rev;
std::vector<Z> roots{0, 1};
void dft(std::vector<Z> &a)
{
int n = a.size();
if (int(rev.size()) != n)
{
int k = __builtin_ctz(n) - 1;
rev.resize(n);
for (int i = 0; i < n; i++)
{
rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
}
}
for (int i = 0; i < n; i++)
{
if (rev[i] < i)
{
std::swap(a[i], a[rev[i]]);
}
}
if (int(roots.size()) < n)
{
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n)
{
Z e = power(Z(3), (P - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++)
{
roots[2 * i] = roots[i];
roots[2 * i + 1] = roots[i] * e;
}
k++;
}
}
for (int k = 1; k < n; k *= 2)
{
for (int i = 0; i < n; i += 2 * k)
{
for (int j = 0; j < k; j++)
{
Z u = a[i + j];
Z v = a[i + j + k] * roots[k + j];
a[i + j] = u + v;
a[i + j + k] = u - v;
}
}
}
}
void idft(std::vector<Z> &a)
{
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
Z inv = (1 - P) / n;
for (int i = 0; i < n; i++)
{
a[i] *= inv;
}
}
struct Poly
{
std::vector<Z> a;
Poly() {}
Poly(const std::vector<Z> &a) : a(a) {}
Poly(const std::initializer_list<Z> &a) : a(a) {}
int size() const
{
return a.size();
}
void resize(int n)
{
a.resize(n);
}
Z operator[](int idx) const
{
if (idx < size())
{
return a[idx];
}
else
{
return 0;
}
}
Z &operator[](int idx)
{
return a[idx];
}
Poly mulxk(int k) const
{
auto b = a;
b.insert(b.begin(), k, 0);
return Poly(b);
}
Poly Pxk(int k) const
{
k = std::min(k, size());
return Poly(std::vector<Z>(a.begin(), a.begin() + k));
}
Poly divxk(int k) const
{
if (size() <= k)
{
return Poly();
}
return Poly(std::vector<Z>(a.begin() + k, a.end()));
}
friend Poly operator+(const Poly &a, const Poly &b)
{
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++)
{
res[i] = a[i] + b[i];
}
return Poly(res);
}
friend Poly operator-(const Poly &a, const Poly &b)
{
std::vector<Z> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); i++)
{
res[i] = a[i] - b[i];
}
return Poly(res);
}
friend Poly operator*(Poly a, Poly b)
{
if (a.size() == 0 || b.size() == 0)
{
return Poly();
}
int sz = 1, tot = a.size() + b.size() - 1;
while (sz < tot)
{
sz *= 2;
}
a.a.resize(sz);
b.a.resize(sz);
dft(a.a);
dft(b.a);
for (int i = 0; i < sz; ++i)
{
a.a[i] = a[i] * b[i];
}
idft(a.a);
a.resize(tot);
return a;
}
friend Poly operator*(Z a, Poly b)
{
for (int i = 0; i < int(b.size()); i++)
{
b[i] *= a;
}
return b;
}
friend Poly operator*(Poly a, Z b)
{
for (int i = 0; i < int(a.size()); i++)
{
a[i] *= b;
}
return a;
}
Poly &operator+=(Poly b)
{
return (*this) = (*this) + b;
}
Poly &operator-=(Poly b)
{
return (*this) = (*this) - b;
}
Poly &operator*=(Poly b)
{
return (*this) = (*this) * b;
}
Poly deriv() const
{
if (a.empty())
{
return Poly();
}
std::vector<Z> res(size() - 1);
for (int i = 0; i < size() - 1; ++i)
{
res[i] = (i + 1) * a[i + 1];
}
return Poly(res);
}
Poly integr() const
{
std::vector<Z> res(size() + 1);
for (int i = 0; i < size(); ++i)
{
res[i + 1] = a[i] / (i + 1);
}
return Poly(res);
}
Poly inv(int m) const
{
Poly x{a[0].inv()};
int k = 1;
while (k < m)
{
k *= 2;
x = (x * (Poly{2} - Pxk(k) * x)).Pxk(k);
}
return x.Pxk(m);
}
Poly log(int m) const
{
return (deriv() * inv(m)).integr().Pxk(m);
}
Poly exp(int m) const
{
Poly x{1};
int k = 1;
while (k < m)
{
k *= 2;
x = (x * (Poly{1} - x.log(k) + Pxk(k))).Pxk(k);
}
return x.Pxk(m);
}
Poly pow(int k, int m) const
{
int i = 0;
while (i < size() && a[i].val() == 0)
{
i++;
}
if (i == size() || 1LL * i * k >= m)
{
return Poly(std::vector<Z>(m));
}
Z v = a[i];
auto f = divxk(i) * v.inv();
return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k);
}
Poly sqrt(int m) const
{
Poly x{1};
int k = 1;
while (k < m)
{
k *= 2;
x = (x + (Pxk(k) * x.inv(k)).Pxk(k)) * ((P + 1) / 2);
}
return x.Pxk(m);
}
Poly mulT(Poly b) const
{
if (b.size() == 0)
{
return Poly();
}
int n = b.size();
std::reverse(b.a.begin(), b.a.end());
return ((*this) * b).divxk(n - 1);
}
std::vector<Z> eval(std::vector<Z> x) const
{
if (size() == 0)
{
return std::vector<Z>(x.size(), 0);
}
const int n = std::max(int(x.size()), size());
std::vector<Poly> q(4 * n);
std::vector<Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int p, int l, int r)
{
if (r - l == 1)
{
q[p] = Poly{1, -x[l]};
}
else
{
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
q[p] = q[2 * p] * q[2 * p + 1];
}
};
build(1, 0, n);
std::function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num)
{
if (r - l == 1)
{
if (l < int(ans.size()))
{
ans[l] = num[0];
}
}
else
{
int m = (l + r) / 2;
work(2 * p, l, m, num.mulT(q[2 * p + 1]).Pxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).Pxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};
int main()
{
fac[0] = ifac[0] = fac[1] = ifac[1] = inv[0] = inv[1] = 1;
int n;
cin >> n;
for (int i = 2; i < 5e5; i++)
{
inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
fac[i] = 1ll * fac[i - 1] * i % P;
ifac[i] = 1ll * ifac[i - 1] * inv[i] % P;
}
Poly g{1};
int k = 1;
Poly e = {1}, x{0, 1};
while (k < n)
{
k *= 2;
Poly H1i = ((g * g).Pxk(k) * (g + e)).Pxk(k);
Poly H = (g.deriv() * H1i.inv(k)).integr().Pxk(k) - x;
g = (g - H * H1i).Pxk(k);
}
Poly f = (e - g.integr()).inv(n + 1);
cout << f[n] * fac[n] << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 9504kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 5ms
memory: 9568kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 4ms
memory: 9848kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 9632kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 4ms
memory: 9860kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 0ms
memory: 9648kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 0ms
memory: 9560kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 0ms
memory: 9564kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 5ms
memory: 9572kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 0ms
memory: 9516kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 5ms
memory: 9580kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 14ms
memory: 9788kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 450ms
memory: 14116kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 511ms
memory: 16784kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: 0
Accepted
time: 1112ms
memory: 24236kb
input:
131072
output:
842796122
result:
ok 1 number(s): "842796122"
Test #16:
score: 0
Accepted
time: 940ms
memory: 20232kb
input:
131071
output:
981021531
result:
ok 1 number(s): "981021531"
Test #17:
score: 0
Accepted
time: 950ms
memory: 20236kb
input:
131070
output:
480197639
result:
ok 1 number(s): "480197639"
Test #18:
score: -100
Time Limit Exceeded
input:
131074
output:
383000585