結果

問題 No.2688 Cell Proliferation (Hard)
ユーザー tpynerivertpyneriver
提出日時 2024-04-09 17:58:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,481 ms / 4,000 ms
コード長 10,918 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 82,536 KB
実行使用メモリ 151,884 KB
最終ジャッジ日時 2024-10-01 18:02:12
合計ジャッジ時間 27,027 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,296 KB
testcase_01 AC 72 ms
73,600 KB
testcase_02 AC 51 ms
64,128 KB
testcase_03 AC 238 ms
84,956 KB
testcase_04 AC 1,380 ms
147,076 KB
testcase_05 AC 781 ms
113,968 KB
testcase_06 AC 413 ms
94,448 KB
testcase_07 AC 418 ms
94,488 KB
testcase_08 AC 1,334 ms
144,832 KB
testcase_09 AC 1,275 ms
142,316 KB
testcase_10 AC 1,404 ms
148,892 KB
testcase_11 AC 1,353 ms
145,752 KB
testcase_12 AC 450 ms
96,236 KB
testcase_13 AC 1,481 ms
151,484 KB
testcase_14 AC 1,213 ms
138,828 KB
testcase_15 AC 1,464 ms
151,884 KB
testcase_16 AC 1,210 ms
138,104 KB
testcase_17 AC 838 ms
116,300 KB
testcase_18 AC 1,427 ms
149,276 KB
testcase_19 AC 817 ms
116,092 KB
testcase_20 AC 650 ms
108,136 KB
testcase_21 AC 646 ms
108,072 KB
testcase_22 AC 797 ms
115,504 KB
testcase_23 AC 679 ms
109,628 KB
testcase_24 AC 1,395 ms
148,236 KB
testcase_25 AC 467 ms
96,304 KB
testcase_26 AC 1,437 ms
150,100 KB
testcase_27 AC 811 ms
116,564 KB
testcase_28 AC 1,209 ms
138,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline

MOD = 998244353
_IMAG = 911660635
_IIMAG = 86583718
_rate2 = (0, 911660635, 509520358, 369330050, 332049552, 983190778, 123842337, 238493703, 975955924, 603855026, 856644456, 131300601, 842657263, 730768835, 942482514, 806263778, 151565301, 510815449, 503497456, 743006876, 741047443, 56250497, 867605899, 0)
_irate2 = (0, 86583718, 372528824, 373294451, 645684063, 112220581, 692852209, 155456985, 797128860, 90816748, 860285882, 927414960, 354738543, 109331171, 293255632, 535113200, 308540755, 121186627, 608385704, 438932459, 359477183, 824071951, 103369235, 0)
_rate3 = (0, 372528824, 337190230, 454590761, 816400692, 578227951, 180142363, 83780245, 6597683, 70046822, 623238099, 183021267, 402682409, 631680428, 344509872, 689220186, 365017329, 774342554, 729444058, 102986190, 128751033, 395565204, 0)
_irate3 = (0, 509520358, 929031873, 170256584, 839780419, 282974284, 395914482, 444904435, 72135471, 638914820, 66769500, 771127074, 985925487, 262319669, 262341272, 625870173, 768022760, 859816005, 914661783, 430819711, 272774365, 530924681, 0)

