結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー lemondolemondo
提出日時 2021-01-03 21:34:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 787 bytes
コンパイル時間 1,364 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 52,864 KB
最終ジャッジ日時 2024-04-21 16:34:47
合計ジャッジ時間 2,493 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M = mapint()
# 行列累乗
memo = [0]*60
memo[0] = [[1, 1], [1, 0]]

def dot(mat1, mat2, mod):
    row1, col1 = len(mat1), len(mat1[0])
    row2, col2 = len(mat2), len(mat2[0])
    if col1!=row2:
        return False
    ret = [[0]*col2 for _ in range(row1)]
    for r1 in range(row1):
        for c1 in range(col1):
            for c2 in range(col2):
                ret[r1][c2] += mat1[r1][c1]*mat2[c1][c2]
                ret[r1][c2] %= mod
    return ret

for i in range(1, 60):
    memo[i] = dot(memo[i-1], memo[i-1], M)

base = [[1, 0], [0, 1]]
for i in range(60):
    if ((N-1)>>i)&1:
        base = dot(memo[i], base, M)
print(base[1][0])
0