結果

問題 No.1856 Mex Sum 2
ユーザー tamatotamato
提出日時 2022-02-26 11:41:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,830 ms / 3,000 ms
コード長 4,049 bytes
コンパイル時間 480 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 137,220 KB
最終ジャッジ日時 2024-04-26 16:01:30
合計ジャッジ時間 76,858 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
58,808 KB
testcase_01 AC 47 ms
59,012 KB
testcase_02 AC 93 ms
76,444 KB
testcase_03 AC 48 ms
58,916 KB
testcase_04 AC 68 ms
67,388 KB
testcase_05 AC 53 ms
61,272 KB
testcase_06 AC 51 ms
61,292 KB
testcase_07 AC 55 ms
62,660 KB
testcase_08 AC 47 ms
59,068 KB
testcase_09 AC 51 ms
60,928 KB
testcase_10 AC 50 ms
60,048 KB
testcase_11 AC 50 ms
61,156 KB
testcase_12 AC 49 ms
59,768 KB
testcase_13 AC 46 ms
58,816 KB
testcase_14 AC 47 ms
58,880 KB
testcase_15 AC 47 ms
59,044 KB
testcase_16 AC 53 ms
61,624 KB
testcase_17 AC 87 ms
76,372 KB
testcase_18 AC 70 ms
68,704 KB
testcase_19 AC 50 ms
61,132 KB
testcase_20 AC 51 ms
61,696 KB
testcase_21 AC 45 ms
59,648 KB
testcase_22 AC 88 ms
76,416 KB
testcase_23 AC 47 ms
59,392 KB
testcase_24 AC 56 ms
63,088 KB
testcase_25 AC 85 ms
75,136 KB
testcase_26 AC 48 ms
58,880 KB
testcase_27 AC 48 ms
59,264 KB
testcase_28 AC 47 ms
59,264 KB
testcase_29 AC 715 ms
89,600 KB
testcase_30 AC 408 ms
83,328 KB
testcase_31 AC 688 ms
89,088 KB
testcase_32 AC 1,067 ms
96,256 KB
testcase_33 AC 1,095 ms
98,304 KB
testcase_34 AC 691 ms
89,472 KB
testcase_35 AC 443 ms
83,456 KB
testcase_36 AC 239 ms
79,488 KB
testcase_37 AC 689 ms
88,832 KB
testcase_38 AC 195 ms
78,080 KB
testcase_39 AC 77 ms
71,424 KB
testcase_40 AC 86 ms
76,288 KB
testcase_41 AC 90 ms
76,160 KB
testcase_42 AC 2,698 ms
133,668 KB
testcase_43 AC 2,205 ms
123,696 KB
testcase_44 AC 2,388 ms
128,228 KB
testcase_45 AC 2,571 ms
132,748 KB
testcase_46 AC 2,170 ms
124,264 KB
testcase_47 AC 2,442 ms
129,132 KB
testcase_48 AC 2,649 ms
133,868 KB
testcase_49 AC 2,639 ms
133,816 KB
testcase_50 AC 1,845 ms
116,960 KB
testcase_51 AC 2,710 ms
136,812 KB
testcase_52 AC 2,742 ms
136,524 KB
testcase_53 AC 2,766 ms
136,576 KB
testcase_54 AC 2,774 ms
136,960 KB
testcase_55 AC 2,769 ms
136,964 KB
testcase_56 AC 2,736 ms
136,648 KB
testcase_57 AC 2,749 ms
137,088 KB
testcase_58 AC 2,830 ms
137,220 KB
testcase_59 AC 2,796 ms
137,216 KB
testcase_60 AC 2,763 ms
136,580 KB
testcase_61 AC 2,759 ms
136,960 KB
testcase_62 AC 2,741 ms
137,148 KB
testcase_63 AC 2,752 ms
136,568 KB
testcase_64 AC 2,751 ms
136,964 KB
testcase_65 AC 2,753 ms
137,084 KB
testcase_66 AC 2,801 ms
136,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    input = sys.stdin.readline

    W = (
        1, 998244352, 911660635, 372528824, 929031873, 452798380, 922799308, 781712469, 476477967, 166035806, 258648936,
        584193783, 63912897, 350007156, 666702199, 968855178, 629671588, 24514907, 996173970, 363395222, 565042129,
        733596141, 267099868, 15311432)
    IW = (1, 998244352, 86583718, 509520358, 337190230, 87557064, 609441965, 135236158, 304459705, 685443576, 381598368,
          335559352, 129292727, 358024708, 814576206, 708402881, 283043518, 3707709, 121392023, 704923114, 950391366,
          428961804, 382752275, 469870224)

    # A: 係数を並べたベクトル,len(A) = 2^n
    def fft(A):
        N = len(A)
        n = N.bit_length() - 1
        N2 = N // 2
        A_new = [0] * N
        for lv in range(n):
            L = 1 << (n - lv - 1)
            w = W[lv + 1]
            ww = 1
            for j in range(1 << lv):
                for i in range(L):
                    f0_val = A[i + j * L * 2]
                    f1_val = A[i + j * L * 2 + L]
                    tmp = (ww * f1_val) % mod
                    A_new[i + j * L] = (f0_val + tmp) % mod
                    A_new[i + j * L + N2] = (f0_val - tmp) % mod
                ww = (ww * w) % mod
            A, A_new = A_new, A
        return A

    # A: 係数を並べたベクトル,len(A) = 2^n
    def ifft(A):
        N = len(A)
        n = N.bit_length() - 1
        N2 = N // 2
        A_new = [0] * N
        for lv in range(n):
            L = 1 << (n - lv - 1)
            w = IW[lv + 1]
            ww = 1
            for j in range(1 << lv):
                for i in range(L):
                    f0_val = A[i + j * L * 2]
                    f1_val = A[i + j * L * 2 + L]
                    tmp = (ww * f1_val) % mod
                    A_new[i + j * L] = (f0_val + tmp) % mod
                    A_new[i + j * L + N2] = (f0_val - tmp) % mod
                ww = (ww * w) % mod
            A, A_new = A_new, A
        return A

    def convolution(A0, B0, limit):
        if len(A0) <= 60 or len(B0) <= 60:
            return convolution_naive(A0, B0, limit)
        N = len(A0) + len(B0) - 1
        N0 = 1 << ((N - 1).bit_length())
        A = A0 + [0] * (N0 - len(A0))
        B = B0 + [0] * (N0 - len(B0))
        AA = fft(A)
        BB = fft(B)
        CC = [(aa * bb) % mod for aa, bb in zip(AA, BB)]
        C = ifft(CC)
        invN0 = pow(N0, mod - 2, mod)
        C = [(c * invN0) % mod for c in C]
        return C[:limit]

    def convolution_naive(A0, B0, limit):
        NA = len(A0)
        NB = len(B0)
        C = [0] * (NA + NB - 1)
        for i in range(NA):
            for j in range(NB):
                C[i + j] = (C[i + j] + (A0[i] * B0[j]) % mod) % mod
        return C[:limit]

    # comb init
    nmax = 2 * 10 ** 3 + 10  # change here
    fac = [0] * nmax
    finv = [0] * nmax
    inv = [0] * nmax
    fac[0] = 1
    fac[1] = 1
    finv[0] = 1
    finv[1] = 1
    inv[1] = 1
    for i in range(2, nmax):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod

    def comb(n, r):
        if n < r:
            return 0
        else:
            return (fac[n] * ((finv[r] * finv[n - r]) % mod)) % mod

    N, M = map(int, input().split())
    vec = []
    for i in range(N+1):
        vec.append(((pow(2, i, mod) - 1) * finv[i])%mod)
    dp = [vec[:]]
    for i in range(1, min(N, M + 1)):
        dp.append(convolution(vec, dp[-1], N+1))
        vec.pop()

    ans = 0
    for i in range(min(N, M + 1)):
        powM = 1
        pow2 = 1
        for j in range(N, i, -1):
            if i == M:
                if j == N:
                    ans = (ans + dp[i][j])%mod
            else:
                ans = (ans + ((dp[i][j] * powM)%mod * (finv[N - j] * pow2)%mod)%mod)%mod
            powM = (powM * (M - i))%mod
            pow2 = (pow2 * 2)%mod
    print((ans * fac[N])%mod)


if __name__ == '__main__':
    main()
0