結果

問題 No.891 隣接3項間の漸化式
ユーザー H3PO4H3PO4
提出日時 2023-06-17 20:21:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 613 bytes
コンパイル時間 132 ms
コンパイル使用メモリ 10,828 KB
実行使用メモリ 29,784 KB
最終ジャッジ日時 2023-09-07 16:20:39
合計ジャッジ時間 8,926 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
29,656 KB
testcase_01 AC 127 ms
29,776 KB
testcase_02 AC 130 ms
29,636 KB
testcase_03 AC 129 ms
29,668 KB
testcase_04 AC 131 ms
29,584 KB
testcase_05 AC 129 ms
29,768 KB
testcase_06 AC 129 ms
29,616 KB
testcase_07 AC 128 ms
29,640 KB
testcase_08 AC 128 ms
29,664 KB
testcase_09 AC 129 ms
29,596 KB
testcase_10 AC 133 ms
29,664 KB
testcase_11 AC 132 ms
29,588 KB
testcase_12 AC 133 ms
29,688 KB
testcase_13 AC 127 ms
29,640 KB
testcase_14 AC 128 ms
29,600 KB
testcase_15 AC 127 ms
29,652 KB
testcase_16 AC 129 ms
29,596 KB
testcase_17 AC 132 ms
29,436 KB
testcase_18 AC 127 ms
29,636 KB
testcase_19 AC 128 ms
29,608 KB
testcase_20 AC 127 ms
29,624 KB
testcase_21 AC 127 ms
29,440 KB
testcase_22 AC 126 ms
29,636 KB
testcase_23 AC 128 ms
29,636 KB
testcase_24 AC 128 ms
29,680 KB
testcase_25 AC 127 ms
29,512 KB
testcase_26 AC 127 ms
29,652 KB
testcase_27 AC 127 ms
29,560 KB
testcase_28 AC 128 ms
29,656 KB
testcase_29 AC 130 ms
29,540 KB
testcase_30 AC 129 ms
29,500 KB
testcase_31 AC 128 ms
29,664 KB
testcase_32 AC 128 ms
29,580 KB
testcase_33 AC 129 ms
29,540 KB
testcase_34 AC 130 ms
29,580 KB
testcase_35 AC 128 ms
29,592 KB
testcase_36 AC 130 ms
29,644 KB
testcase_37 AC 131 ms
29,508 KB
testcase_38 AC 133 ms
29,784 KB
testcase_39 AC 131 ms
29,644 KB
testcase_40 AC 134 ms
29,656 KB
testcase_41 AC 128 ms
29,652 KB
権限があれば一括ダウンロードができます

ソースコード

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