結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー Chihaya_chanChihaya_chan
提出日時 2020-08-09 13:17:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 472 ms / 2,000 ms
コード長 771 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 44,468 KB
最終ジャッジ日時 2024-04-15 02:52:38
合計ジャッジ時間 9,534 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 460 ms
44,336 KB
testcase_01 AC 460 ms
44,468 KB
testcase_02 AC 463 ms
43,828 KB
testcase_03 AC 469 ms
43,960 KB
testcase_04 AC 464 ms
44,080 KB
testcase_05 AC 466 ms
44,076 KB
testcase_06 AC 471 ms
44,076 KB
testcase_07 AC 472 ms
44,204 KB
testcase_08 AC 464 ms
43,952 KB
testcase_09 AC 471 ms
43,696 KB
testcase_10 AC 467 ms
43,740 KB
testcase_11 AC 464 ms
43,952 KB
testcase_12 AC 467 ms
44,080 KB
testcase_13 AC 461 ms
44,300 KB
testcase_14 AC 459 ms
44,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())
import numpy as np
def matrix_power(A, N, mod):
    # returnA^N %mod in O(K**3 log N). (K is the size of A.)
    assert A.shape[0] == A.shape[1]
    K = A.shape[0]
    if N == 0:
        return np.eye(K, dtype=np.int64)
    else:
        if N % 2 == 0:
            mat = matrix_power(A, N//2, mod)
            return np.dot(mat, mat) % mod
        else:
            mat = matrix_power(A, N//2, mod)
            return np.dot(np.dot(mat, mat) % mod, A) % mod


def Fibonacci(N, mod):
    # return the n-th term of the fivonacci sequence  in O(logN).
    # F0=0,F1=1
    d = np.array([1, 0])
    A = np.array([[1, 1], [1, 0]], dtype=np.int64)
    res = np.dot(matrix_power(A, N, mod), d)
    return int(res[-1]) % mod

print(Fibonacci(N-1,M))
0