def _fft(a):
    n = len(a)
    h = (n - 1).bit_length()
    le = 0
    for le in range(0, h - 1, 2):
        p = 1 << (h - le - 2)
        rot = 1
        for s in range(1 << le):
            rot2 = rot * rot % MOD
            rot3 = rot2 * rot % MOD
            offset = s << (h - le)
            for i in range(p):
                a0 = a[i + offset]
                a1 = a[i + offset + p] * rot
                a2 = a[i + offset + p * 2] * rot2
                a3 = a[i + offset + p * 3] * rot3
                a1na3imag = (a1 - a3) % MOD * _IMAG
                a[i + offset] = (a0 + a2 + a1 + a3) % MOD
                a[i + offset + p] = (a0 + a2 - a1 - a3) % MOD
                a[i + offset + p * 2] = (a0 - a2 + a1na3imag) % MOD
                a[i + offset + p * 3] = (a0 - a2 - a1na3imag) % MOD
            rot = rot * _rate3[(~s & -~s).bit_length()] % MOD
    if h - le & 1:
        rot = 1
        for s in range(1 << (h - 1)):
            offset = s << 1
            l = a[offset]
            r = a[offset + 1] * rot
            a[offset] = (l + r) % MOD
            a[offset + 1] = (l - r) % MOD
            rot = rot * _rate2[(~s & -~s).bit_length()] % MOD

def _ifft(a):
    n = len(a)
    h = (n - 1).bit_length()
    le = h
    for le in range(h, 1, -2):
        p = 1 << (h - le)
        irot = 1
        for s in range(1 << (le - 2)):
            irot2 = irot * irot % MOD
            irot3 = irot2 * irot % MOD
            offset = s << (h - le + 2)
            for i in range(p):
                a0 = a[i + offset]
                a1 = a[i + offset + p]
                a2 = a[i + offset + p * 2]
                a3 = a[i + offset + p * 3]
                a2na3iimag = (a2 - a3) * _IIMAG % MOD
                a[i + offset] = (a0 + a1 + a2 + a3) % MOD
                a[i + offset + p] = (a0 - a1 + a2na3iimag) * irot % MOD
                a[i + offset + p * 2] = (a0 + a1 - a2 - a3) * irot2 % MOD
                a[i + offset + p * 3] = (a0 - a1 - a2na3iimag) * irot3 % MOD
            irot = irot * _irate3[(~s & -~s).bit_length()] % MOD
    if le & 1:
        p = 1 << (h - 1)
        for i in range(p):
            l = a[i]
            r = a[i + p]
            a[i] = l + r if l + r < MOD else l + r - MOD
            a[i + p] = l - r if l - r >= 0 else l - r + MOD

def ntt(a) -> None:
    if len(a) <= 1: return
    _fft(a)

def intt(a) -> None:
    if len(a) <= 1: return
    _ifft(a)
    iv = pow(len(a), MOD - 2, MOD)
    for i, x in enumerate(a): a[i] = x * iv % MOD

def multiply(s: list, t: list) -> list:
    n, m = len(s), len(t)
    l = n + m - 1
    if min(n, m) <= 60:
        a = [0] * l
        for i, x in enumerate(s):
            for j, y in enumerate(t):
                a[i + j] += x * y
        return [x % MOD for x in a]
    z = 1 << (l - 1).bit_length()
    a = s + [0] * (z - n)
    b = t + [0] * (z - m)
    _fft(a)
    _fft(b)
    for i, x in enumerate(b): a[i] = a[i] * x % MOD
    _ifft(a)
    a[l:] = []
    iz = pow(z, MOD - 2, MOD)
    return [x * iz % MOD for x in a]

def pow2(s: list) -> list:
    n = len(s)
    l = (n << 1) - 1
    if n <= 60:
        a = [0] * l
        for i, x in enumerate(s):
            for j, y in enumerate(s):
                a[i + j] += x * y
        return [x % MOD for x in a]
    z = 1 << (l - 1).bit_length()
    a = s + [0] * (z - n)
    _fft(a)
    for i, x in enumerate(a): a[i] = x * x % MOD
    _ifft(a)
    a[l:] = []
    iz = pow(z, MOD - 2, MOD)
    return [x * iz % MOD for x in a]

