QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#138918 | #1810. Generate the Sequences | foreverlasting | AC ✓ | 5ms | 3772kb | C++20 | 10.8kb | 2023-08-12 14:17:20 | 2023-08-12 14:17:20 |
Judging History
answer
#include <bits/stdc++.h>
using LL = long long;
using ld = long double;
using ull = unsigned int;
using Pair = std::pair<int, int>;
//using unl = __int128;
#define inf 2'000'000'000'000'000'000ll
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
template<int kcz>
struct ModInt {
#define T (*this)
int x;
ModInt() : x(0) {}
ModInt(int y) : x(y >= 0 ? y : y + kcz) {}
ModInt(LL y) : x(y >= 0 ? y % kcz : (kcz - (-y) % kcz) % kcz) {}
inline int inc(const int &v) {
return v >= kcz ? v - kcz : v;
}
inline int dec(const int &v) {
return v < 0 ? v + kcz : v;
}
inline ModInt &operator+=(const ModInt &p) {
x = inc(x + p.x);
return T;
}
inline ModInt &operator-=(const ModInt &p) {
x = dec(x - p.x);
return T;
}
inline ModInt &operator*=(const ModInt &p) {
x = (int) ((LL) x * p.x % kcz);
return T;
}
inline ModInt inverse() const {
int a = x, b = kcz, u = 1, v = 0, t;
while (b > 0)t = a / b, std::swap(a -= t * b, b), std::swap(u -= t * v, v);
return u;
}
inline ModInt &operator/=(const ModInt &p) {
T *= p.inverse();
return T;
}
inline ModInt operator-() const {
return -x;
}
inline friend ModInt operator+(const ModInt &lhs, const ModInt &rhs) {
return ModInt(lhs) += rhs;
}
inline friend ModInt operator-(const ModInt &lhs, const ModInt &rhs) {
return ModInt(lhs) -= rhs;
}
inline friend ModInt operator*(const ModInt &lhs, const ModInt &rhs) {
return ModInt(lhs) *= rhs;
}
inline friend ModInt operator/(const ModInt &lhs, const ModInt &rhs) {
return ModInt(lhs) /= rhs;
}
inline bool operator==(const ModInt &p) const {
return x == p.x;
}
inline bool operator!=(const ModInt &p) const {
return x != p.x;
}
inline ModInt qpow(LL n) const {
ModInt ret(1), mul(x);
while (n > 0) {
if (n & 1)ret *= mul;
mul *= mul, n >>= 1;
}
return ret;
}
inline friend std::ostream &operator<<(std::ostream &os, const ModInt &p) {
return os << p.x;
}
inline friend std::istream &operator>>(std::istream &is, ModInt &a) {
LL t;
is >> t, a = ModInt<kcz>(t);
return is;
}
static int get_mod() {
return kcz;
}
inline bool operator<(const ModInt &A) const {
return x < A.x;
}
inline bool operator>(const ModInt &A) const {
return x > A.x;
}
#undef T
};
const int kcz = 998244353;
using Z = ModInt<kcz>;
namespace NTT {
std::vector<int> rev;
std::vector<Z> roots{0, 1};
inline void dft(std::vector<Z> &a) {
int n = (int) (a.size());
if (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 (roots.size() < n) {
int k = __builtin_ctz(roots.size());
roots.resize(n);
while ((1 << k) < n) {
Z e = Z(3).qpow((kcz - 1) >> (k + 1));
for (int i = 1 << (k - 1); i < (1 << k); i++)
roots[i << 1] = roots[i], roots[i << 1 | 1] = roots[i] * e;
k++;
}
}
for (int k = 1; k < n; k <<= 1) {
for (int i = 0; i < n; i += k << 1) {
for (int j = 0; j < k; j++) {
Z u = a[i + j], v = a[i + j + k] * roots[k + j];
a[i + j] = u + v, a[i + j + k] = u - v;
}
}
}
}
inline void idft(std::vector<Z> &a) {
int n = (int) (a.size());
reverse(a.begin() + 1, a.end()), dft(a);
Z inv = Z(n).inverse();
for (int i = 0; i < n; i++)a[i] = a[i] * inv;
}
}
struct Poly : public std::vector<Z> {
#define T (*this)
using vector<Z>::vector;
inline int deg() const {
return (int) (size());
}
inline Z operator[](const int &idx) const {
if (idx < 0 || idx >= deg())return Z(0);
return at(idx);
}
inline Z &operator[](const int &idx) {
return at(idx);
}
inline Poly &operator^=(const Poly &b) {
if (b.deg() < deg())resize(b.deg());
for (int i = 0, sz = deg(); i < sz; i++)T[i] *= b[i];
return T;
}
inline Poly &operator<<=(const int &k) {
return insert(begin(), k, Z(0)), T;
}
inline Poly operator<<(const int &r) const {
return Poly(T) <<= r;
}
inline Poly operator>>(const int &r) const {
return r >= deg() ? Poly() : Poly(begin() + r, end());
}
inline Poly &operator>>=(const int &r) {
return T = T >> r;
}
inline Poly mod(const int &k) const {
return k < deg() ? Poly(begin(), begin() + k) : T;
}
inline friend Poly operator*(const Z &a, Poly b) {
for (auto &x: b)x *= a;
return b;
}
inline friend Poly operator*(Poly b, const Z &a) {
for (auto &x: b)x *= a;
return b;
}
inline friend Poly operator*(Poly a, Poly b) {
if (a.empty() || b.empty())return {};
int sz = 1, tot = a.deg() + b.deg() - 1;
while (sz < tot)sz <<= 1;
a.resize(sz), b.resize(sz);
NTT::dft(a), NTT::dft(b);
for (int i = 0; i < sz; i++)a[i] *= b[i];
NTT::idft(a), a.resize(tot);
return a;
}
inline Poly &operator*=(const Poly &b) {
return T = T * b;
}
inline friend Poly operator+(const Poly &a, const Poly &b) {
int n = (int) std::max(a.size(), b.size());
Poly c;
c.resize(n);
for (int i = 0, sz = (int) a.size(); i < sz; i++)c[i] = a[i];
for (int i = 0, sz = (int) b.size(); i < sz; i++)c[i] += b[i];
return c;
}
inline Poly &operator+=(const Poly &b) {
return T = T + b;
}
inline friend Poly operator-(const Poly &a, const Poly &b) {
int n = (int) std::max(a.size(), b.size());
Poly c;
c.resize(n);
for (int i = 0, sz = (int) a.size(); i < sz; i++)c[i] = a[i];
for (int i = 0, sz = (int) b.size(); i < sz; i++)c[i] -= b[i];
return c;
}
inline Poly &operator-=(const Poly &b) {
return T = T - b;
}
inline Poly derivation() const {
if (T.empty())return {};
int n = (int) (T.size());
Poly c;
c.resize(n - 1);
for (int i = 0; i < n - 1; i++)c[i] = T[i + 1] * (i + 1);
return c;
}
inline Poly integration() const {
int n = (int) (T.size());
Poly c;
c.resize(n + 1);
for (int i = 0; i < n; i++)c[i + 1] = T[i] * Z(i + 1).inverse();
return c;
}
inline Poly inv(const int &m) const {
Poly c{T[0].inverse()};
int k = 1;
while (k < m)k <<= 1, c = (c * (Poly{2} - T.mod(k) * c)).mod(k);
return c.mod(m);
}
inline Poly log(const int &m) const {
return (derivation() * inv(m)).integration().mod(m);
}
inline Poly exp(const int &m) const {
Poly x{1};
int k = 1;
while (k < m)k <<= 1, x = (x * (Poly{1} - x.log(k) + mod(k))).mod(k);
return x.mod(m);
}
inline Poly pow(const int &k, const int &m) const {
int i = 0;
while (i < T.size() && T[i] == Z(0))i++;
if (i == T.size() || (LL) i * k >= m)return Poly(m);
Z v = T[i];
auto g = (T >> i) * (v.inverse());
return ((g.log(m - i * k) * Z(k)).exp(m - i * k) << (i * k)) * v.qpow(k);
}
inline Poly sqrt(const int &m) const {
Poly x{1};
int k = 1;
while (k < m)k <<= 1, x = (x + (mod(k) * x.inv(k)).mod(k)) * Z(2).inverse();
return x.mod(m);
}
inline Poly rev() const {
return Poly(rbegin(), rend());
}
inline Poly mulT(const Poly &b) const {
return T * b.rev() >> (b.deg() - 1);
}
inline vector <Z> eval(vector <Z> x) const {
if (T.empty())return vector<Z>(x.size(), Z(0));
int n = std::max((int) (x.size()), (int) (T.size()));
vector <Poly> q(4 * n);
vector <Z> ans(x.size());
x.resize(n);
std::function<void(int, int, int)> build = [&](int rt, int l, int r) {
if (l == r) {
q[rt] = {Z(1), -x[l]};
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);
q[rt] = q[rt << 1] * q[rt << 1 | 1];
};
build(1, 0, n - 1);
std::function<void(int, int, int, const Poly &)> work = [&](int rt, int l, int r, const Poly &num) {
if (l == r) {
if (l < (int) (ans.size()))ans[l] = num[0];
return;
}
int mid = (l + r) >> 1;
work(rt << 1, l, mid, num.mulT(q[rt << 1 | 1]).mod(mid - l + 1));
work(rt << 1 | 1, mid + 1, r, num.mulT(q[rt << 1]).mod(r - mid));
};
work(1, 0, n - 1, mulT(q[1].inv(n)));
return ans;
}
#undef T
};
inline Z divAt(Poly F, Poly G, LL k) {
int i;
for (; k; k >>= 1) {
Poly R = G;
// R=G(-x)
int sz = (int) (R.size());
for (i = 1; i < sz; i += 2)R[i] = -R[i];
F *= R, G *= R, sz = (int) (F.size());
for (i = (int) (k & 1); i < sz; i += 2)F[i >> 1] = F[i];
F.resize(i / 2), sz = (int) (G.size());
for (i = 0; i < sz; i += 2)G[i >> 1] = G[i];
G.resize(i / 2);
}
return F.empty() ? Z(0) : (F[0] * (G[0].inverse()));
}
inline Z getAn(Poly F, const Poly &A, const LL &n, const int &k) {
F = Poly{1} - F;
Poly f = A * F;;
return divAt(f.mod(k), F, n);
}
inline void solve(const int &Case) {
int n, m;
std::cin >> n >> m;
std::vector<Z> inv(n + 1);
inv[0] = inv[1] = 1;
for (int i = 2; i <= n; i++)inv[i] = inv[kcz % i] * (kcz - kcz / i);
Poly g(n + 1);
g[0] = 1;
for (int i = 1; i <= n; i++)g[i] = g[i - 1] * (m - 1 - i) * inv[i];
g = g.integration();
g[1] += 1;
g = g.exp(n + 1);
Z ans = g[n];
for (int i = 1; i <= n; i++)ans *= i;
std::cout << ans << '\n';
}
int main() {
// freopen("1.in", "r", stdin);
// freopen("1.out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T = 1;
// std::cin >> T;
for (int i = 1; i <= T; i++)solve(i);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3552kb
input:
2 3
output:
5
result:
ok answer is '5'
Test #2:
score: 0
Accepted
time: 3ms
memory: 3700kb
input:
1024 52689658
output:
654836147
result:
ok answer is '654836147'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
1 2
output:
2
result:
ok answer is '2'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3616kb
input:
1 3
output:
2
result:
ok answer is '2'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
1 100000000
output:
2
result:
ok answer is '2'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3544kb
input:
2 2
output:
4
result:
ok answer is '4'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
2 4
output:
6
result:
ok answer is '6'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3564kb
input:
2 5
output:
7
result:
ok answer is '7'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
2 100000000
output:
100000002
result:
ok answer is '100000002'
Test #10:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
3 2
output:
8
result:
ok answer is '8'
Test #11:
score: 0
Accepted
time: 1ms
memory: 3548kb
input:
3 3
output:
14
result:
ok answer is '14'
Test #12:
score: 0
Accepted
time: 1ms
memory: 3548kb
input:
3 4
output:
22
result:
ok answer is '22'
Test #13:
score: 0
Accepted
time: 1ms
memory: 3580kb
input:
3 5
output:
32
result:
ok answer is '32'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3540kb
input:
3 100000000
output:
446563791
result:
ok answer is '446563791'
Test #15:
score: 0
Accepted
time: 2ms
memory: 3756kb
input:
3000 2
output:
21292722
result:
ok answer is '21292722'
Test #16:
score: 0
Accepted
time: 5ms
memory: 3712kb
input:
3000 3
output:
172222927
result:
ok answer is '172222927'
Test #17:
score: 0
Accepted
time: 5ms
memory: 3752kb
input:
3000 100000000
output:
736503947
result:
ok answer is '736503947'
Test #18:
score: 0
Accepted
time: 5ms
memory: 3660kb
input:
2522 61077387
output:
857454425
result:
ok answer is '857454425'
Test #19:
score: 0
Accepted
time: 1ms
memory: 3628kb
input:
426 7215704
output:
799491736
result:
ok answer is '799491736'
Test #20:
score: 0
Accepted
time: 2ms
memory: 3676kb
input:
772 72289915
output:
848141383
result:
ok answer is '848141383'
Test #21:
score: 0
Accepted
time: 3ms
memory: 3652kb
input:
1447 83321470
output:
160422285
result:
ok answer is '160422285'
Test #22:
score: 0
Accepted
time: 5ms
memory: 3756kb
input:
2497 64405193
output:
355300540
result:
ok answer is '355300540'
Test #23:
score: 0
Accepted
time: 2ms
memory: 3696kb
input:
775 9385367
output:
470172346
result:
ok answer is '470172346'
Test #24:
score: 0
Accepted
time: 2ms
memory: 3576kb
input:
982 72596758
output:
7144187
result:
ok answer is '7144187'
Test #25:
score: 0
Accepted
time: 2ms
memory: 3640kb
input:
417 26177178
output:
776374896
result:
ok answer is '776374896'
Test #26:
score: 0
Accepted
time: 3ms
memory: 3772kb
input:
1932 19858856
output:
285834553
result:
ok answer is '285834553'
Test #27:
score: 0
Accepted
time: 5ms
memory: 3772kb
input:
2728 23009122
output:
433516287
result:
ok answer is '433516287'
Test #28:
score: 0
Accepted
time: 3ms
memory: 3768kb
input:
1857 22578508
output:
243488639
result:
ok answer is '243488639'
Test #29:
score: 0
Accepted
time: 2ms
memory: 3664kb
input:
2918 69623276
output:
546299707
result:
ok answer is '546299707'
Test #30:
score: 0
Accepted
time: 3ms
memory: 3704kb
input:
1679 21332149
output:
217000656
result:
ok answer is '217000656'
Test #31:
score: 0
Accepted
time: 1ms
memory: 3768kb
input:
1340 6251797
output:
267221018
result:
ok answer is '267221018'
Test #32:
score: 0
Accepted
time: 2ms
memory: 3624kb
input:
868 64770398
output:
652067665
result:
ok answer is '652067665'