結果
| 問題 | No.754 畳み込みの和 | 
| コンテスト | |
| ユーザー |  maspy | 
| 提出日時 | 2020-01-01 18:21:39 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 723 ms / 5,000 ms | 
| コード長 | 1,038 bytes | 
| コンパイル時間 | 139 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 64,008 KB | 
| 最終ジャッジ日時 | 2024-11-22 13:14:31 | 
| 合計ジャッジ時間 | 4,033 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 3 | 
ソースコード
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
MOD = 10 ** 9 + 7
def fft_convolve(f, g, MOD = MOD):
    """
    数列 (多項式) f, g の畳み込みの計算.上下 15 bitずつ分けて計算することで,
    30 bit以下の整数,長さ 250000 程度の数列での計算が正確に行える.
    """
    fft = np.fft.rfft; ifft = np.fft.irfft
    Lf = len(f); Lg = len(g); L = Lf + Lg - 1
    fft_len = 1 << L.bit_length()
    fl = f & (1 << 15) - 1; fh = f >> 15
    gl = g & (1 << 15) - 1; gh = g >> 15
    conv = lambda f,g: ifft(fft(f,fft_len) * fft(g,fft_len))[:L]
    x = conv(fl, gl) % MOD
    y = conv(fl+fh, gl+gh) % MOD
    z = conv(fh, gh) % MOD
    a, b, c = map(lambda x: (x + .5).astype(np.int64), [x,y,z])
    return (a + ((b - a - c) << 15) + (c << 30)) % MOD
N = int(readline())
AB = np.array(read().split(),np.int64)
A = AB[:N+1]; B = AB[N+1:]
C = fft_convolve(A,B)[:N+1]
answer = C.sum() % MOD
print(answer)
            
            
            
        