結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kumatan400kumatan400
提出日時 2023-05-13 00:29:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 756 bytes
コンパイル時間 408 ms
コンパイル使用メモリ 82,824 KB
実行使用メモリ 52,736 KB
最終ジャッジ日時 2024-05-06 14:07:04
合計ジャッジ時間 1,812 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,096 KB
testcase_01 AC 36 ms
51,968 KB
testcase_02 AC 37 ms
52,480 KB
testcase_03 AC 36 ms
52,352 KB
testcase_04 AC 38 ms
52,352 KB
testcase_05 AC 39 ms
52,224 KB
testcase_06 AC 38 ms
52,224 KB
testcase_07 AC 38 ms
52,352 KB
testcase_08 AC 39 ms
52,096 KB
testcase_09 AC 38 ms
52,352 KB
testcase_10 AC 37 ms
52,736 KB
testcase_11 AC 37 ms
52,736 KB
testcase_12 AC 36 ms
52,224 KB
testcase_13 AC 38 ms
52,480 KB
testcase_14 AC 36 ms
52,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def mul(A, B):
    if len(A[0]) != len(B):
        return -1

    R = len(A)
    C = len(B[0])
    K = len(B)

    res = [[0]*C for _ in range(R)]
    for i in range(R):
        for j in range(C):
            for k in range(K):
                res[i][j] += A[i][k] * B[k][j]
                res[i][j] %= MOD #MODはグローバルにある
    return res


def power(A, x):
    if x == 0:
        N = len(A)
        res = [[0]*N for _ in range(N)]
        for i in range(N):
            res[i][i] = 1
        return res

    B = power(A, x//2)
    res = mul(B, B)
    if x % 2 == 1:
        res = mul(res, A)
    return res


N, MOD = map(int, input().split())
F = [[1, 1],
     [1, 0]]

A = [[1],
     [0]]

F = power(F, N-2)
A = mul(F, A)

print(A[0][0])
0