結果

問題 No.206 数の積集合を求めるクエリ
ユーザー ygd.ygd.
提出日時 2021-03-28 13:42:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 872 ms / 7,000 ms
コード長 1,288 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 69,956 KB
最終ジャッジ日時 2024-05-06 22:38:54
合計ジャッジ時間 24,608 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 571 ms
43,604 KB
testcase_01 AC 544 ms
43,864 KB
testcase_02 AC 553 ms
43,612 KB
testcase_03 AC 545 ms
43,632 KB
testcase_04 AC 547 ms
43,736 KB
testcase_05 AC 547 ms
43,628 KB
testcase_06 AC 575 ms
43,612 KB
testcase_07 AC 582 ms
43,632 KB
testcase_08 AC 587 ms
43,632 KB
testcase_09 AC 567 ms
43,628 KB
testcase_10 AC 540 ms
43,756 KB
testcase_11 AC 581 ms
43,504 KB
testcase_12 AC 586 ms
43,480 KB
testcase_13 AC 582 ms
43,624 KB
testcase_14 AC 578 ms
43,636 KB
testcase_15 AC 591 ms
43,500 KB
testcase_16 AC 563 ms
43,500 KB
testcase_17 AC 777 ms
68,840 KB
testcase_18 AC 695 ms
65,264 KB
testcase_19 AC 743 ms
69,568 KB
testcase_20 AC 672 ms
65,440 KB
testcase_21 AC 790 ms
67,136 KB
testcase_22 AC 711 ms
66,916 KB
testcase_23 AC 735 ms
69,280 KB
testcase_24 AC 872 ms
69,956 KB
testcase_25 AC 842 ms
69,828 KB
testcase_26 AC 806 ms
65,312 KB
testcase_27 AC 751 ms
64,848 KB
testcase_28 AC 811 ms
67,468 KB
testcase_29 AC 799 ms
66,888 KB
testcase_30 AC 788 ms
64,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
#f,gは普通のリスト。
#C[i+j] = ΣF[i]*G[j]
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]


L,M,N = map(int,input().split())
A = list(map(int,input().split()))
A = [a-1 for a in A]
B = list(map(int,input().split()))
B = [b-1 for b in B]
Q = int(input())

MAX = pow(2,10*5) + 100
AL = [0]*N
BL = [0]*N
BL_inv = [0]*N
for i in range(L):
  AL[A[i]] = 1
for i in range(M):
  BL[B[i]] = 1
  BL_inv[N - 1 - B[i]] = 1
#print(AL)
#print(BL_inv)
P = convolve(AL,BL_inv)
#print(P)
for i in range(Q):
  print(P[i+N-1])
0