結果

問題 No.3228 Very Large Fibonacci Sum
ユーザー timi
提出日時 2025-08-08 22:43:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 860 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 82,600 KB
実行使用メモリ 64,044 KB
最終ジャッジ日時 2025-08-08 22:43:44
合計ジャッジ時間 2,325 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

a,b,c,d,e,N=map(int, input().split())

mod=10**9+7
if N==0:
  print(a%mod)
  exit()
if N==1:
  print((a+b)%mod)
  exit()
if N==2:
  ans=a+b+c*b+a*d+e
  print(ans%mod)
  exit()
  
def matmul(A, B):
    Ah, Bh, Bw = len(A), len(B), len(B[0])
    C = [[0 for _ in range(Bw)] for _ in range(Ah)]
    for i in range(Ah):
        for j in range(Bw):
            for k in range(Bh):
                C[i][j] += A[i][k] * B[k][j] % mod
                C[i][j] %= mod
    return C

# Mのk乗を効率的に計算する
def doubling(M, k):
    k -= 1
    Mc = M.copy()
    while k > 0:
        if k & 1 == 1:
            Mc = matmul(Mc, M)
        M = matmul(M, M) # Mの(2のi乗)の乗 を計算する
        k >>= 1
    return Mc 
 
M = [[c+1,-c+d,-d,e], [1,0,0,0],[0,1,0,0],[0,0,0,1]]
F = [[a+b], [a],[0],[1]]
Mn = doubling(M, N-1)
G = matmul(Mn, F)
print(G[0][0])
0