結果

問題 No.980 Fibonacci Convolution Hard
ユーザー Eki1009Eki1009
提出日時 2020-11-01 18:19:00
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,360 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 87,276 KB
実行使用メモリ 96,040 KB
最終ジャッジ日時 2023-09-29 11:20:01
合計ジャッジ時間 9,437 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 sys
sys.setrecursionlimit(10**7)
mod = 10**9+7

def convolution(A, B):
    la = len(A)
    lb = len(B)
    res = [0]*(la+lb-1)
    for i, a in enumerate(A):
        for j, b in enumerate(B):
            res[i+j] += a*b
            res[i+j] %= mod
    return res

def poly_mod(X, Q):
    a = len(X)
    b = len(Q)
    if a < b:
        return X
    for i in range(a-b, -1, -1):
        d = X[i+b-1]
        for j, q in enumerate(Q):
            X[i+j] -= q*d
            X[i+j] %= mod
    return X[:b-1]

def poly_pow(C, Q, n):
    if n == 1:
        return C
    d = len(C)
    if n%2:
        res = convolution(poly_pow(C, Q, n-1), C)
    else:
        T = poly_pow(C, Q, n//2)
        res = convolution(T, T)
    res = poly_mod(res, Q)
    return res
        
#a_{n} = c_{1}*a_{n-1} + ... + c_{d}*a_{n-d} の第n項を求める(0~d-1項はE_{i}とする)
def rec_fomula(C, E, n):
    d = len(C) 
    Q = [1]*(d+1)
    for i, c in enumerate(C, 1):
        Q[i] = -c
    P = convolution(Q[:-1], E)[:d]
    inv = pow(Q[-1], mod-2, mod)
    norm_Q = [q*inv%mod for q in Q]
    X = poly_pow(C, norm_Q, n)
    res = convolution(X, P)
    res = poly_mod(res, norm_Q)
    return res[0]
    

p = int(input())
C = [2*p, 2-p**2, -2*p, -1]
E = [0, 0, 0, 1]
q = int(input())
for _ in range(q):
    n = int(input())
    ans = rec_fomula(C, E, n-1)
    print(ans)
0