結果

問題 No.891 隣接3項間の漸化式
ユーザー k-k
提出日時 2019-09-21 17:07:00
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 613 ms / 2,000 ms
コード長 737 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 44,484 KB
最終ジャッジ日時 2024-09-19 02:48:10
合計ジャッジ時間 27,379 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

MOD = 10 ** 9 + 7
def pow(A, n):
    if n == 0:
        return np.eye(len(A))
    if n == 1:
        return A % MOD
    elif n % 2 == 0:
        return pow(np.dot(A, A) % MOD, n // 2)
    else:
        return np.dot(A, pow(A, n - 1)) % MOD

def calc(a, b, n):
    """
    x_n を計算する
    """
    A = np.array([
        [a, b],
        [1, 0]
    ], dtype="uint64")
    s = np.array([
        [1],
        [0]
    ], dtype="uint64")
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        sq = pow(A, n - 1)
        return np.dot(sq, s)[0][0]

if __name__ == "__main__":
    input_line = input().split(" ")
    print(calc(int(input_line[0]), int(input_line[1]), int(input_line[2])))
0