結果

問題 No.1857 Gacha Addiction
ユーザー MitarushiMitarushi
提出日時 2021-12-31 22:57:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 5,411 ms / 6,000 ms
コード長 3,102 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 286,004 KB
最終ジャッジ日時 2023-09-14 14:31:19
合計ジャッジ時間 128,166 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,508 KB
testcase_01 AC 92 ms
71,696 KB
testcase_02 AC 93 ms
71,516 KB
testcase_03 AC 91 ms
71,564 KB
testcase_04 AC 217 ms
80,136 KB
testcase_05 AC 216 ms
80,200 KB
testcase_06 AC 224 ms
80,392 KB
testcase_07 AC 222 ms
80,180 KB
testcase_08 AC 217 ms
80,140 KB
testcase_09 AC 1,248 ms
183,724 KB
testcase_10 AC 1,255 ms
181,788 KB
testcase_11 AC 1,643 ms
176,352 KB
testcase_12 AC 1,267 ms
183,644 KB
testcase_13 AC 1,240 ms
182,784 KB
testcase_14 AC 3,864 ms
279,892 KB
testcase_15 AC 3,871 ms
276,556 KB
testcase_16 AC 3,819 ms
278,728 KB
testcase_17 AC 3,793 ms
277,772 KB
testcase_18 AC 3,819 ms
271,464 KB
testcase_19 AC 3,876 ms
271,484 KB
testcase_20 AC 5,365 ms
281,688 KB
testcase_21 AC 5,411 ms
276,976 KB
testcase_22 AC 5,392 ms
276,952 KB
testcase_23 AC 3,877 ms
273,648 KB
testcase_24 AC 3,860 ms
285,664 KB
testcase_25 AC 3,855 ms
276,964 KB
testcase_26 AC 3,882 ms
277,096 KB
testcase_27 AC 3,843 ms
269,688 KB
testcase_28 AC 3,883 ms
277,700 KB
testcase_29 AC 3,841 ms
277,872 KB
testcase_30 AC 5,338 ms
286,004 KB
testcase_31 AC 3,875 ms
280,652 KB
testcase_32 AC 3,882 ms
283,464 KB
testcase_33 AC 3,838 ms
278,864 KB
testcase_34 AC 5,376 ms
281,688 KB
testcase_35 AC 3,869 ms
277,556 KB
testcase_36 AC 3,909 ms
274,784 KB
testcase_37 AC 3,880 ms
277,560 KB
testcase_38 AC 3,855 ms
279,832 KB
testcase_39 AC 1,288 ms
184,644 KB
testcase_40 AC 1,876 ms
203,380 KB
testcase_41 AC 1,784 ms
242,388 KB
testcase_42 AC 519 ms
113,912 KB
testcase_43 AC 3,460 ms
261,320 KB
testcase_44 AC 3,704 ms
270,672 KB
testcase_45 AC 93 ms
71,588 KB
testcase_46 AC 94 ms
71,588 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