QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#397568#8010. Hierarchies of JudgeschhokmahAC ✓2892ms61764kbC++236.7kb2024-04-24 13:10:012024-04-24 13:10:01

Judging History

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

  • [2024-04-24 13:10:01]
  • 评测
  • 测评结果:AC
  • 用时:2892ms
  • 内存:61764kb
  • [2024-04-24 13:10:01]
  • 提交

answer

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
void io() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 4e5 + 5;
const ll INF = 0x3f3f3f3f3f3f3f;
const ll MOD = 998244353;
const int inv2 = (MOD + 1) >> 1;
typedef vector<int> Poly;
int inc(int a, int b) { return (a += b) >= MOD ? a - MOD : a; }
int fpow(int a, int b) {
    int t = 1;
    while (b) {
        if (b & 1) {
            t = 1LL * t * a % MOD;
        }
        a = 1LL * a * a % MOD;
        b >>= 1;
    }
    return t;
}
int W[MAXN * 4], inv[MAXN * 4];
// 预处理原根,以及线性求逆元
void prework(int n) {
    for (int i = 1; i < n; i <<= 1) {
        for (int j = W[i] = 1, Wn = fpow(3, (MOD - 1) / i >> 1); j < i; j++) {
            W[i + j] = 1LL * W[i + j - 1] * Wn % MOD;
        }
    }
    inv[1] = 1;
    for (int i = 2; i <= n; i++) {
        inv[i] = 1LL * (MOD - MOD / i) * inv[MOD % i] % MOD;
    }
}
void fft(Poly &a, int n, int opt) {
    a.resize(n);
    static int rev[MAXN * 4];
    for (int i = 1; i < n; i++) {
        if ((rev[i] = rev[i >> 1] >> 1 | (i & 1 ? n >> 1 : 0)) > i) {
            swap(a[i], a[rev[i]]);
        }
    }
    for (int q = 1; q < n; q <<= 1) {
        for (int p = 0; p < n; p += q << 1) {
            for (int i = 0, t; i < q; i++) {
                t = 1LL * W[q + i] * a[p + q + i] % MOD;
                a[p + q + i] = inc(a[p + i], MOD - t);
                a[p + i] = inc(a[p + i], t);
            }
        }
    }
    if (opt) {
        return;
    }
    for (int i = 0, inv = fpow(n, MOD - 2); i < n; i++) {
        a[i] = 1LL * a[i] * inv % MOD;
    }
    reverse(a.begin() + 1, a.end());
}
Poly poly_inv(Poly A) {
    Poly B(1, fpow(A[0], MOD - 2)), C(2);
    for (int L = 1; L < A.size(); L <<= 1) {
        (C = A).resize(L * 2);
        fft(B, L * 4, 1), fft(C, L * 4, 1);
        for (int i = 0; i < L * 4; i++) {
            B[i] =
                (2 * B[i] - 1LL * B[i] * B[i] % MOD * C[i] % MOD + MOD) % MOD;
        }
        fft(B, L * 4, 0);
        B.resize(L * 2);
    }
    return B.resize(A.size()), B;
}
int getsz(int n) {
    int x = 1;
    while (x < n) {
        x <<= 1;
    }
    return x;
}
void fix(Poly &A) {
    int x = A.size();
    while (x > 1 && !A[x - 1]) {
        x--;
    }
    A.resize(x);
}
Poly operator+(Poly A, Poly B) {
    if (A.size() < B.size()) {
        swap(A, B);
    }
    for (int i = 0; i < B.size(); i++) {
        A[i] = inc(A[i], B[i]);
    }
    return fix(A), A;
}
Poly operator-(Poly A, Poly B) {
    A.resize(max(A.size(), B.size()));
    for (int i = 0; i < B.size(); i++) {
        A[i] = inc(A[i], MOD - B[i]);
    }
    return fix(A), A;
}
Poly operator*(int k, Poly A) {
    for (int i = 0; i < A.size(); i++) {
        A[i] = 1LL * k * A[i] % MOD;
    }
    return A;
}
Poly operator*(Poly A, Poly B) {
    int L = getsz(A.size() + B.size() - 1);
    fft(A, L, 1), fft(B, L, 1);
    for (int i = 0; i < L; i++) {
        A[i] = 1LL * A[i] * B[i] % MOD;
    }
    return fft(A, L, 0), fix(A), A;
}
// 需要有 A.size() >= B.size()
Poly operator/(Poly A, Poly B) {
    int n = A.size() - B.size() + 1;
    reverse(A.begin(), A.end());
    A.resize(n);
    reverse(B.begin(), B.end());
    B.resize(n);
    return A = A * poly_inv(B), A.resize(n), reverse(A.begin(), A.end()),
           fix(A), A;
}
Poly operator>>(Poly A, int k) {
    A.resize(A.size() + 1);
    for (int i = A.size() - 1; i > 0; i--) {
        A[i] = A[i - 1];
    }
    A[0] = 0;
    return A;
}
Poly operator%(Poly A, Poly B) {
    if (A.size() < B.size()) {
        return A;
    }
    return A - A / B * B;
}
Poly poly_deri(Poly A) {
    for (int i = 0; i < A.size() - 1; i++) {
        A[i] = 1LL * (i + 1) * A[i + 1] % MOD;
    }
    return A.resize(A.size() - 1), A;
}
Poly poly_int(Poly A) {
    for (int i = A.size() - 1; i; i--) {
        A[i] = 1LL * A[i - 1] * inv[i] % MOD;
    }
    return A[0] = 0, A;
}
Poly poly_sqrt(Poly A) {
    Poly B(1, 1), iB, C(2);
    for (int L = 1; L < A.size(); L <<= 1) {
        (C = A).resize(L * 2);
        B.resize(L * 2);
        iB = poly_inv(B);
        fft(B, L * 4, 1), fft(iB, L * 4, 1), fft(C, L * 4, 1);
        for (int i = 0; i < L * 4; i++) {
            B[i] = (1LL * B[i] * B[i] + C[i]) % MOD * iB[i] % MOD * inv2 % MOD;
        }
        fft(B, L * 4, 0);
        B.resize(L * 2);
    }
    return B.resize(A.size()), B;
}
Poly poly_ln(Poly A) {
    Poly B = poly_deri(A) * poly_inv(A);
    return B.resize(A.size()), poly_int(B);
}
Poly poly_exp(Poly A) {
    Poly B(1, 1), C;
    for (int L = 1; L < A.size(); L <<= 1) {
        B.resize(L * 2);
        C = A + Poly(1, 1) - poly_ln(B), C.resize(L * 2);
        B = B * C;
    }
    return B.resize(A.size()), B;
}
Poly poly_pow(Poly A, int k) { return poly_exp(k * poly_ln(A)); }

