結果

問題 No.1302 Random Tree Score
ユーザー tpynerivertpyneriver
提出日時 2020-10-29 21:11:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,882 ms / 3,000 ms
コード長 9,079 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 198,704 KB
最終ジャッジ日時 2024-07-21 22:39:21
合計ジャッジ時間 17,954 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
62,720 KB
testcase_01 AC 46 ms
62,720 KB
testcase_02 AC 485 ms
104,036 KB
testcase_03 AC 924 ms
137,180 KB
testcase_04 AC 478 ms
103,500 KB
testcase_05 AC 1,786 ms
197,580 KB
testcase_06 AC 1,783 ms
197,960 KB
testcase_07 AC 489 ms
104,452 KB
testcase_08 AC 948 ms
139,060 KB
testcase_09 AC 1,882 ms
198,212 KB
testcase_10 AC 1,765 ms
192,436 KB
testcase_11 AC 472 ms
102,136 KB
testcase_12 AC 1,779 ms
195,688 KB
testcase_13 AC 47 ms
62,720 KB
testcase_14 AC 1,820 ms
198,704 KB
testcase_15 AC 1,822 ms
198,380 KB
testcase_16 AC 46 ms
62,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#Convolution_998244353

MOD = 998244353
ROOT = 3

sum_e = (911660635, 509520358, 369330050, 332049552, 983190778, 123842337, 238493703, 975955924, 603855026, 856644456, 131300601, 842657263, 730768835, 942482514, 806263778, 151565301, 510815449, 503497456, 743006876, 741047443, 56250497, 0, 0, 0, 0, 0, 0, 0, 0, 0)
sum_ie = (86583718, 372528824, 373294451, 645684063, 112220581, 692852209, 155456985, 797128860, 90816748, 860285882, 927414960, 354738543, 109331171, 293255632, 535113200, 308540755, 121186627, 608385704, 438932459, 359477183, 824071951, 0, 0, 0, 0, 0, 0, 0, 0, 0)

def butterfly(arr):
    n = len(arr)
    h = (n - 1).bit_length()
    for ph in range(1, h + 1):
        w = 1 << (ph - 1)
        p = 1 << (h - ph)
        now = 1
        for s in range(w):
            offset = s << (h - ph + 1)
            for i in range(p):
                l = arr[i + offset]
                r = arr[i + offset + p] * now
                arr[i + offset] = (l + r) % MOD
                arr[i + offset + p] = (l - r) % MOD
            now *= sum_e[(~s & -~s).bit_length() - 1]
            now %= MOD

def butterfly_inv(arr):
    n = len(arr)
    h = (n - 1).bit_length()
    for ph in range(1, h + 1)[::-1]:
        w = 1 << (ph - 1)
        p = 1 << (h - ph)
        inow = 1
        for s in range(w):
            offset = s << (h - ph + 1)
            for i in range(p):
                l = arr[i + offset]
                r = arr[i + offset + p]
                arr[i + offset] = (l + r) % MOD
                arr[i + offset + p] = (MOD + l - r) * inow % MOD
            inow *= sum_ie[(~s & -~s).bit_length() - 1]
            inow %= MOD

def convolution(a, b):
    n = len(a)
    m = len(b)
    if not n or not m: return []
    if min(n, m) <= 50:
        if n < m:
            n, m = m, n
            a, b = b, a
        res = [0] * (n + m - 1)
        for i in range(n):
            for j in range(m):
                res[i + j] += a[i] * b[j]
                res[i + j] %= MOD
        return res
    z = 1 << (n + m - 2).bit_length()
    a += [0] * (z - n)
    b += [0] * (z - m)
    butterfly(a)
    butterfly(b)
    for i in range(z):
        a[i] *= b[i]
        a[i] %= MOD
    butterfly_inv(a)
    a = a[:n + m - 1]
    iz = pow(z, MOD - 2, MOD)
    for i in range(n + m - 1):
        a[i] *= iz
        a[i] %= MOD
    return a

