結果

問題 No.1068 #いろいろな色 / Red and Blue and more various colors (Hard)
ユーザー maspymaspy
提出日時 2020-07-11 07:56:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,886 ms / 3,500 ms
コード長 1,849 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 95,648 KB
最終ジャッジ日時 2024-04-20 16:34:35
合計ジャッジ時間 43,555 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 481 ms
44,400 KB
testcase_01 AC 455 ms
44,400 KB
testcase_02 AC 461 ms
44,400 KB
testcase_03 AC 480 ms
44,524 KB
testcase_04 AC 466 ms
44,396 KB
testcase_05 AC 470 ms
44,276 KB
testcase_06 AC 465 ms
44,400 KB
testcase_07 AC 479 ms
44,400 KB
testcase_08 AC 483 ms
44,396 KB
testcase_09 AC 479 ms
44,404 KB
testcase_10 AC 469 ms
44,528 KB
testcase_11 AC 458 ms
44,020 KB
testcase_12 AC 462 ms
44,144 KB
testcase_13 AC 1,882 ms
94,176 KB
testcase_14 AC 1,784 ms
93,632 KB
testcase_15 AC 1,806 ms
93,272 KB
testcase_16 AC 1,801 ms
93,204 KB
testcase_17 AC 1,792 ms
93,288 KB
testcase_18 AC 1,804 ms
94,356 KB
testcase_19 AC 1,799 ms
93,096 KB
testcase_20 AC 1,780 ms
93,568 KB
testcase_21 AC 1,825 ms
92,976 KB
testcase_22 AC 1,886 ms
94,540 KB
testcase_23 AC 1,817 ms
94,072 KB
testcase_24 AC 1,832 ms
93,716 KB
testcase_25 AC 1,807 ms
94,624 KB
testcase_26 AC 1,802 ms
95,648 KB
testcase_27 AC 1,801 ms
93,704 KB
testcase_28 AC 1,828 ms
95,048 KB
testcase_29 AC 1,788 ms
95,084 KB
testcase_30 AC 1,773 ms
95,300 KB
testcase_31 AC 470 ms
44,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import numpy as np

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

MOD = 998244353

def fft_convolve(f, g):
    """
    数列 (多項式) f, g の畳み込みの計算.上下 15 bitずつ分けて計算することで,
    30 bit以下の整数,長さ 250000 程度の数列での計算が正確に行える.
    """
    fft = np.fft.rfft
    ifft = np.fft.irfft
    Lf = f.shape[-1]
    Lg = g.shape[-1]
    L = Lf + Lg - 1
    fft_len = 1 << L.bit_length()
    fh, fl = f >> 15, f & (1 << 15) - 1
    gh, gl = g >> 15, g & (1 << 15) - 1

    def conv(f, g):
        Ff = fft(f, fft_len)
        Fg = fft(g, fft_len)
        h = ifft(Ff * Fg)
        return np.rint(h)[..., :L].astype(np.int64) % MOD

    x = conv(fl, gl)
    z = conv(fh, gh)
    y = conv(fl + fh, gl + gh) - x - z
    return (x + (y << 15) + (z << 30)) % MOD

def product_of_polynomials(polynomials):
    """Compute products of polynomials. 
    The length of polynomials must be the same. 
    
    Parameters
    ----------
    polynomials : np.ndarray
        2D array containing input polynomials. 
        The i-th row polynomials[i, :] is i-th polynomial. 
        
    Returns
    -------
    product : np.ndarray
        Product of input polynomials. 
    """
    polys = polynomials
    while len(polys) > 1:
        if len(polys) & 1:
            polys = np.pad(polys, ((0, 1), (0, 0)))
            polys[-1, 0] = 1
        P = polys[:len(polys) // 2]
        Q = polys[len(polys) // 2:]
        polys = fft_convolve(P, Q)
    return polys[0]

N, Q = map(int, readline().split())
A = np.array(readline().split(), np.int64)

A %= MOD
polys = np.empty((N, 2), np.int64)
polys[:, 1] = 1
polys[:, 0] = A - 1

P = product_of_polynomials(polys)

for q in map(int, read().split()):
    print(P[q])
0