pair<Poly, Poly> newton(int n, Poly B, Poly W) {
    Poly WW, BW, xeB, xeBW;
    Poly xeBW_B, xeBW_BWW;
    Poly xeBW_W, xeBW_WW, xeBW_WWW;
    Poly f0, g0, fB, fW, gB, gW;
    Poly x, y;
    Poly det_inv;
    for (int L = 1; L <= n; L <<= 1) {
        WW = W * W;
        BW = B * W;
        B.resize(2 * L);
        xeB = poly_exp(B) >> 1;
        fix(B);
        xeBW = poly_exp(BW) >> 1;
        xeBW_B = xeBW * B;
        xeBW_B.resize(2 * L);
        xeBW_BWW = xeBW_B * WW;
        xeBW_BWW.resize(2 * L);
        xeBW_W = xeBW * W;
        xeBW_W.resize(2 * L);
        xeBW_WW = xeBW_W * W;
        xeBW_WW.resize(2 * L);
        xeBW_WWW = xeBW_WW * W;
        xeBW_WWW.resize(2 * L);
        f0 = B - BW - xeB + xeBW_WW;
        g0 = W - WW - xeB + xeBW;
        fB = xeBW_WWW - W - xeB;
        fB[0] = (fB[0] + 1) % MOD;
        fW = xeBW_BWW + 2 * xeBW_W - B;
        gB = xeBW_W - xeB;
        gW = xeBW_B - 2 * W;
        gW[0] = (gW[0] + 1) % MOD;

        x = B * fB + W * fW - f0;
        x.resize(2 * L);
        y = B * gB + W * gW - g0;
        y.resize(2 * L);

        det_inv = fB * gW - fW * gB;
        det_inv.resize(2 * L);
        det_inv = poly_inv(det_inv);

        B = x * gW - y * fW;
        B.resize(2 * L);
        B = B * det_inv;
        B.resize(2 * L);
        W = y * fB - x * gB;
        W.resize(2 * L);
        W = W * det_inv;
        W.resize(2 * L);
    }
    return {B, W};
}

