結果

問題 No.891 隣接3項間の漸化式
ユーザー brthyyjp
提出日時 2021-01-02 11:46:42
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 503 bytes
コンパイル時間 117 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 44,044 KB
最終ジャッジ日時 2024-10-12 01:36:57
合計ジャッジ時間 25,050 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other AC * 8 RE * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

#A**n
def mat_pow(A, n, mod):
    size = len(A)
    res = np.eye(size, dtype=np.object)
    while n > 0:
        if n & 1 == 1:
            res = res @ A
            res %= mod
        A = A @ A
        A %= mod
        n = n>>1
    return res

# https://yukicoder.me/problems/no/891
a, b, n = map(int, input().split())
x = np.array([[1], [0]])
mod = 10**9+7

if n <= 1:
    print(x[1-n][0]%mod)
    exit()

A = np.array([[a, b], [1, 0]])
A = mat_pow(A, n-1, mod)
print((A@x)[0][0])
0