結果
問題 | No.754 畳み込みの和 |
ユーザー |
|
提出日時 | 2024-04-08 16:33:28 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 718 ms / 5,000 ms |
コード長 | 1,621 bytes |
コンパイル時間 | 143 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 65,016 KB |
最終ジャッジ日時 | 2024-10-01 08:45:10 |
合計ジャッジ時間 | 5,493 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 3 |
ソースコード
import sysimport mathimport bisectfrom heapq import heapify, heappop, heappushfrom collections import deque, defaultdict, Counterfrom functools import lru_cachefrom itertools import accumulate, combinations, permutations, productsys.set_int_max_str_digits(10 ** 6)sys.setrecursionlimit(1000000)MOD = 10 ** 9 + 7MOD99 = 998244353input = lambda: sys.stdin.readline().strip()NI = lambda: int(input())NMI = lambda: map(int, input().split())NLI = lambda: list(NMI())SI = lambda: input()SMI = lambda: input().split()SLI = lambda: list(SMI())EI = lambda m: [NLI() for _ in range(m)]import numpy as npdef fft_convolve(f, g, MOD=MOD):"""数列 (多項式) f, g の畳み込みの計算.上下 15 bitずつ分けて計算することで,30 bit以下の整数,長さ 250000 程度の数列での計算が正確に行える."""fft = np.fft.rfftifft = np.fft.irfftLf = len(f)Lg = len(g)L = Lf + Lg - 1fft_len = 1 << L.bit_length()fl = f & (1 << 15) - 1fh = f >> 15gl = g & (1 << 15) - 1gh = g >> 15def conv(f, g):return ifft(fft(f, fft_len) * fft(g, fft_len))[:L]x = conv(fl, gl) % MODy = conv(fl + fh, gl + gh) % MODz = conv(fh, gh) % MODa, b, c = map(lambda x: (x + .5).astype(np.int64), [x, y, z])return (a + ((b - a - c) << 15) + (c << 30)) % MODdef main():N = NI()A = [NI() for _ in range(N+1)]B = [NI() for _ in range(N+1)]A = np.array(A)B = np.array(B)C = fft_convolve(A, B, MOD)[:N+1].sum() % MODprint(C)if __name__ == "__main__":main()