結果

問題 No.1857 Gacha Addiction
ユーザー MitarushiMitarushi
提出日時 2021-12-31 22:57:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 5,456 ms / 6,000 ms
コード長 3,102 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 282,836 KB
最終ジャッジ日時 2024-07-01 21:53:05
合計ジャッジ時間 127,020 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
54,272 KB
testcase_01 AC 45 ms
54,400 KB
testcase_02 AC 44 ms
54,784 KB
testcase_03 AC 44 ms
54,144 KB
testcase_04 AC 181 ms
79,000 KB
testcase_05 AC 190 ms
78,844 KB
testcase_06 AC 187 ms
78,752 KB
testcase_07 AC 185 ms
79,236 KB
testcase_08 AC 182 ms
78,996 KB
testcase_09 AC 1,231 ms
180,920 KB
testcase_10 AC 1,228 ms
180,708 KB
testcase_11 AC 1,609 ms
175,396 KB
testcase_12 AC 1,224 ms
180,720 KB
testcase_13 AC 1,222 ms
180,636 KB
testcase_14 AC 3,860 ms
272,956 KB
testcase_15 AC 3,854 ms
275,352 KB
testcase_16 AC 3,807 ms
273,080 KB
testcase_17 AC 3,802 ms
275,880 KB
testcase_18 AC 3,826 ms
275,908 KB
testcase_19 AC 3,899 ms
271,164 KB
testcase_20 AC 5,427 ms
272,148 KB
testcase_21 AC 5,431 ms
277,196 KB
testcase_22 AC 5,387 ms
279,420 KB
testcase_23 AC 3,898 ms
280,464 KB
testcase_24 AC 3,868 ms
282,836 KB
testcase_25 AC 3,856 ms
281,804 KB
testcase_26 AC 3,919 ms
276,744 KB
testcase_27 AC 3,908 ms
278,848 KB
testcase_28 AC 3,868 ms
279,140 KB
testcase_29 AC 3,826 ms
277,468 KB
testcase_30 AC 5,368 ms
273,816 KB
testcase_31 AC 3,859 ms
276,580 KB
testcase_32 AC 3,877 ms
279,900 KB
testcase_33 AC 3,931 ms
274,032 KB
testcase_34 AC 5,456 ms
270,660 KB
testcase_35 AC 3,896 ms
277,088 KB
testcase_36 AC 3,911 ms
273,996 KB
testcase_37 AC 3,857 ms
281,440 KB
testcase_38 AC 3,850 ms
279,024 KB
testcase_39 AC 1,250 ms
186,128 KB
testcase_40 AC 1,869 ms
200,420 KB
testcase_41 AC 1,758 ms
239,092 KB
testcase_42 AC 484 ms
111,032 KB
testcase_43 AC 3,438 ms
260,960 KB
testcase_44 AC 3,717 ms
267,660 KB
testcase_45 AC 45 ms
54,528 KB
testcase_46 AC 45 ms
54,656 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 <<= 1


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 zero_pad(a, n):
    a.extend([0] * (n - len(a)))


def zero_remove(a, n):
    for _ in range(len(a) - n):
        a.pop()


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

    return c

def normal_convolution_mod(a, b):
    n = max(len(a) + len(b) - 1, 1)
    c = [0] * n
    for idx1, i in enumerate(a):
        for idx2, j in enumerate(b):
            c[idx1 + idx2] += i * j

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

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

    def __add__(self, other):
        a_num = self.num
        a_den = self.den
        b_num = other.num
        b_den = other.den

        n2 = max(len(a_num) + len(b_num) - 1, 1)

        if n2 < 128:
            c_num = [(i + j ) % mod for i,j in zip(normal_convolution(a_num, b_den), normal_convolution(b_num, a_den))]
            c_den = normal_convolution_mod(a_den, b_den)

        else:
            n = 1 << (n2-1).bit_length()
            zero_pad(a_num, n)
            zero_pad(a_den, n)
            zero_pad(b_num, n)
            zero_pad(b_den, n)

            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_num, w)
            fft_inplace(a_den, w)
            fft_inplace(b_num, w)
            fft_inplace(b_den, w)

            c_num = [(a * d + b * c) % mod for a, b, c, d in zip(a_num, a_den, b_num, b_den)]
            c_den = [a * b % mod for a, b in zip(a_den, b_den)]

            ifft_inplace(c_num, w)
            ifft_inplace(c_den, w)

            zero_remove(c_num, n)
            zero_remove(c_den, n)

        return Fraction(c_num, c_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([0, i ** 2 % mod], [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[i] * factorial % mod
    factorial = factorial * (i + 2) % mod
    ans %= mod

print(ans)

0