QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#404189#6323. Range NEQcomeintocalm#AC ✓602ms37396kbC++2010.1kb2024-05-03 15:24:092024-05-03 15:24:10

Judging History

你现在查看的是最新测评结果

  • [2024-05-03 15:24:10]
  • 评测
  • 测评结果:AC
  • 用时:602ms
  • 内存:37396kb
  • [2024-05-03 15:24:09]
  • 提交

answer

#include<bits/stdc++.h>
const int p = 998244353;
typedef long long LL;
using namespace std;
const int mod = p;

template<int mod>
struct ModInt {
#define T (*this)
    int x;
    ModInt() : x(0) {}
    ModInt(int y) : x(y >= 0 ? y : y + mod) {}
    ModInt(LL y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    inline int inc(const int &v) {
        return v >= mod ? v - mod : v;
    }
    inline int dec(const int &v) {
        return v < 0 ? v + mod : 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 % mod);
        return T;
    }
    inline ModInt inverse() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while (b > 0)t = a / b, swap(a -= t * b, b), 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;
    }
    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 ostream &operator<<(ostream &os, const ModInt &p) {
        return os << p.x;
    }
    inline friend istream &operator>>(istream &is, ModInt &a) {
        LL t;
        is >> t, a = ModInt<mod>(t);
        return is;
    }
    static int get_mod() {
        return mod;
    }
#undef T
};