def ntt_doubling(a: list) -> None:
    M = len(a)
    b = a[:]
    intt(b)
    r = 1
    zeta = pow(3, (MOD - 1) // (M << 1), MOD)
    for i, x in enumerate(b):
        b[i] = x * r % MOD
        r = r * zeta % MOD
    ntt(b)
    a += b

# https://nyaannyaan.github.io/library/fps/formal-power-series.hpp
def shrink(a: list) -> None:
    while a and not a[-1]: a.pop()

def fps_add(a: list, b: list) -> list:
    if len(a) < len(b):
        res = b[::]
        for i, x in enumerate(a): res[i] += x
    else:
        res = a[::]
        for i, x in enumerate(b): res[i] += x
    return [x % MOD for x in res]

def fps_add_scalar(a: list, k: int) -> list:
    res = a[:]
    res[0] = (res[0] + k) % MOD
    return res

def fps_sub(a: list, b: list) -> list:
    if len(a) < len(b):
        res = b[::]
        for i, x in enumerate(a): res[i] -= x
        res = fps_neg(res)
    else:
        res = a[::]
        for i, x in enumerate(b): res[i] -= x
    return [x % MOD for x in res]

def fps_sub_scalar(a: list, k: int) -> list:
    return fps_add_scalar(a, -k)

def fps_neg(a: list) -> list:
    return [MOD - x if x else 0 for x in a]

def fps_mul_scalar(a: list, k: int) -> list:
    return [x * k % MOD for x in a]

def fps_matmul(a: list, b: list) -> list:
    'not verified'
    return [x * b[i] % MOD for i, x in enumerate(a)]

def fps_div(a: list, b: list) -> list:
    if len(a) < len(b): return []
    n = len(a) - len(b) + 1
    cnt = 0
    if len(b) > 64:
        return multiply(a[::-1][:n], fps_inv(b[::-1], n))[:n][::-1]
    f, g = a[::], b[::]
    while g and not g[-1]:
        g.pop()
        cnt += 1
    coef = pow(g[-1], MOD - 2, MOD)
    g = fps_mul_scalar(g, coef)
    deg = len(f) - len(g) + 1
    gs = len(g)
    quo = [0] * deg
    for i in range(deg)[::-1]:
        quo[i] = x = f[i + gs - 1] % MOD
        for j, y in enumerate(g):
            f[i + j] -= x * y
    return fps_mul_scalar(quo, coef) + [0] * cnt

def fps_mod(a: list, b: list) -> list:
    res = fps_sub(a, multiply(fps_div(a, b),  b))
    while res and not res[-1]: res.pop()
    return res

def fps_divmod(a: list, b: list):
    q = fps_div(a, b)
    r = fps_sub(a, multiply(q, b))
    while r and not r[-1]: r.pop()
    return q, r

def fps_eval(a: list, x: int) -> int:
    r = 0; w = 1
    for v in a:
        r += w * v % MOD
        w = w * x % MOD
    return r % MOD

def fps_inv(a: list, deg: int=-1) -> list:
    # assert(self[0] != 0)
    if deg == -1: deg = len(a)
    res = [0] * deg
    res[0] = pow(a[0], MOD - 2, MOD)
    d = 1
    while d < deg:
        f = [0] * (d << 1)
        tmp = min(len(a), d << 1)
        f[:tmp] = a[:tmp]
        g = [0] * (d << 1)
        g[:d] = res[:d]
        ntt(f)
        ntt(g)
        for i, x in enumerate(g): f[i] = f[i] * x % MOD
        intt(f)
        f[:d] = [0] * d
        ntt(f)
        for i, x in enumerate(g): f[i] = f[i] * x % MOD
        intt(f)
        for j in range(d, min(d << 1, deg)):
            if f[j]: res[j] = MOD - f[j]
            else: res[j] = 0
        d <<= 1
    return res

def fps_pow(a: list, k: int, deg=-1) -> list:
    n = len(a)
    if deg == -1: deg = n
    if k == 0:
        if not deg: return []
        ret = [0] * deg
        ret[0] = 1
        return ret
    for i, x in enumerate(a):
        if x:
            rev = pow(x, MOD - 2, MOD)
            ret = fps_mul_scalar(fps_exp(fps_mul_scalar(fps_log(fps_mul_scalar(a, rev)[i:], deg),  k), deg), pow(x, k, MOD))
            ret[:0] = [0] * (i * k)
            if len(ret) < deg:
                ret[len(ret):] = [0] * (deg - len(ret))
                return ret
            return ret[:deg]
        if (i + 1) * k >= deg: break
    return [0] * deg

def fps_exp(a: list, deg=-1) -> list:
    # assert(not self or self[0] == 0)
    if deg == -1: deg = len(a)
    inv = [0, 1]

    def inplace_integral(F: list) -> list:
        n = len(F)
        while len(inv) <= n:
            j, k = divmod(MOD, len(inv))
            inv.append((-inv[k] * j) % MOD)
        return [0] + [x * inv[i + 1] % MOD for i, x in enumerate(F)]

    def inplace_diff(F: list) -> list:
        return [x * i % MOD for i, x in enumerate(F) if i]

    b = [1, (a[1] if 1 < len(a) else 0)]
    c = [1]
    z1 = []
    z2 = [1, 1]
    m = 2
    while m < deg:
        y = b + [0] * m
        ntt(y)
        z1 = z2
        z = [y[i] * p % MOD for i, p in enumerate(z1)]
        intt(z)
        z[:m >> 1] = [0] * (m >> 1)
        ntt(z)
        for i, p in enumerate(z1): z[i] = z[i] * (-p) % MOD
        intt(z)
        c[m >> 1:] = z[m >> 1:]
        z2 = c + [0] * m
        ntt(z2)
        tmp = min(len(a), m)
        x = a[:tmp] + [0] * (m - tmp)
        x = inplace_diff(x)
        x.append(0)
        ntt(x)
        for i, p in enumerate(x): x[i] = y[i] * p % MOD
        intt(x)
        for i, p in enumerate(b):
            if not i: continue
            x[i - 1] -= p * i % MOD
        x += [0] * m
        for i in range(m - 1): x[m + i], x[i] = x[i], 0
        ntt(x)
        for i, p in enumerate(z2): x[i] = x[i] * p % MOD
        intt(x)
        x.pop()
        x = inplace_integral(x)
        x[:m] = [0] * m
        for i in range(m, min(len(a), m << 1)): x[i] += a[i]
        ntt(x)
        for i, p in enumerate(y): x[i] = x[i] * p % MOD
        intt(x)
        b[m:] = x[m:]
        m <<= 1
    return b[:deg]

def fps_log(a: list, deg=-1) -> list:
    # assert(a[0] == 1)
    if deg == -1: deg = len(a)
    return fps_integral(multiply(fps_diff(a), fps_inv(a, deg))[:deg - 1])

def fps_integral(a: list) -> list:
    n = len(a)
    res = [0] * (n + 1)
    if n: res[1] = 1
    for i in range(2, n + 1):
        j, k = divmod(MOD, i)
        res[i] = (-res[k] * j) % MOD
    for i, x in enumerate(a): res[i + 1] = res[i + 1] * x % MOD
    return res

def fps_diff(a: list) -> list:
    return [i * x % MOD for i, x in enumerate(a) if i]

p1, p2, q1, q2, T = map(int, readline().split())
P = p1 * pow(p2, MOD - 2, MOD) % MOD
Q = q1 * pow(q2, MOD - 2, MOD) % MOD

G = [0]*(T+2)
G[2] = 1
for i in range(1, T+2):
    G[i] += G[i-1]

for i in range(1, T+2):
    G[i] = (G[i] + G[i-1]) % (MOD-1)
for i in range(1, T+2):
    G[i] = (G[i] + G[i-1]) % (MOD-1)
G = [pow(Q, g, MOD) for g in G]
F = fps_inv(fps_sub([1+P], fps_mul_scalar(G, P)))
ans = F[T+1]*pow(P, MOD-2, MOD)%MOD if P else G[T+1]
print(ans)
0