結果
問題 | No.1068 #いろいろな色 / Red and Blue and more various colors (Hard) |
ユーザー |
![]() |
提出日時 | 2021-05-09 00:23:36 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,889 ms / 3,500 ms |
コード長 | 1,826 bytes |
コンパイル時間 | 137 ms |
コンパイル使用メモリ | 82,556 KB |
実行使用メモリ | 250,972 KB |
最終ジャッジ日時 | 2024-09-17 08:05:19 |
合計ジャッジ時間 | 36,917 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
ROOT = 3 MOD = 998244353 roots = [pow(ROOT,(MOD-1)>>i,MOD) for i in range(24)] # 1 の 2^i 乗根 iroots = [pow(x,MOD-2,MOD) for x in roots] # 1 の 2^i 乗根の逆元 def untt(a,n): for i in range(n): m = 1<<(n-i-1) for s in range(1<<i): w_N = 1 s *= m*2 for p in range(m): a[s+p], a[s+p+m] = (a[s+p]+a[s+p+m])%MOD, (a[s+p]-a[s+p+m])*w_N%MOD w_N = w_N*roots[n-i]%MOD def iuntt(a,n): for i in range(n): m = 1<<i for s in range(1<<(n-i-1)): w_N = 1 s *= m*2 for p in range(m): a[s+p], a[s+p+m] = (a[s+p]+a[s+p+m]*w_N)%MOD, (a[s+p]-a[s+p+m]*w_N)%MOD w_N = w_N*iroots[i+1]%MOD inv = pow((MOD+1)//2,n,MOD) for i in range(1<<n): a[i] = a[i]*inv%MOD def convolution(a,b): la = len(a) lb = len(b) if min(la, lb) <= 50: if la < lb: la,lb = lb,la a,b = b,a res = [0]*(la+lb-1) for i in range(la): for j in range(lb): res[i+j] += a[i]*b[j] res[i+j] %= MOD return res deg = la+lb-2 n = deg.bit_length() N = 1<<n a += [0]*(N-len(a)) b += [0]*(N-len(b)) untt(a,n) untt(b,n) for i in range(N): a[i] = a[i]*b[i]%MOD iuntt(a,n) return a[:deg+1] def convolution_all(polys): if not polys: return [1] from collections import deque q = deque(polys) for _ in range(len(polys)-1): a = q.popleft() b = q.popleft() q.append(convolution(a,b)) return q[0] n,q = map(int,input().split()) *a, = map(int,input().split()) q = [[ai-1,1] for ai in a] dp = convolution_all(q) *b, = map(int,input().split()) for i in b: print(dp[i])