結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー lemondolemondo
提出日時 2020-12-04 22:52:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 732 bytes
コンパイル時間 1,897 ms
コンパイル使用メモリ 86,472 KB
実行使用メモリ 71,248 KB
最終ジャッジ日時 2023-10-13 12:05:04
合計ジャッジ時間 2,713 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,120 KB
testcase_01 AC 79 ms
71,136 KB
testcase_02 AC 75 ms
71,188 KB
testcase_03 AC 76 ms
71,060 KB
testcase_04 AC 75 ms
71,028 KB
testcase_05 AC 77 ms
71,208 KB
testcase_06 AC 76 ms
71,160 KB
testcase_07 AC 77 ms
71,036 KB
testcase_08 AC 77 ms
71,216 KB
testcase_09 AC 75 ms
70,996 KB
testcase_10 AC 74 ms
71,164 KB
testcase_11 AC 71 ms
70,812 KB
testcase_12 AC 74 ms
71,248 KB
testcase_13 AC 73 ms
70,980 KB
testcase_14 AC 74 ms
71,156 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):
    ret = [[0]*2 for _ in range(2)]
    ret[0][0] = (mat1[0][0]*mat2[0][0] + mat1[0][1]*mat2[1][0])%M
    ret[0][1] = (mat1[0][0]*mat2[0][1] + mat1[0][1]*mat2[1][1])%M
    ret[1][0] = (mat1[1][0]*mat2[0][0] + mat1[1][1]*mat2[1][0])%M
    ret[1][1] = (mat1[1][0]*mat2[0][1] + mat1[1][1]*mat2[1][1])%M
    return ret
    

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

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