結果
問題 | No.2272 多項式乗算 mod 258280327 |
ユーザー |
![]() |
提出日時 | 2023-02-04 16:04:09 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 954 ms / 2,000 ms |
コード長 | 1,643 bytes |
コンパイル時間 | 152 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 108,416 KB |
最終ジャッジ日時 | 2024-07-03 13:17:05 |
合計ジャッジ時間 | 20,023 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
import numpy as np# copy by https://maspypy.com/%E6%95%B0%E5%AD%A6%E3%83%BBnumpy-%E9%AB%98%E9%80%9F%E3%83%95%E3%83%BC%E3%83%AA%E3%82%A8%E5%A4%89%E6%8F%9Bfft%E3%81%AB%E3%82%88%E3%82%8B%E7%95%B3%E3%81%BF%E8%BE%BC%E3%81%BFdef convolve(f, g):"""多項式 f, g の積を計算する。Parameters----------f : np.ndarray (int64)f[i] に、x^i の係数が入っているg : np.ndarray (int64)g[i] に、x^i の係数が入っているReturns-------h : np.ndarrayf,g の積"""# h の長さ以上の n=2^k を計算fft_len = 1while 2 * fft_len < len(f) + len(g) - 1:fft_len *= 2fft_len *= 2# フーリエ変換Ff = np.fft.rfft(f, fft_len)Fg = np.fft.rfft(g, fft_len)# 各点積Fh = Ff * Fg# フーリエ逆変換h = np.fft.irfft(Fh, fft_len)# 小数になっているので、整数にまるめるh = np.rint(h).astype(np.int64)return h[:len(f) + len(g) - 1]def convolve2(f,g,p):f1, f2=np.divmod(f,1<<15)g1, g2=np.divmod(g,1<<15)a=convolve(f1,g1)%pc=convolve(f2,g2)%pb=(convolve(f1+f2,g1+g2)-(a+c))%ph=(a<<30)+(b<<15)+creturn h%pn=int(input())a=np.array(list(map(int,input().split())),np.int64)m=int(input())b=np.array(list(map(int,input().split())),np.int64)while a.size>=2 and a[-1]==0 :a.resize(a.size-1)while b.size>=2 and b[-1]==0 :b.resize(b.size-1)if np.all(a==np.zeros(1,np.int64)) or np.all(b==np.zeros(1,np.int64)) :print(0)print(0)exit()c=convolve2(a%258280327,b%258280327,258280327)print(c.size-1)print(' '.join(map(str,c.tolist())))