結果

問題 No.1307 Rotate and Accumulate
ユーザー sotanishysotanishy
提出日時 2020-12-06 14:24:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 671 ms / 5,000 ms
コード長 1,786 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 81,728 KB
実行使用メモリ 301,572 KB
最終ジャッジ日時 2023-10-17 15:24:06
合計ジャッジ時間 9,803 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,880 KB
testcase_01 AC 38 ms
53,880 KB
testcase_02 AC 38 ms
53,880 KB
testcase_03 AC 40 ms
53,880 KB
testcase_04 AC 50 ms
62,584 KB
testcase_05 AC 48 ms
62,584 KB
testcase_06 AC 40 ms
53,880 KB
testcase_07 AC 38 ms
53,880 KB
testcase_08 AC 635 ms
298,028 KB
testcase_09 AC 624 ms
298,340 KB
testcase_10 AC 305 ms
144,560 KB
testcase_11 AC 289 ms
146,616 KB
testcase_12 AC 297 ms
143,876 KB
testcase_13 AC 100 ms
87,900 KB
testcase_14 AC 165 ms
97,584 KB
testcase_15 AC 659 ms
279,700 KB
testcase_16 AC 665 ms
279,696 KB
testcase_17 AC 671 ms
279,704 KB
testcase_18 AC 662 ms
301,572 KB
testcase_19 AC 657 ms
283,968 KB
testcase_20 AC 661 ms
283,932 KB
testcase_21 AC 38 ms
53,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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])
0