結果

問題 No.2272 多項式乗算 mod 258280327
ユーザー hotman78hotman78
提出日時 2023-02-04 16:04:09
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 609 ms / 2,000 ms
コード長 1,643 bytes
コンパイル時間 862 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 97,888 KB
最終ジャッジ日時 2023-09-16 12:44:21
合計ジャッジ時間 10,256 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
29,652 KB
testcase_01 AC 127 ms
29,560 KB
testcase_02 AC 127 ms
29,700 KB
testcase_03 AC 127 ms
29,744 KB
testcase_04 AC 131 ms
29,756 KB
testcase_05 AC 131 ms
29,796 KB
testcase_06 AC 129 ms
29,556 KB
testcase_07 AC 128 ms
29,572 KB
testcase_08 AC 130 ms
29,688 KB
testcase_09 AC 128 ms
29,804 KB
testcase_10 AC 132 ms
29,508 KB
testcase_11 AC 130 ms
29,592 KB
testcase_12 AC 134 ms
29,608 KB
testcase_13 AC 128 ms
29,384 KB
testcase_14 AC 127 ms
29,440 KB
testcase_15 AC 127 ms
29,768 KB
testcase_16 AC 128 ms
29,796 KB
testcase_17 AC 127 ms
29,716 KB
testcase_18 AC 132 ms
29,760 KB
testcase_19 AC 129 ms
29,692 KB
testcase_20 AC 129 ms
29,772 KB
testcase_21 AC 127 ms
29,700 KB
testcase_22 AC 127 ms
29,692 KB
testcase_23 AC 130 ms
29,580 KB
testcase_24 AC 133 ms
30,736 KB
testcase_25 AC 153 ms
34,140 KB
testcase_26 AC 152 ms
34,196 KB
testcase_27 AC 184 ms
39,684 KB
testcase_28 AC 180 ms
39,080 KB
testcase_29 AC 360 ms
62,604 KB
testcase_30 AC 605 ms
97,888 KB
testcase_31 AC 595 ms
96,228 KB
testcase_32 AC 609 ms
96,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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%BF
def 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.ndarray
        f,g の積
    """
    # h の長さ以上の n=2^k を計算
    fft_len = 1
    while 2 * fft_len < len(f) + len(g) - 1:
        fft_len *= 2
    fft_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)%p
  c=convolve(f2,g2)%p
  b=(convolve(f1+f2,g1+g2)-(a+c))%p
  h=(a<<30)+(b<<15)+c
  return h%p
n=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())))
0