結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー 山本信二山本信二
提出日時 2022-05-23 23:10:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 591 bytes
コンパイル時間 133 ms
コンパイル使用メモリ 12,088 KB
実行使用メモリ 10,056 KB
最終ジャッジ日時 2023-10-20 17:45:50
合計ジャッジ時間 1,413 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,056 KB
testcase_01 AC 29 ms
10,056 KB
testcase_02 AC 29 ms
10,056 KB
testcase_03 AC 29 ms
10,056 KB
testcase_04 AC 29 ms
10,056 KB
testcase_05 AC 29 ms
10,056 KB
testcase_06 AC 29 ms
10,056 KB
testcase_07 AC 29 ms
10,056 KB
testcase_08 AC 29 ms
10,056 KB
testcase_09 AC 30 ms
10,056 KB
testcase_10 AC 30 ms
10,056 KB
testcase_11 AC 31 ms
10,056 KB
testcase_12 AC 30 ms
10,056 KB
testcase_13 AC 30 ms
10,056 KB
testcase_14 AC 29 ms
10,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
P = [[1, 1], [1, 0]]
A1 = [[0], [1]]
E = [[1, 0], [0, 1]]

def matrix_multi(A, B):
    res = [[0] * len(B[0]) for _ in range(len(A))]
    for i in range(len(A)):
        for j in range(len(B[0])):
            for k in range(len(A[0])):
                res[i][j] += A[i][k] * B[k][j]
                res[i][j] %= M
    return res

def matrix_power(P, x):
    if x == 0:return E
    res = matrix_power(P, x//2)
    res = matrix_multi(res, res)
    if x&1:res = matrix_multi(res, P)
    return res

ans = matrix_multi(matrix_power(P, N-1), A1)[0][0]
print(ans)
0