結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー ayaoniayaoni
提出日時 2020-08-12 13:48:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 690 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 82,460 KB
実行使用メモリ 53,760 KB
最終ジャッジ日時 2024-04-18 00:25:56
合計ジャッジ時間 1,353 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,216 KB
testcase_01 AC 36 ms
52,344 KB
testcase_02 AC 38 ms
52,772 KB
testcase_03 AC 36 ms
52,924 KB
testcase_04 AC 36 ms
52,816 KB
testcase_05 AC 41 ms
53,664 KB
testcase_06 AC 38 ms
52,944 KB
testcase_07 AC 37 ms
53,760 KB
testcase_08 AC 37 ms
52,572 KB
testcase_09 AC 36 ms
53,544 KB
testcase_10 AC 38 ms
53,684 KB
testcase_11 AC 38 ms
53,744 KB
testcase_12 AC 38 ms
53,448 KB
testcase_13 AC 39 ms
53,356 KB
testcase_14 AC 39 ms
52,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())


N,M = MI()


def matrix_multiplication_mod(A,B,p):
    l = len(A)
    m = len(B)
    n = len(B[0])
    C = [[0]*n for _ in range(l)]
    for i in range(l):
        for j in range(n):
            C[i][j] = sum((A[i][k]*B[k][j]) % p for k in range(m)) % p
    return C

def matrix_power_mod(A,n,p):
    l = len(A)
    C = [[0] * l for _ in range(l)]
    for i in range(l):
        C[i][i] = 1
    while n > 0:
        if n % 2 == 1:
            C = matrix_multiplication_mod(C,A,p)
        A = matrix_multiplication_mod(A,A,p)
        n >>= 1
    return C


A = [[1,1],[1,0]]
print(matrix_power_mod(A,N-2,M)[0][0])
0