結果

問題 No.891 隣接3項間の漸化式
コンテスト
ユーザー H3PO4
提出日時 2023-06-17 20:21:23
言語 Python3
(3.14.3 + numpy 2.4.2 + scipy 1.17.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 521 ms / 2,000 ms
コード長 613 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 320 ms
コンパイル使用メモリ 20,956 KB
実行使用メモリ 35,336 KB
最終ジャッジ日時 2026-03-11 18:58:00
合計ジャッジ時間 23,678 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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