結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kokoa1046
提出日時 2017-09-25 09:03:24
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,059 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 21,632 KB
最終ジャッジ日時 2024-11-14 13:07:17
合計ジャッジ時間 17,635 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from functools import wraps
sys.setrecursionlimit(10000)

def tail_recursive(func):
    self_func = [func]
    self_firstcall = [True]
    self_CONTINUE = [object()]
    self_argskwd = [None]

    @wraps(func)
    def _tail_recursive(*args, **kwd):
        if self_firstcall[0] == True:
            func = self_func[0]
            CONTINUE = self_CONTINUE
            self_firstcall[0] = False
            try:
                while True:
                    result = func(*args, **kwd)
                    if result is CONTINUE:  # update arguments
                        args, kwd = self_argskwd[0]
                    else:  # last call
                        return result
            finally:
                self_firstcall[0] = True
        else:  # return the arguments of the tail call
            self_argskwd[0] = args, kwd
            return self_CONTINUE

    return _tail_recursive
@tail_recursive
def fibonacci(n, a=1, b=0):
    return b if n < 1 else fibonacci(n - 1, a + b, a)
n,m=map(int, input().split())
print(fibonacci(n-1)%m)
0