結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー 👑 rin204rin204
提出日時 2021-08-18 00:28:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 661 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 54,016 KB
最終ジャッジ日時 2024-04-19 08:35:05
合計ジャッジ時間 1,807 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,760 KB
testcase_01 AC 42 ms
53,248 KB
testcase_02 AC 44 ms
53,504 KB
testcase_03 AC 54 ms
53,376 KB
testcase_04 AC 50 ms
53,248 KB
testcase_05 AC 50 ms
53,248 KB
testcase_06 AC 53 ms
53,504 KB
testcase_07 AC 47 ms
53,504 KB
testcase_08 AC 50 ms
53,376 KB
testcase_09 AC 50 ms
53,632 KB
testcase_10 AC 51 ms
54,016 KB
testcase_11 AC 50 ms
53,632 KB
testcase_12 AC 47 ms
53,504 KB
testcase_13 AC 53 ms
53,888 KB
testcase_14 AC 48 ms
53,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from copy import deepcopy

def matpow(A, B, w):
    l = len(A)
    while w:
        if w & 1:
            C = [0] * l
            for i in range(l):
                for j in range(l):
                    C[i] += A[i][j] * B[j]
                    C[i] %= MOD
            B = C.copy()
        C = [[0] * l for _ in range(l)]
        for i in range(l):
            for j in range(l):
                for k in range(l):
                    C[i][j] += A[i][k] * A[k][j]
                    C[i][j] %= MOD
        A = deepcopy(C)
        w >>= 1
    return B
    
n, MOD = map(int, input().split())
A = [[1, 1], [1, 0]]
B = [1, 0]
C = matpow(A, B, n - 1)
print(C[1])
0