結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー 👑 rin204rin204
提出日時 2022-06-05 22:31:33
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,005 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 81,552 KB
実行使用メモリ 338,844 KB
最終ジャッジ日時 2023-10-21 03:19:53
合計ジャッジ時間 69,162 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,392 KB
testcase_01 AC 43 ms
55,392 KB
testcase_02 AC 43 ms
55,392 KB
testcase_03 AC 190 ms
80,628 KB
testcase_04 AC 176 ms
79,908 KB
testcase_05 AC 184 ms
80,136 KB
testcase_06 AC 172 ms
79,864 KB
testcase_07 AC 169 ms
79,328 KB
testcase_08 AC 177 ms
80,096 KB
testcase_09 AC 186 ms
80,540 KB
testcase_10 AC 161 ms
78,780 KB
testcase_11 AC 167 ms
79,312 KB
testcase_12 AC 156 ms
77,916 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 AC 43 ms
55,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
MOD = 998244353

class FFT:
    def __init__(self, MOD=998244353):
        FFT.MOD = MOD
        g = self.primitive_root_constexpr()
        ig = pow(g, FFT.MOD - 2, FFT.MOD)
        FFT.W = [pow(g, (FFT.MOD - 1) >> i, FFT.MOD) for i in range(30)]
        FFT.iW = [pow(ig, (FFT.MOD - 1) >> i, FFT.MOD) for i in range(30)]

    def primitive_root_constexpr(self):
        if FFT.MOD == 998244353:
            return 3
        elif FFT.MOD == 200003:
            return 2
        elif FFT.MOD == 167772161:
            return 3
        elif FFT.MOD == 469762049:
            return 3
        elif FFT.MOD == 754974721:
            return 11
        divs = [0] * 20
        divs[0] = 2
        cnt = 1
        x = (FFT.MOD - 1) // 2
        while x % 2 == 0:
            x //= 2
        i = 3
        while i * i <= x:
            if x % i == 0:
                divs[cnt] = i
                cnt += 1
                while x % i == 0:
                    x //= i
            i += 2
        if x > 1:
            divs[cnt] = x
            cnt += 1
        g = 2
        while 1:
            ok = True
            for i in range(cnt):
                if pow(g, (FFT.MOD - 1) // divs[i], FFT.MOD) == 1:
                    ok = False
                    break
            if ok:
                return g
            g += 1

    def fft(self, k, f):
        for l in range(k, 0, -1):
            d = 1 << l - 1
            U = [1]
            for i in range(d):
                U.append(U[-1] * FFT.W[l] % FFT.MOD)
            
            for i in range(1 << k - l):
                for j in range(d):
                    s = i * 2 * d + j
                    f[s], f[s + d] = (f[s] + f[s + d]) % FFT.MOD, U[j] * (f[s] - f[s + d]) % FFT.MOD

    def ifft(self, k, f):
        for l in range(1, k + 1):
            d = 1 << l - 1
            for i in range(1 << k - l):
                u = 1
                for j in range(i * 2 * d, (i * 2 + 1) * d):
                    f[j+d] *= u
                    f[j], f[j + d] = (f[j] + f[j + d]) % FFT.MOD, (f[j] - f[j + d]) % FFT.MOD
                    u = u * FFT.iW[l] % FFT.MOD

    def convolve(self, A, B):
        n0 = len(A) + len(B) - 1
        k = (n0).bit_length()
        n = 1 << k
        A += [0] * (n - len(A))
        B += [0] * (n - len(B))
        self.fft(k, A)
        self.fft(k, B)
        A = [a * b % FFT.MOD for a, b in zip(A, B)]
        self.ifft(k, A)
        inv = pow(n, FFT.MOD - 2, FFT.MOD)
        A = [a * inv % FFT.MOD for a in A]
        del A[n0:]
        return A

n, Q = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
fft = FFT()
queue = deque()
poly = [None] * n
for i, a in enumerate(A):
    poly[i] = [(a - 1) % MOD, 1]
    queue.append(i)

while len(queue) >= 2:
    i = queue.popleft()
    j = queue.popleft()
    poly[i] = fft.convolve(poly[i], poly[j])
    queue.append(i)
    
i = queue.popleft()
F = poly[i]
for b in B:
    print(F[b])

0