結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー wesoweeenwesoweeen
提出日時 2022-02-02 21:24:31
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,239 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 16,384 KB
最終ジャッジ日時 2024-06-11 09:36:47
合計ジャッジ時間 4,137 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import wraps

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

def fibonacci(n):

    @tail_recursive
    def fibonacci_recursive(k, fk, fk_1):
        if k == n:
            return fk
        else:
            return fibonacci_recursive(k + 1, fk_1, fk + fk_1)

    return fibonacci_recursive(0, 0, 1)

def main() -> None:
    N, M = map(int, input().split())
    print(fibonacci(N - 1) % M)


if __name__ == "__main__":
    main()
0