結果

問題 No.1857 Gacha Addiction
ユーザー MitarushiMitarushi
提出日時 2021-12-30 22:46:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 5,761 ms / 6,000 ms
コード長 2,653 bytes
コンパイル時間 883 ms
コンパイル使用メモリ 86,888 KB
実行使用メモリ 265,168 KB
最終ジャッジ日時 2023-09-14 14:44:03
合計ジャッジ時間 173,698 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,648 KB
testcase_01 AC 91 ms
71,536 KB
testcase_02 AC 90 ms
71,764 KB
testcase_03 AC 90 ms
71,688 KB
testcase_04 AC 256 ms
81,020 KB
testcase_05 AC 259 ms
81,212 KB
testcase_06 AC 256 ms
80,684 KB
testcase_07 AC 260 ms
80,776 KB
testcase_08 AC 257 ms
81,236 KB
testcase_09 AC 1,832 ms
167,860 KB
testcase_10 AC 1,848 ms
167,956 KB
testcase_11 AC 1,800 ms
168,948 KB
testcase_12 AC 1,838 ms
167,796 KB
testcase_13 AC 1,830 ms
167,408 KB
testcase_14 AC 5,508 ms
263,064 KB
testcase_15 AC 5,531 ms
263,968 KB
testcase_16 AC 5,536 ms
264,884 KB
testcase_17 AC 5,688 ms
263,596 KB
testcase_18 AC 5,654 ms
264,808 KB
testcase_19 AC 5,627 ms
264,220 KB
testcase_20 AC 5,585 ms
263,128 KB
testcase_21 AC 5,701 ms
264,540 KB
testcase_22 AC 5,761 ms
265,168 KB
testcase_23 AC 5,614 ms
262,384 KB
testcase_24 AC 5,640 ms
263,576 KB
testcase_25 AC 5,576 ms
263,336 KB
testcase_26 AC 5,610 ms
263,848 KB
testcase_27 AC 5,612 ms
262,744 KB
testcase_28 AC 5,613 ms
263,960 KB
testcase_29 AC 5,563 ms
262,996 KB
testcase_30 AC 5,571 ms
264,616 KB
testcase_31 AC 5,634 ms
263,492 KB
testcase_32 AC 5,669 ms
263,580 KB
testcase_33 AC 5,534 ms
263,332 KB
testcase_34 AC 5,517 ms
263,008 KB
testcase_35 AC 5,520 ms
264,872 KB
testcase_36 AC 5,565 ms
263,496 KB
testcase_37 AC 5,578 ms
264,616 KB
testcase_38 AC 5,547 ms
264,120 KB
testcase_39 AC 2,026 ms
175,152 KB
testcase_40 AC 2,204 ms
185,168 KB
testcase_41 AC 2,610 ms
219,868 KB
testcase_42 AC 696 ms
104,684 KB
testcase_43 AC 4,982 ms
262,016 KB
testcase_44 AC 5,460 ms
263,432 KB
testcase_45 AC 93 ms
71,480 KB
testcase_46 AC 92 ms
71,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

mod = 998244353


def fft_inplace(a, w):
    n = len(a)
    m = n
    t = 1
    while m >= 2:
        mh = m >> 1
        for i in range(0, n, m):
            for s in range(mh):
                j, k = i+s, i+mh+s
                a[j], a[k] = (a[j]+a[k]) % mod, (a[j]-a[k])*w[s*t] % mod
        m = mh
        t *= 2


def ifft_inplace(a, w):
    n = len(a)
    m = 2
    t = -(n >> 1)
    while m <= n:
        mh = m >> 1
        for i in range(0, n, m):
            for s in range(mh):
                j, k = i+s, i+mh+s
                a[k] *= w[s*t]
                a[j], a[k] = (a[j]+a[k]) % mod, (a[j]-a[k]) % mod
        m <<= 1
        t //= 2
    n_inv = pow(n, mod-2, mod)
    for i in range(n):
        a[i] = a[i] * n_inv % mod


def normal_convolution(a, b):
    n = max(len(a) + len(b) - 1, 1)
    c = [0] * n
    for i in range(len(a)):
        for j in range(len(b)):
            c[i+j] += a[i] * b[j]

    for i in range(n):
        c[i] %= mod
    return c


def convolution(a, b):
    n2 = max(len(a) + len(b), 1)

    if len(a) * len(b) < 1 << 16:
        return normal_convolution(a, b)

    n = 1 << (n2-1).bit_length()
    a = a + [0] * (n-len(a))
    b = b + [0] * (n-len(b))

    w_root = pow(3, (mod-1)//n, mod)
    w = [1] * n
    for i in range(1, n):
        w[i] = w[i-1] * w_root % mod

    fft_inplace(a, w)
    fft_inplace(b, w)
    c = [i*j % mod for i, j in zip(a, b)]
    ifft_inplace(c, w)
    return c[:n2]


class Poly:
    def __init__(Self, coeffs):
        Self.coeffs = coeffs

    def __add__(Self, other):
        n = max(len(Self.coeffs), len(other.coeffs))
        result = [0] * n
        for idx, i in enumerate(Self.coeffs):
            result[idx] += i
        for idx, i in enumerate(other.coeffs):
            result[idx] += i
            result[idx] %= mod
        return Poly(result)

    def __mul__(Self, other):
        return Poly(convolution(Self.coeffs, other.coeffs))


class Fraction:
    def __init__(Self, num, den):
        Self.num = num
        Self.den = den

    def __add__(Self, other):
        return Fraction(Self.num * other.den + Self.den * other.num, Self.den * other.den)


n, s = map(int, input().split())
s_inv = pow(s, mod-2, mod)
p = [i * s_inv % mod for i in map(int, input().split())]

fractions = collections.deque(
    Fraction(Poly([0, i ** 2 % mod]), Poly([1, i])) for i in p
)

while len(fractions) > 1:
    fractions.append(fractions.popleft() + fractions.popleft())

ans = 0
factorial = 2
for i in range(1, n + 1):
    ans += fractions[0].num.coeffs[i] * factorial % mod
    factorial = factorial * (i + 2) % mod
    ans %= mod

print(ans)

0