結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー Eki1009Eki1009
提出日時 2020-11-01 19:06:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 572 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 71,612 KB
最終ジャッジ日時 2023-09-29 11:22:24
合計ジャッジ時間 3,782 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,600 KB
testcase_01 AC 71 ms
71,316 KB
testcase_02 AC 70 ms
71,292 KB
testcase_03 AC 71 ms
71,496 KB
testcase_04 AC 69 ms
71,404 KB
testcase_05 AC 69 ms
71,208 KB
testcase_06 AC 70 ms
71,276 KB
testcase_07 AC 70 ms
71,580 KB
testcase_08 AC 70 ms
71,596 KB
testcase_09 AC 70 ms
71,400 KB
testcase_10 AC 71 ms
71,200 KB
testcase_11 AC 70 ms
71,284 KB
testcase_12 AC 71 ms
71,260 KB
testcase_13 AC 70 ms
71,292 KB
testcase_14 AC 71 ms
71,284 KB
testcase_15 AC 70 ms
71,068 KB
testcase_16 AC 70 ms
71,228 KB
testcase_17 AC 70 ms
71,552 KB
testcase_18 AC 71 ms
71,404 KB
testcase_19 AC 70 ms
71,112 KB
testcase_20 AC 68 ms
71,204 KB
testcase_21 AC 69 ms
71,612 KB
testcase_22 AC 69 ms
71,408 KB
権限があれば一括ダウンロードができます

ソースコード

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]
    
n = int(input())
C = [3, 0, -3, 1]
E = [0, 1, 2, 6]
ans = rec_fomula(C, E, n)
print(ans)
0