結果

問題 No.1066 #いろいろな色 / Red and Blue and more various colors (Easy)
ユーザー 👑 rin204rin204
提出日時 2022-06-05 22:31:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 3,005 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 81,536 KB
実行使用メモリ 82,344 KB
最終ジャッジ日時 2023-10-21 03:18:39
合計ジャッジ時間 5,127 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,392 KB
testcase_01 AC 43 ms
55,392 KB
testcase_02 AC 44 ms
55,392 KB
testcase_03 AC 46 ms
55,392 KB
testcase_04 AC 44 ms
55,392 KB
testcase_05 AC 43 ms
55,392 KB
testcase_06 AC 44 ms
55,392 KB
testcase_07 AC 44 ms
55,392 KB
testcase_08 AC 189 ms
80,152 KB
testcase_09 AC 65 ms
68,224 KB
testcase_10 AC 211 ms
81,384 KB
testcase_11 AC 171 ms
79,768 KB
testcase_12 AC 168 ms
79,308 KB
testcase_13 AC 174 ms
79,832 KB
testcase_14 AC 177 ms
79,772 KB
testcase_15 AC 200 ms
81,616 KB
testcase_16 AC 166 ms
78,796 KB
testcase_17 AC 166 ms
78,768 KB
testcase_18 AC 199 ms
80,956 KB
testcase_19 AC 182 ms
79,680 KB
testcase_20 AC 61 ms
65,808 KB
testcase_21 AC 189 ms
80,600 KB
testcase_22 AC 64 ms
68,148 KB
testcase_23 AC 45 ms
55,392 KB
testcase_24 AC 45 ms
55,392 KB
testcase_25 AC 212 ms
81,956 KB
testcase_26 AC 214 ms
82,344 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