int main() {
    io();
    prework(getsz(MAXN) * 2);
    int n;
    cin >> n;
    auto [B, W] = newton(n, {0}, {0});
    int fac_n = 1;
    for (int i = 1; i <= n; i++) {
        fac_n = (ll)fac_n * i % MOD;
    }

    cout << (ll)(B[n] + W[n]) * fac_n % MOD;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 15ms
memory: 11848kb

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 11ms
memory: 12084kb

input:

3

output:

24

result:

ok 1 number(s): "24"

Test #3:

score: 0
Accepted
time: 7ms
memory: 12080kb

input:

5

output:

3190

result:

ok 1 number(s): "3190"

Test #4:

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

input:

100

output:

413875584

result:

ok 1 number(s): "413875584"

Test #5:

score: 0
Accepted
time: 7ms
memory: 11784kb

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #6:

score: 0
Accepted
time: 7ms
memory: 11884kb

input:

2

output:

4

result:

ok 1 number(s): "4"

Test #7:

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

input:

3

output:

24

result:

ok 1 number(s): "24"

Test #8:

score: 0
Accepted
time: 11ms
memory: 11784kb

input:

4

output:

236

result:

ok 1 number(s): "236"

Test #9:

score: 0
Accepted
time: 7ms
memory: 12096kb

input:

5

output:

3190

result:

ok 1 number(s): "3190"

Test #10:

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

input:

6

output:

55182

result:

ok 1 number(s): "55182"

Test #11:

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

input:

7

output:

1165220

result:

ok 1 number(s): "1165220"

Test #12:

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

input:

8

output:

29013896

result:

ok 1 number(s): "29013896"

Test #13:

score: 0
Accepted
time: 11ms
memory: 11864kb

input:

9

output:

832517514

result:

ok 1 number(s): "832517514"

Test #14:

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

input:

10

output:

96547079

result:

ok 1 number(s): "96547079"

Test #15:

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

input:

11

output:

296100513

result:

ok 1 number(s): "296100513"

Test #16:

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

input:

12

output:

672446962

result:

ok 1 number(s): "672446962"

Test #17:

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

input:

13

output:

986909297

result:

ok 1 number(s): "986909297"

Test #18:

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

input:

14

output:

306542229

result:

ok 1 number(s): "306542229"

Test #19:

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

input:

15

output:

8548107

result:

ok 1 number(s): "8548107"

Test #20:

score: 0
Accepted
time: 11ms
memory: 11796kb

input:

16

output:

773960239

result:

ok 1 number(s): "773960239"

Test #21:

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

input:

17

output:

611627547

result:

ok 1 number(s): "611627547"

Test #22:

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

input:

18

output:

91793980

result:

ok 1 number(s): "91793980"

Test #23:

score: 0
Accepted
time: 7ms
memory: 11860kb

input:

19

output:

689202618

result:

ok 1 number(s): "689202618"

Test #24:

score: 0
Accepted
time: 11ms
memory: 11860kb

input:

20

output:

605957782

result:

ok 1 number(s): "605957782"

Test #25:

score: 0
Accepted
time: 141ms
memory: 14816kb

input:

10000

output:

713782215

result:

ok 1 number(s): "713782215"

Test #26:

score: 0
Accepted
time: 304ms
memory: 17592kb

input:

20000

output:

337916836

result:

ok 1 number(s): "337916836"

Test #27:

score: 0
Accepted
time: 300ms
memory: 17484kb

input:

30000

output:

580803285

result:

ok 1 number(s): "580803285"

Test #28:

score: 0
Accepted
time: 636ms
memory: 24396kb

input:

40000

output:

732660392

result:

ok 1 number(s): "732660392"

Test #29:

score: 0
Accepted
time: 636ms
memory: 24420kb

input:

50000

output:

660835595

result:

ok 1 number(s): "660835595"

Test #30:

score: 0
Accepted
time: 640ms
memory: 24468kb

input:

60000

output:

323452070

result:

ok 1 number(s): "323452070"

Test #31:

score: 0
Accepted
time: 1327ms
memory: 38044kb

input:

70000

output:

307315915

result:

ok 1 number(s): "307315915"

Test #32:

score: 0
Accepted
time: 1334ms
memory: 38124kb

input:

80000

output:

951757567

result:

ok 1 number(s): "951757567"

Test #33:

score: 0
Accepted
time: 1337ms
memory: 38044kb

input:

90000

output:

426123208

result:

ok 1 number(s): "426123208"

Test #34:

score: 0
Accepted
time: 1329ms
memory: 37960kb

input:

100000

output:

827418228

result:

ok 1 number(s): "827418228"

Test #35:

score: 0
Accepted
time: 1332ms
memory: 38056kb

input:

110000

output:

541614559

result:

ok 1 number(s): "541614559"

Test #36:

score: 0
Accepted
time: 1357ms
memory: 38128kb

input:

120000

output:

184688986

result:

ok 1 number(s): "184688986"

Test #37:

score: 0
Accepted
time: 1339ms
memory: 38020kb

input:

130000

output:

898089371

result:

ok 1 number(s): "898089371"

Test #38:

score: 0
Accepted
time: 2870ms
memory: 61740kb

input:

140000

output:

949540221

result:

ok 1 number(s): "949540221"

Test #39:

score: 0
Accepted
time: 2833ms
memory: 61668kb

input:

150000

output:

767689851

result:

ok 1 number(s): "767689851"

Test #40:

score: 0
Accepted
time: 2830ms
memory: 61588kb

input:

160000

output:

553494563

result:

ok 1 number(s): "553494563"

Test #41:

score: 0
Accepted
time: 2808ms
memory: 61692kb

input:

170000

output:

270711750

result:

ok 1 number(s): "270711750"

Test #42:

score: 0
Accepted
time: 2837ms
memory: 61588kb

input:

180000

output:

108155689

result:

ok 1 number(s): "108155689"

Test #43:

score: 0
Accepted
time: 2860ms
memory: 61576kb

input:

190000

output:

327542856

result:

ok 1 number(s): "327542856"

Test #44:

score: 0
Accepted
time: 2892ms
memory: 61592kb

input:

200000

output:

236144151

result:

ok 1 number(s): "236144151"

Test #45:

score: 0
Accepted
time: 2851ms
memory: 61764kb

input:

198798

output:

16935264

result:

ok 1 number(s): "16935264"

Extra Test:

score: 0
Extra Test Passed