結果

問題 No.1857 Gacha Addiction
ユーザー MitarushiMitarushi
提出日時 2021-12-31 22:58:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 5,108 ms / 6,000 ms
コード長 3,101 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 300,616 KB
最終ジャッジ日時 2023-09-14 14:46:39
合計ジャッジ時間 117,706 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,808 KB
testcase_01 AC 90 ms
71,768 KB
testcase_02 AC 88 ms
71,520 KB
testcase_03 AC 89 ms
71,720 KB
testcase_04 AC 236 ms
81,016 KB
testcase_05 AC 240 ms
80,824 KB
testcase_06 AC 246 ms
81,276 KB
testcase_07 AC 240 ms
81,172 KB
testcase_08 AC 239 ms
80,832 KB
testcase_09 AC 1,240 ms
195,628 KB
testcase_10 AC 1,228 ms
196,256 KB
testcase_11 AC 1,225 ms
195,704 KB
testcase_12 AC 1,229 ms
195,944 KB
testcase_13 AC 1,226 ms
196,200 KB
testcase_14 AC 5,108 ms
294,080 KB
testcase_15 AC 3,800 ms
294,380 KB
testcase_16 AC 3,796 ms
295,968 KB
testcase_17 AC 3,733 ms
294,508 KB
testcase_18 AC 3,747 ms
299,332 KB
testcase_19 AC 3,758 ms
299,580 KB
testcase_20 AC 3,763 ms
300,268 KB
testcase_21 AC 3,688 ms
296,580 KB
testcase_22 AC 3,716 ms
296,324 KB
testcase_23 AC 3,684 ms
299,232 KB
testcase_24 AC 3,720 ms
299,108 KB
testcase_25 AC 3,724 ms
295,632 KB
testcase_26 AC 3,738 ms
295,268 KB
testcase_27 AC 3,702 ms
298,360 KB
testcase_28 AC 3,749 ms
298,272 KB
testcase_29 AC 3,694 ms
295,084 KB
testcase_30 AC 3,712 ms
298,352 KB
testcase_31 AC 3,676 ms
298,880 KB
testcase_32 AC 3,706 ms
298,496 KB
testcase_33 AC 3,654 ms
298,104 KB
testcase_34 AC 3,682 ms
299,344 KB
testcase_35 AC 3,769 ms
298,900 KB
testcase_36 AC 3,696 ms
299,312 KB
testcase_37 AC 3,649 ms
296,876 KB
testcase_38 AC 3,664 ms
300,616 KB
testcase_39 AC 1,327 ms
209,384 KB
testcase_40 AC 1,381 ms
213,012 KB
testcase_41 AC 2,424 ms
255,876 KB
testcase_42 AC 538 ms
119,952 KB
testcase_43 AC 3,351 ms
281,868 KB
testcase_44 AC 3,651 ms
299,860 KB
testcase_45 AC 92 ms
71,652 KB
testcase_46 AC 92 ms
71,496 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 < 16:
            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