結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー AT274_AT274_
提出日時 2021-04-04 14:14:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 883 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 10,980 KB
実行使用メモリ 8,300 KB
最終ジャッジ日時 2023-08-27 15:12:18
合計ジャッジ時間 1,558 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,300 KB
testcase_01 AC 15 ms
7,800 KB
testcase_02 AC 14 ms
8,124 KB
testcase_03 AC 15 ms
7,764 KB
testcase_04 AC 15 ms
7,772 KB
testcase_05 AC 15 ms
7,824 KB
testcase_06 AC 16 ms
7,764 KB
testcase_07 AC 15 ms
7,760 KB
testcase_08 AC 14 ms
7,792 KB
testcase_09 AC 14 ms
7,892 KB
testcase_10 AC 14 ms
7,792 KB
testcase_11 AC 14 ms
7,788 KB
testcase_12 AC 14 ms
7,764 KB
testcase_13 AC 15 ms
7,864 KB
testcase_14 AC 14 ms
7,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
N -= 1
MOD = M

def calc_matrix_product(A, B):
    if not len(A[0]) == len(B):
        raise Exception("Invalid arguments. A_col: {} B_row: {}".format(len(A), len(B)))

    C = [[0] * len(B[0]) for _ in range(len(A))]
    for i in range(len(A)):
        for j in range(len(B)):
            for k in range(len(B[0])):
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD

    return C


def calc_matrix_pow(A, n):
    if not len(A) == len(A[0]):
        raise Exception("Invalid arguments. A_row: {} A_col: {}".format(len(A), len(A[0])))

    B = [[0] * len(A) for _ in range(len(A))]
    for i in range(len(A)):
        B[i][i] = 1

    while n:
        if n & 1:
            B = calc_matrix_product(B, A)
        A = calc_matrix_product(A, A)
        n = n >> 1

    return B


A = [[1, 1], [1, 0]]
print(calc_matrix_pow(A, N)[1][0] % MOD)
0