QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#108105 | #6411. Classical FFT Problem | whatever# | WA | 2ms | 3408kb | C++17 | 8.4kb | 2023-05-23 16:26:16 | 2023-05-23 16:26:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define rep(i, a, b) for (int i = (a), I = (b); i <= I; ++i)
#define per(i, a, b) for (int i = (a), I = (b); i >= I; --i)
using i64 = long long;
using pii = pair<int, int>;
template<typename T> void up(T &x, T y) { if (x < y) x = y; }
template<typename T> void down(T &x, T y) { if (x > y) x = y; }
const int P = 998244353;
void add(int &x, int y) { (x += y) >= P && (x -= P); }
int Add(int x, int y) { return (x += y) >= P ? (x - P) : x; }
void sub(int &x, int y) { (x -= y) < 0 && (x += P); }
int Sub(int x, int y) { return (x -= y) < 0 ? (x + P) : x; }
void mul(int &x, int y) { x = 1ll * x * y % P; }
int Mul(int x, int y) { return 1ll * x * y % P; }
std::vector<int> rev, roots{0, 1};
int power(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % P)
if (b & 1)
res = 1ll * res * a % P;
return res;
}
void dft(std::vector<int> &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) {
int e = power(3, (P - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); ++i) {
roots[2 * i] = roots[i];
roots[2 * i + 1] = 1ll * roots[i] * e % P;
}
++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) {
int u = a[i + j];
int v = 1ll * a[i + j + k] * roots[k + j] % P;
int x = u + v;
if (x >= P)
x -= P;
a[i + j] = x;
x = u - v;
if (x < 0)
x += P;
a[i + j + k] = x;
}
}
}
}
void idft(std::vector<int> &a) {
int n = a.size();
std::reverse(a.begin() + 1, a.end());
dft(a);
int inv = power(n, P - 2);
for (int i = 0; i < n; ++i)
a[i] = 1ll * a[i] * inv % P;
}
struct poly {
std::vector<int> a;
poly() {}
poly(int a0) {
if (a0)
a = {a0};
}
poly(const std::vector<int> &a1) : a(a1) {
while (!a.empty() && !a.back())
a.pop_back();
}
int size() const {
return a.size();
}
int operator[](int idx) const {
if (idx < 0 || idx >= size())
return 0;
return a[idx];
}
poly mulxk(int k) const {
auto b = a;
b.insert(b.begin(), k, 0);
return poly(b);
}
poly modxk(int k) const {
k = std::min(k, size());
return poly(std::vector<int>(a.begin(), a.begin() + k));
}
poly divxk(int k) const {
if (size() <= k)
return poly();
return poly(std::vector<int>(a.begin() + k, a.end()));
}
friend poly operator+(const poly a, const poly &b) {
std::vector<int> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); ++i) {
res[i] = a[i] + b[i];
if (res[i] >= P)
res[i] -= P;
}
return poly(res);
}
friend poly operator-(const poly a, const poly &b) {
std::vector<int> res(std::max(a.size(), b.size()));
for (int i = 0; i < int(res.size()); ++i) {
res[i] = a[i] - b[i];
if (res[i] < 0)
res[i] += P;
}
return poly(res);
}
friend poly operator*(poly a, poly b) {
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] = 1ll * a[i] * b[i] % P;
idft(a.a);
return poly(a.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<int> res(size() - 1);
for (int i = 0; i < size() - 1; ++i)
res[i] = 1ll * (i + 1) * a[i + 1] % P;
return poly(res);
}
poly integr() const {
if (a.empty())
return poly();
std::vector<int> res(size() + 1);
for (int i = 0; i < size(); ++i)
res[i + 1] = 1ll * a[i] * power(i + 1, P - 2) % P;
return poly(res);
}
poly inv(int m) const {
poly x(power(a[0], P - 2));
int k = 1;
while (k < m) {
k *= 2;
x = (x * (2 - modxk(k) * x)).modxk(k);
}
return x.modxk(m);
}
poly log(int m) const {
return (deriv() * inv(m)).integr().modxk(m);
}
poly exp(int m) const {
poly x(1);
int k = 1;
while (k < m) {
k *= 2;
x = (x * (1 - x.log(k) + modxk(k))).modxk(k);
}
return x.modxk(m);
}
poly sqrt(int m) const {
poly x(1);
int k = 1;
while (k < m) {
k *= 2;
x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((P + 1) / 2);
}
return x.modxk(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<int> eval(std::vector<int> x) const {
if (size() == 0)
return std::vector<int>(x.size(), 0);
const int n = std::max(int(x.size()), size());
std::vector<poly> q(4 * n);
std::vector<int> 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] = std::vector<int>{1, (P - x[l]) % P};
} 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]).modxk(m - l));
work(2 * p + 1, m, r, num.mulT(q[2 * p]).modxk(r - m));
}
};
work(1, 0, n, mulT(q[1].inv(n)));
return ans;
}
};
const int N = 140000;
int n, a[N], b[N], k;
int fac[N], ifac[N];
void init(int n) {
fac[0] = 1;
rep(i, 1, n) fac[i] = Mul(fac[i - 1], i);
ifac[n] = power(fac[n], P - 2);
per(i, n, 1) ifac[i - 1] = Mul(ifac[i], i);
}
int binom(int n, int m) {
if (n < 0 || m < 0 || n < m) return 0;
return Mul(fac[n], Mul(ifac[m], ifac[n - m]));
}
poly solve(int l, int r, int *a) {
if (l == r) return poly({P - a[l], 1});
int mid = (l + r) >> 1;
return solve(l, mid, a) * solve(mid + 1, r, a);
}
int solve(int *a) {
int ans = 0;
poly f = solve(1, k, a);
vector<int> x(a[k + 1] + 1);
iota(x.begin(), x.end(), 0);
poly g = f.eval(x);
rep(j, 0, a[k + 1]) {
int coef = binom(a[k + 1], j);
if (j & 1) coef = Sub(0, coef);
add(ans, Mul(coef, g[j]));
}
return ans;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
init(n);
rep(i, 1, n) cin >> a[i];
reverse(a + 1, a + n + 1);
for (int i = 1, j = n; i <= n; ++i) {
while (j && a[j] < i) --j;
b[i] = j;
}
k = 0;
while (k < n && a[k + 1] >= k + 1) ++k;
cout << k << " ";
int ans = Add(solve(a), solve(b));
sub(ans, fac[k]);
cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3408kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 3384kb
input:
1 1
output:
1 998244350
result:
wrong answer 2nd numbers differ - expected: '1', found: '998244350'