結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー nao_130913nao_130913
提出日時 2022-05-23 23:13:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 709 bytes
コンパイル時間 1,068 ms
コンパイル使用メモリ 81,568 KB
実行使用メモリ 53,440 KB
最終ジャッジ日時 2023-10-20 17:45:53
合計ジャッジ時間 2,311 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

def matmul(A, B, MOD):   # A,B: 行列
    res = [[0] * len(B[0]) for _ in [None] * len(A)]
    for i, resi in enumerate(res):
        for k, aik in enumerate(A[i]):
            for j,bkj in enumerate(B[k]):
                resi[j] += aik * bkj
                resi[j] %= MOD
    return res

def matpow(A, p, mod):   # A^p を mod で割った余り
    if p % 2:
        return matmul(A, matpow(A, p-1, mod), mod)
    elif p > 0:
        b = matpow(A, p//2 ,mod)
        return matmul(b, b, mod)
    else:
        return [[int(i==j) for j in range(len(A))] for i in range(len(A))]


N, M = map(int, input().split())
S = [[1], [0]]
mat = [[1, 1], [1, 0]]
T = matpow(mat, N - 2, M)
print(matmul(T, S, M)[0][0])
0