def autocorrelation(a):
    n = len(a)
    if not n: return []
    if n <= 50:
        res = [0] * (2 * n - 1)
        for i in range(n):
            for j in range(n):
                res[i + j] += a[i] * a[j]
                res[i + j] %= MOD
        return res
    z = 1 << (2 * n - 2).bit_length()
    a += [0] * (z - n)
    butterfly(a)
    for i in range(z):
        a[i] *= a[i]
        a[i] %= MOD
    butterfly_inv(a)
    a = a[:2 * n - 1]
    iz = pow(z, MOD - 2, MOD)
    for i in range(2 * n - 1):
        a[i] *= iz
        a[i] %= MOD
    return a

def add(a, b):
    return [(va + vb) % MOD for va, vb in zip(a, b)]

def sub(a, b):
    return [(va - vb) % MOD for va, vb in zip(a, b)]

def times(a, k):
    return [v * k % MOD for v in a]

def multiply(a, b):
    return convolution(a.copy(), b.copy())

def square(a):
    return autocorrelation(a.copy())

def inverse(a):
    n = len(a)
    r = pow(a[0], MOD - 2, MOD)
    m = 1
    tmp = [r]
    while m < n:
        tmp += [0] * m
        m *= 2
        tmp = sub(times(tmp, 2), multiply(a[:m], square(tmp.copy())[:m]))
    res = tmp[:n]
    return res

def differentiate(a):
    n = len(a)
    res = [0] * n
    for i in range(1, n):
        res[i - 1] = a[i] * i % MOD
    return res

def integrate(a):
    n = len(a)
    res = [0] * n
    for i in range(n - 1):
        res[i + 1] = a[i] * pow(i + 1, MOD - 2, MOD) % MOD
    return res

def log(a):
    #assert a[0] == 1
    n = len(a)
    return integrate(multiply(differentiate(a), inverse(a))[:n])

def exp(a):
    #assert a[0] == 0
    n = len(a)
    res = [1]
    g = [1]
    q = differentiate(a)
    m = 1
    while m < n:
        g = sub(times(g, 2), multiply(res, square(g)[:m]))
        g += [0] * m
        res += [0] * m
        m *= 2
        w = add(q[:m], multiply(g, sub(differentiate(res), multiply(res, q[:m])[:m]))[:m])
        res = add(res, multiply(res, sub(a[:m], integrate(w)))[:m])
    return res[:n]

#Convolution_998244353

MOD = 998244353
ROOT = 3

sum_e = (911660635, 509520358, 369330050, 332049552, 983190778, 123842337, 238493703, 975955924, 603855026, 856644456, 131300601, 842657263, 730768835, 942482514, 806263778, 151565301, 510815449, 503497456, 743006876, 741047443, 56250497, 0, 0, 0, 0, 0, 0, 0, 0, 0)
sum_ie = (86583718, 372528824, 373294451, 645684063, 112220581, 692852209, 155456985, 797128860, 90816748, 860285882, 927414960, 354738543, 109331171, 293255632, 535113200, 308540755, 121186627, 608385704, 438932459, 359477183, 824071951, 0, 0, 0, 0, 0, 0, 0, 0, 0)

def butterfly(arr):
    n = len(arr)
    h = (n - 1).bit_length()
    for ph in range(1, h + 1):
        w = 1 << (ph - 1)
        p = 1 << (h - ph)
        now = 1
        for s in range(w):
            offset = s << (h - ph + 1)
            for i in range(p):
                l = arr[i + offset]
                r = arr[i + offset + p] * now
                arr[i + offset] = (l + r) % MOD
                arr[i + offset + p] = (l - r) % MOD
            now *= sum_e[(~s & -~s).bit_length() - 1]
            now %= MOD

def butterfly_inv(arr):
    n = len(arr)
    h = (n - 1).bit_length()
    for ph in range(1, h + 1)[::-1]:
        w = 1 << (ph - 1)
        p = 1 << (h - ph)
        inow = 1
        for s in range(w):
            offset = s << (h - ph + 1)
            for i in range(p):
                l = arr[i + offset]
                r = arr[i + offset + p]
                arr[i + offset] = (l + r) % MOD
                arr[i + offset + p] = (MOD + l - r) * inow % MOD
            inow *= sum_ie[(~s & -~s).bit_length() - 1]
            inow %= MOD

