結果

問題 No.980 Fibonacci Convolution Hard
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-10 16:46:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,235 bytes
コンパイル時間 692 ms
コンパイル使用メモリ 11,868 KB
実行使用メモリ 408,540 KB
最終ジャッジ日時 2023-10-20 00:48:32
合計ジャッジ時間 9,611 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7


def convolve(A, B):
    n = len(A) + len(B) - 1
    fftlen = 1 << (n-1).bit_length()
    fa = np.fft.rfft(A, fftlen)
    fb = np.fft.rfft(B, fftlen)
    inv = (np.fft.irfft(fa * fb, fftlen) + 0.5).astype(np.int64)
    return inv[:n]


def mod_convolve_10(A, B, mod):
    a1, a2 = np.divmod(A, 1 << 20)
    a2, a3 = np.divmod(a2, 1 << 10)
    b1, b2 = np.divmod(B, 1 << 20)
    b2, b3 = np.divmod(b2, 1 << 10)
    x = convolve(a1, b1) % mod
    y = convolve(a2, b2) % mod
    z = convolve(a3, b3) % mod
    xy = (convolve(a1 + a2, b1 + b2) - (x + y)) % mod
    yz = (convolve(a2 + a3, b2 + b3) - (y + z)) % mod
    zx = (convolve(a3 + a1, b3 + b1) - (z + x)) % mod
    res = ((((x << 20) % mod) << 20) +
           (((xy << 20) % mod) << 10) +
           ((y + zx) << 20) +
           (yz << 10) % mod + z) % mod
    return res


p = int(input())
Q = int(input())
query = np.array([input() for _ in range(Q)], dtype=np.intc)
U = 2 * 10 ** 6

A = [0, 1]
for _ in range(2, U+1):
    A.append((p * A[-1] + A[-2]) % mod)
A = np.array(A, dtype=np.int64)
B = mod_convolve_10(A, A, mod)

ans = B[query - 2]
print(*ans, sep="\n")
0