結果
問題 |
No.1307 Rotate and Accumulate
|
ユーザー |
![]() |
提出日時 | 2020-12-06 14:24:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 668 ms / 5,000 ms |
コード長 | 1,786 bytes |
コンパイル時間 | 186 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 302,300 KB |
最終ジャッジ日時 | 2024-09-17 13:06:05 |
合計ジャッジ時間 | 8,921 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
from math import pi, cos, sin import sys input = sys.stdin.readline class FFT: @classmethod def convolve(cls, f, g): size = len(f) + len(g) - 1 n = 1 while n < size: n *= 2 nf = f[:] + [0] * (n - len(f)) ng = g[:] + [0] * (n - len(g)) cls.fft(nf) cls.fft(ng) for i in range(n): nf[i] *= ng[i] cls.ifft(nf) ret = [0] * size for i in range(size): ret[i] = nf[i].real / n return ret @classmethod def fft(cls, f): n = len(f) m = n while m > 1: ang = 2 * pi / m omega = complex(cos(ang), sin(ang)) for s in range(n // m): w = 1 for i in range(m // 2): l = f[s * m + i] r = f[s * m + i + m // 2] f[s * m + i] = l + r f[s * m + i + m // 2] = (l - r) * w w *= omega m //= 2 @classmethod def ifft(cls, f): n = len(f) m = 2 while m <= n: ang = -2 * pi / m omega = complex(cos(ang), sin(ang)) for s in range(n // m): w = 1 for i in range(m // 2): l = f[s * m + i] r = f[s * m + i + m // 2] * w f[s * m + i] = l + r f[s * m + i + m // 2] = l - r w *= omega m *= 2 N, Q = map(int, input().split()) a = list(map(int, input().split())) r = list(map(int, input().split())) x = [0] * N for i in r: x[-i] += 1 ans = FFT.convolve(a, x) ans = list(map(round, ans)) for i in range(N, len(ans)): ans[i % N] += ans[i] print(*ans[:N])