結果

問題 No.2047 Path Factory
ユーザー tamatotamato
提出日時 2022-08-19 22:31:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 3,837 bytes
コンパイル時間 228 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 108,764 KB
最終ジャッジ日時 2024-04-23 13:31:19
合計ジャッジ時間 5,445 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,860 KB
testcase_01 AC 38 ms
54,880 KB
testcase_02 AC 39 ms
55,428 KB
testcase_03 AC 39 ms
55,496 KB
testcase_04 AC 40 ms
55,776 KB
testcase_05 AC 40 ms
56,180 KB
testcase_06 AC 44 ms
55,156 KB
testcase_07 AC 188 ms
93,376 KB
testcase_08 AC 175 ms
90,976 KB
testcase_09 AC 195 ms
93,528 KB
testcase_10 AC 248 ms
101,060 KB
testcase_11 AC 162 ms
90,992 KB
testcase_12 AC 136 ms
84,904 KB
testcase_13 AC 144 ms
85,204 KB
testcase_14 AC 186 ms
91,984 KB
testcase_15 AC 186 ms
94,364 KB
testcase_16 AC 146 ms
89,764 KB
testcase_17 AC 148 ms
89,408 KB
testcase_18 AC 165 ms
92,908 KB
testcase_19 AC 188 ms
96,100 KB
testcase_20 AC 257 ms
104,380 KB
testcase_21 AC 171 ms
93,024 KB
testcase_22 AC 98 ms
79,912 KB
testcase_23 AC 102 ms
81,216 KB
testcase_24 AC 98 ms
80,376 KB
testcase_25 AC 160 ms
107,536 KB
testcase_26 AC 265 ms
107,764 KB
testcase_27 AC 260 ms
106,640 KB
testcase_28 AC 163 ms
108,764 KB
testcase_29 AC 51 ms
68,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    from collections import deque
    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]

    N = int(input())
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    dp0 = [0] * (N + 1)
    dp1 = [0] * (N + 1)
    for v in seq:
        if not child[v]:
            dp1[v] = 1
        else:
            tmp = [1, 0, 0]
            for u in child[v]:
                tmp = convolution(tmp, [dp0[u], dp1[u]], 3)
            dp1[v] = (tmp[0] + tmp[1]) % mod
            dp0[v] = (tmp[1] + tmp[2]) % mod
    print(dp0[1])


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