結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kumatan400kumatan400
提出日時 2023-05-13 00:29:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 756 bytes
コンパイル時間 1,646 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 71,304 KB
最終ジャッジ日時 2023-08-19 06:53:41
合計ジャッジ時間 3,731 ms
ジャッジサーバーID
(参考情報)
judge9 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,128 KB
testcase_01 AC 70 ms
71,272 KB
testcase_02 AC 71 ms
71,304 KB
testcase_03 AC 74 ms
71,268 KB
testcase_04 AC 70 ms
71,208 KB
testcase_05 AC 72 ms
71,252 KB
testcase_06 AC 73 ms
71,024 KB
testcase_07 AC 75 ms
71,260 KB
testcase_08 AC 71 ms
71,284 KB
testcase_09 AC 72 ms
71,172 KB
testcase_10 AC 72 ms
71,108 KB
testcase_11 AC 71 ms
71,124 KB
testcase_12 AC 72 ms
71,056 KB
testcase_13 AC 73 ms
71,272 KB
testcase_14 AC 72 ms
70,844 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