using Z = ModInt<mod>;
namespace NTT {
    vector<int> rev;
    vector<Z> roots{0, 1};
    inline void dft(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)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((mod - 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(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 vector<Z> {
#define T (*this)
    using vector<Z>::vector;
    inline int deg() const {
        return (int)(size());
    }
    inline bool operator <(const Poly &rhs) const{
      return deg() > rhs.deg();
    }
    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)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 friend Poly operator-(const Poly &a, const Poly &b) {
        int n = (int)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 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 = 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
};


const int N = 1e6+7;
Z ifac[N], fac[N];

Z binom(int n, int m) {
    if(!m) return 1;
    if(!n || n < m) return 0;
    return fac[n] * ifac[m] * ifac[n - m];
}
Z iep(int x) {
    if(x & 1) return Z(998244352);
    return Z(1);
}

int main() {
    int n, m;
    cin >> n >> m;
    int up = (n * m);
    fac[0] = ifac[0] = Z(1);
    const int M = 1e6;
    for(int i = 1; i <= M; i++) {
        fac[i] = fac[i - 1] * i;
    }
    ifac[M] = fac[M].inverse();
    for(int i = M - 1; i >= 1; i--) {
        ifac[i] = ifac[i + 1] * (i + 1);
    }

    //cout << binom(6, 3).x;

    Poly F;
    for(int i = 0; i <= m; i++) {
        Z res = binom(m, i) * iep(i) * ifac[m - i] * fac[m];
        F.push_back(res);
    }

    //Poly G = F.pow(n,  1000007);
    //Poly G = F.log(1000007);
    //for(int i = 0; i <= up; i++) G[i] *= Z(n);
    //G = G.exp(1000007);

    priority_queue<Poly> Q;
    for(int i = 1; i <= n; i++) Q.push(F);
    while(Q.size() > 1) {
      Poly f1 = Q.top(); Q.pop();
      Poly f2 = Q.top(); Q.pop();
      Poly f3 = f1*f2;
      f3.mod(up+2);
      Q.push(f3);
    }
    Poly G = Q.top();
    Z ans(0);
    for(int i = 0; i <= up; i++) {
        ans += fac[up - i] * G[i];
    }

    cout << ans.x << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 9ms
memory: 11296kb

input:

2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 8ms
memory: 11292kb

input:

5 1

output:

44

result:

ok 1 number(s): "44"

Test #3:

score: 0
Accepted
time: 15ms
memory: 11568kb

input:

167 91

output:

284830080

result:

ok 1 number(s): "284830080"

Test #4:

score: 0
Accepted
time: 8ms
memory: 11280kb

input:

2 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 13ms
memory: 11296kb

input:

2 3

output:

36

result:

ok 1 number(s): "36"

Test #6:

score: 0
Accepted
time: 8ms
memory: 11556kb

input:

2 4

output:

576

result:

ok 1 number(s): "576"

Test #7:

score: 0
Accepted
time: 8ms
memory: 11288kb

input:

3 1

output:

2

result:

ok 1 number(s): "2"

Test #8:

score: 0
Accepted
time: 8ms
memory: 11488kb

input:

3 2

output:

80

result:

ok 1 number(s): "80"

Test #9:

score: 0
Accepted
time: 9ms
memory: 11292kb

input:

3 3

output:

12096

result:

ok 1 number(s): "12096"

Test #10:

score: 0
Accepted
time: 8ms
memory: 11356kb

input:

3 4

output:

4783104

result:

ok 1 number(s): "4783104"

Test #11:

score: 0
Accepted
time: 8ms
memory: 11304kb

input:

4 1

output:

9

result:

ok 1 number(s): "9"

Test #12:

score: 0
Accepted
time: 8ms
memory: 11348kb

input:

4 2

output:

4752

result:

ok 1 number(s): "4752"

Test #13:

score: 0
Accepted
time: 12ms
memory: 11384kb

input:

4 3

output:

17927568

result:

ok 1 number(s): "17927568"

Test #14:

score: 0
Accepted
time: 6ms
memory: 11292kb

input:

4 4

output:

776703752

result:

ok 1 number(s): "776703752"

Test #15:

score: 0
Accepted
time: 8ms
memory: 11408kb

input:

5 2

output:

440192

result:

ok 1 number(s): "440192"

Test #16:

score: 0
Accepted
time: 6ms
memory: 11284kb

input:

5 3

output:

189125068

result:

ok 1 number(s): "189125068"

Test #17:

score: 0
Accepted
time: 12ms
memory: 11300kb

input:

5 4

output:

975434093

result:

ok 1 number(s): "975434093"

Test #18:

score: 0
Accepted
time: 594ms
memory: 37396kb

input:

1000 1000

output:

720037464

result:

ok 1 number(s): "720037464"

Test #19:

score: 0
Accepted
time: 9ms
memory: 11620kb

input:

72 42

output:

638177567

result:

ok 1 number(s): "638177567"

Test #20:

score: 0
Accepted
time: 6ms
memory: 11516kb

input:

15 19

output:

663050288

result:

ok 1 number(s): "663050288"

Test #21:

score: 0
Accepted
time: 14ms
memory: 11460kb

input:

68 89

output:

94365047

result:

ok 1 number(s): "94365047"

Test #22:

score: 0
Accepted
time: 10ms
memory: 11436kb

input:

92 37

output:

652605307

result:

ok 1 number(s): "652605307"

Test #23:

score: 0
Accepted
time: 14ms
memory: 11556kb

input:

61 87

output:

498277867

result:

ok 1 number(s): "498277867"

Test #24:

score: 0
Accepted
time: 13ms
memory: 11448kb

input:

81 40

output:

133095344

result:

ok 1 number(s): "133095344"

Test #25:

score: 0
Accepted
time: 8ms
memory: 11368kb

input:

7 91

output:

524164813

result:

ok 1 number(s): "524164813"

Test #26:

score: 0
Accepted
time: 8ms
memory: 11336kb

input:

31 18

output:

361233485

result:

ok 1 number(s): "361233485"

Test #27:

score: 0
Accepted
time: 10ms
memory: 11460kb

input:

74 54

output:

500686087

result:

ok 1 number(s): "500686087"

Test #28:

score: 0
Accepted
time: 8ms
memory: 11560kb

input:

32 2

output:

586504335

result:

ok 1 number(s): "586504335"

Test #29:

score: 0
Accepted
time: 354ms
memory: 23212kb

input:

656 718

output:

346764298

result:

ok 1 number(s): "346764298"

Test #30:

score: 0
Accepted
time: 119ms
memory: 16788kb

input:

254 689

output:

358078813

result:

ok 1 number(s): "358078813"

Test #31:

score: 0
Accepted
time: 396ms
memory: 23912kb

input:

713 674

output:

914437613

result:

ok 1 number(s): "914437613"

Test #32:

score: 0
Accepted
time: 60ms
memory: 13952kb

input:

136 698

output:

56687290

result:

ok 1 number(s): "56687290"

Test #33:

score: 0
Accepted
time: 91ms
memory: 16604kb

input:

369 401

output:

312325811

result:

ok 1 number(s): "312325811"

Test #34:

score: 0
Accepted
time: 40ms
memory: 12448kb

input:

280 204

output:

280012063

result:

ok 1 number(s): "280012063"

Test #35:

score: 0
Accepted
time: 121ms
memory: 18160kb

input:

904 225

output:

162909174

result:

ok 1 number(s): "162909174"

Test #36:

score: 0
Accepted
time: 520ms
memory: 35572kb

input:

855 928

output:

39885159

result:

ok 1 number(s): "39885159"

Test #37:

score: 0
Accepted
time: 126ms
memory: 17836kb

input:

503 365

output:

745115888

result:

ok 1 number(s): "745115888"

Test #38:

score: 0
Accepted
time: 422ms
memory: 34900kb

input:

646 996

output:

610925577

result:

ok 1 number(s): "610925577"

Test #39:

score: 0
Accepted
time: 592ms
memory: 36640kb

input:

990 918

output:

203469632

result:

ok 1 number(s): "203469632"

Test #40:

score: 0
Accepted
time: 594ms
memory: 36736kb

input:

961 949

output:

169566857

result:

ok 1 number(s): "169566857"

Test #41:

score: 0
Accepted
time: 583ms
memory: 36620kb

input:

946 932

output:

352423195

result:

ok 1 number(s): "352423195"

Test #42:

score: 0
Accepted
time: 561ms
memory: 36740kb

input:

903 981

output:

196309824

result:

ok 1 number(s): "196309824"

Test #43:

score: 0
Accepted
time: 554ms
memory: 36908kb

input:

916 988

output:

487208972

result:

ok 1 number(s): "487208972"

Test #44:

score: 0
Accepted
time: 594ms
memory: 36908kb

input:

982 982

output:

387421488

result:

ok 1 number(s): "387421488"

Test #45:

score: 0
Accepted
time: 568ms
memory: 36328kb

input:

955 911

output:

955637031

result:

ok 1 number(s): "955637031"

Test #46:

score: 0
Accepted
time: 558ms
memory: 36592kb

input:

906 999

output:

798469943

result:

ok 1 number(s): "798469943"

Test #47:

score: 0
Accepted
time: 602ms
memory: 36840kb

input:

982 975

output:

193506289

result:

ok 1 number(s): "193506289"

Test #48:

score: 0
Accepted
time: 564ms
memory: 36568kb

input:

921 991

output:

431202149

result:

ok 1 number(s): "431202149"