結果

問題 No.931 Multiplicative Convolution
ユーザー maspymaspy
提出日時 2023-05-12 21:12:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 409 ms / 2,000 ms
コード長 2,145 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 11,172 KB
実行使用メモリ 62,000 KB
最終ジャッジ日時 2023-08-19 03:33:31
合計ジャッジ時間 7,714 ms
ジャッジサーバーID
(参考情報)
judge15 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
29,908 KB
testcase_01 AC 127 ms
29,876 KB
testcase_02 AC 134 ms
29,884 KB
testcase_03 AC 129 ms
29,796 KB
testcase_04 AC 130 ms
29,880 KB
testcase_05 AC 125 ms
29,840 KB
testcase_06 AC 127 ms
30,280 KB
testcase_07 AC 165 ms
33,644 KB
testcase_08 AC 397 ms
61,888 KB
testcase_09 AC 337 ms
59,936 KB
testcase_10 AC 375 ms
59,988 KB
testcase_11 AC 339 ms
58,632 KB
testcase_12 AC 295 ms
51,292 KB
testcase_13 AC 399 ms
60,324 KB
testcase_14 AC 409 ms
62,000 KB
testcase_15 AC 405 ms
61,380 KB
testcase_16 AC 393 ms
60,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# 原子根のべき乗で書いて、多項式に帰着

import numpy as np

p = int(readline())
A = np.array([0] + readline().split(), np.int64)
B = np.array([0] + readline().split(), np.int64)

MOD = 998244353

def make_prime(U):
    is_prime = np.zeros(U,np.bool_)
    is_prime[2] = 1
    is_prime[3::2] = 1
    M = int(U**.5)+1
    for p in range(3,M,2):
        if is_prime[p]:
            is_prime[p*p::p+p] = 0
    return is_prime, is_prime.nonzero()[0]

def find_primitive_root(p):
    import random
    if p == 2:
        return 1
    _, primes = make_prime(p)
    e = p - 1
    div = [q for q in primes if not e % q]
    test_n = [e // q for q in div]
    while True:
        r = random.randint(1,p-1)
        if any(pow(r,int(n),p) == 1 for n in test_n):
            continue
        return r

def make_power(a, L, MOD=MOD):
    B = L.bit_length()
    x = np.empty((1<<B), np.int64)
    x[0] = 1
    for n in range(B):
        x[1<<n:1<<(n+1)] = x[:1<<n] * a % MOD
        a *= a; a %= MOD
    return x[:L]

r = find_primitive_root(p)
exp = make_power(r,p-1,p)
log = np.zeros(p,np.int32)
log[exp] = np.arange(p-1)

f = A[exp]; g = B[exp]

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

fg = fft_convolve(f,g)

L = len(fg); fg[0:L-(p-1)] += fg[p-1:L]; fg = fg[:p-1]; fg %= MOD

answer = fg[log[1:]]
print(' '.join(answer.astype(str)))
0