結果

問題 No.1857 Gacha Addiction
ユーザー MitarushiMitarushi
提出日時 2021-12-31 22:58:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 5,181 ms / 6,000 ms
コード長 3,101 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 81,520 KB
実行使用メモリ 299,692 KB
最終ジャッジ日時 2024-07-01 22:05:19
合計ジャッジ時間 116,107 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
54,528 KB
testcase_01 AC 47 ms
54,400 KB
testcase_02 AC 45 ms
54,400 KB
testcase_03 AC 44 ms
54,656 KB
testcase_04 AC 202 ms
80,068 KB
testcase_05 AC 202 ms
80,452 KB
testcase_06 AC 206 ms
80,288 KB
testcase_07 AC 202 ms
80,172 KB
testcase_08 AC 200 ms
79,708 KB
testcase_09 AC 1,185 ms
186,932 KB
testcase_10 AC 1,183 ms
185,084 KB
testcase_11 AC 1,186 ms
186,668 KB
testcase_12 AC 1,190 ms
187,140 KB
testcase_13 AC 1,190 ms
186,676 KB
testcase_14 AC 5,181 ms
291,356 KB
testcase_15 AC 3,694 ms
297,924 KB
testcase_16 AC 3,660 ms
297,500 KB
testcase_17 AC 3,724 ms
296,348 KB
testcase_18 AC 3,676 ms
295,752 KB
testcase_19 AC 3,679 ms
298,088 KB
testcase_20 AC 3,661 ms
297,576 KB
testcase_21 AC 3,689 ms
297,568 KB
testcase_22 AC 3,693 ms
299,692 KB
testcase_23 AC 3,686 ms
296,136 KB
testcase_24 AC 3,694 ms
297,560 KB
testcase_25 AC 3,688 ms
297,696 KB
testcase_26 AC 3,691 ms
296,264 KB
testcase_27 AC 3,681 ms
297,620 KB
testcase_28 AC 3,644 ms
297,816 KB
testcase_29 AC 3,692 ms
297,700 KB
testcase_30 AC 3,711 ms
297,280 KB
testcase_31 AC 3,650 ms
297,816 KB
testcase_32 AC 3,690 ms
297,620 KB
testcase_33 AC 3,683 ms
297,976 KB
testcase_34 AC 3,675 ms
293,728 KB
testcase_35 AC 3,669 ms
297,572 KB
testcase_36 AC 3,681 ms
297,564 KB
testcase_37 AC 3,702 ms
297,420 KB
testcase_38 AC 3,684 ms
297,816 KB
testcase_39 AC 1,299 ms
205,972 KB
testcase_40 AC 1,352 ms
203,892 KB
testcase_41 AC 2,395 ms
253,884 KB
testcase_42 AC 497 ms
117,684 KB
testcase_43 AC 3,343 ms
275,100 KB
testcase_44 AC 3,648 ms
296,796 KB
testcase_45 AC 42 ms
55,276 KB
testcase_46 AC 43 ms
54,920 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