結果

問題 No.891 隣接3項間の漸化式
ユーザー H3PO4
提出日時 2023-06-17 20:21:23
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 509 ms / 2,000 ms
コード長 613 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 44,604 KB
最終ジャッジ日時 2024-06-25 10:15:59
合計ジャッジ時間 23,441 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

A, B, N = map(int, input().split())
MOD = 10 ** 9 + 7


def pow_matrix_mod(x, n, mod=MOD):
    x = x.astype(object)
    if not n:
        return np.eye(len(x), dtype=object)
    if n % 2 == 0:
        return pow_matrix_mod(x @ x % mod, n // 2) % mod
    else:
        return x @ pow_matrix_mod(x @ x % mod, (n - 1) // 2) % mod


if A == 0 and B == 0:
    ans = 1 if N == 1 else 0
elif A == 0:
    ans = pow(B, N // 2, MOD) if N % 2 else 0
elif B == 0:
    ans = pow(A, N - 1, MOD) if N else 0
else:
    X = np.array([[0, 1], [B, A]], dtype=int)
    ans = pow_matrix_mod(X, N)[0, 1]
print(ans)
0