結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー maspy
提出日時 2020-01-17 14:10:33
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 375 bytes
コンパイル時間 357 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-06-25 15:49:34
合計ジャッジ時間 1,410 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

def F(n):
    if n == 0:
        return 1,0
    a,b = F(n//2)
    a,b = a*a + b*b, 2*a*b + b*b
    a %= MOD
    b %= MOD
    if n&1:
        return b,a+b
    else:
        return a,b

answer = F(N)[0] % MOD

print(answer)
0