def convolution(a, b):
    n = len(a)
    m = len(b)
    if not n or not m: return []
    if min(n, m) <= 50:
        if n < m:
            n, m = m, n
            a, b = b, a
        res = [0] * (n + m - 1)
        for i in range(n):
            for j in range(m):
                res[i + j] += a[i] * b[j]
                res[i + j] %= MOD
        return res
    z = 1 << (n + m - 2).bit_length()
    a += [0] * (z - n)
    b += [0] * (z - m)
    butterfly(a)
    butterfly(b)
    for i in range(z):
        a[i] *= b[i]
        a[i] %= MOD
    butterfly_inv(a)
    a = a[:n + m - 1]
    iz = pow(z, MOD - 2, MOD)
    for i in range(n + m - 1):
        a[i] *= iz
        a[i] %= MOD
    return a

def autocorrelation(a):
    n = len(a)
    if not n: return []
    if n <= 50:
        res = [0] * (2 * n - 1)
        for i in range(n):
            for j in range(n):
                res[i + j] += a[i] * a[j]
                res[i + j] %= MOD
        return res
    z = 1 << (2 * n - 2).bit_length()
    a += [0] * (z - n)
    butterfly(a)
    for i in range(z):
        a[i] *= a[i]
        a[i] %= MOD
    butterfly_inv(a)
    a = a[:2 * n - 1]
    iz = pow(z, MOD - 2, MOD)
    for i in range(2 * n - 1):
        a[i] *= iz
        a[i] %= MOD
    return a

def add(a, b):
    return [(va + vb) % MOD for va, vb in zip(a, b)]

def sub(a, b):
    return [(va - vb) % MOD for va, vb in zip(a, b)]

def times(a, k):
    return [v * k % MOD for v in a]

def multiply(a, b):
    return convolution(a.copy(), b.copy())

def square(a):
    return autocorrelation(a.copy())

def inverse(a):
    n = len(a)
    r = pow(a[0], MOD - 2, MOD)
    m = 1
    tmp = [r]
    while m < n:
        tmp += [0] * m
        m *= 2
        tmp = sub(times(tmp, 2), multiply(a[:m], square(tmp.copy())[:m]))
    res = tmp[:n]
    return res

def differentiate(a):
    n = len(a)
    res = [0] * n
    for i in range(1, n):
        res[i - 1] = a[i] * i % MOD
    return res

def integrate(a):
    n = len(a)
    res = [0] * n
    for i in range(n - 1):
        res[i + 1] = a[i] * pow(i + 1, MOD - 2, MOD) % MOD
    return res

def log(a):
    #assert a[0] == 1
    n = len(a)
    return integrate(multiply(differentiate(a), inverse(a))[:n])

def exp(a):
    #assert a[0] == 0
    n = len(a)
    res = [1]
    g = [1]
    q = differentiate(a)
    m = 1
    while m < n:
        g = sub(times(g, 2), multiply(res, square(g)[:m]))
        g += [0] * m
        res += [0] * m
        m *= 2
        w = add(q[:m], multiply(g, sub(differentiate(res), multiply(res, q[:m])[:m]))[:m])
        res = add(res, multiply(res, sub(a[:m], integrate(w)))[:m])
    return res[:n]


def make_fac(limit):
    fac = [1]*limit
    for i in range(2,limit):
        fac[i] = i * fac[i-1]%MOD
    faci = [0]*limit
    faci[-1] = pow(fac[-1], MOD -2, MOD)
    for i in range(limit-2, -1, -1):
        faci[i] = faci[i+1] * (i + 1) % MOD
    return fac, faci
fac, faci = make_fac(134139)


import sys
readline = sys.stdin.readline

N = int(readline())



f = [(i+1)*faci[i]%MOD for i in range(N)]
print(exp(times(log(f), N))[N-2]*fac[N-2]*pow(N, (N-2)*(MOD-2)%(MOD-1), MOD)%MOD)
0