結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー Eki1009Eki1009
提出日時 2020-11-01 19:06:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,282 bytes
コンパイル時間 110 ms
コンパイル使用メモリ 10,936 KB
実行使用メモリ 8,444 KB
最終ジャッジ日時 2023-09-29 11:22:26
合計ジャッジ時間 1,529 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,384 KB
testcase_01 AC 16 ms
8,428 KB
testcase_02 AC 16 ms
8,260 KB
testcase_03 AC 17 ms
8,376 KB
testcase_04 AC 16 ms
8,268 KB
testcase_05 AC 17 ms
8,272 KB
testcase_06 AC 17 ms
8,380 KB
testcase_07 AC 16 ms
8,252 KB
testcase_08 AC 15 ms
8,400 KB
testcase_09 AC 15 ms
8,252 KB
testcase_10 AC 16 ms
8,264 KB
testcase_11 AC 15 ms
8,400 KB
testcase_12 AC 17 ms
8,284 KB
testcase_13 AC 16 ms
8,444 KB
testcase_14 AC 16 ms
8,324 KB
testcase_15 AC 15 ms
8,252 KB
testcase_16 AC 16 ms
8,248 KB
testcase_17 AC 15 ms
8,284 KB
testcase_18 AC 16 ms
8,256 KB
testcase_19 AC 16 ms
8,380 KB
testcase_20 AC 16 ms
8,420 KB
testcase_21 AC 16 ms
8,248 KB
testcase_22 AC 16 ms
8,368 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