結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kutaragi36kutaragi36
提出日時 2023-05-28 04:45:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 762 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 71,624 KB
最終ジャッジ日時 2023-08-27 03:47:16
合計ジャッジ時間 2,344 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,340 KB
testcase_01 AC 69 ms
71,380 KB
testcase_02 AC 71 ms
71,448 KB
testcase_03 AC 75 ms
71,284 KB
testcase_04 AC 70 ms
71,264 KB
testcase_05 AC 71 ms
71,624 KB
testcase_06 AC 70 ms
71,620 KB
testcase_07 AC 71 ms
71,376 KB
testcase_08 AC 71 ms
71,264 KB
testcase_09 AC 72 ms
71,448 KB
testcase_10 AC 72 ms
71,464 KB
testcase_11 AC 73 ms
71,528 KB
testcase_12 AC 71 ms
71,460 KB
testcase_13 AC 71 ms
71,352 KB
testcase_14 AC 71 ms
71,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, MOD = map(int, input().split())


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



F = [[1, 1],
     [1, 0]]

A = [[1],
     [0]]

F = power(F, N-2)
ans = mul(F, A)
print(ans[0][0])
0