結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー toyuzuko
提出日時 2020-05-15 21:21:19
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 655 bytes
コンパイル時間 105 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-09-19 08:18:07
合計ジャッジ時間 1,116 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())

def power(base, exp, op=lambda x, y: x * y, ie=1):
    res = ie
    while exp:
        if exp & 1:
            res = op(res, base)
        base = op(base, base)
        exp >>= 1
    return res

def fibonacci(n, mod=1000000007):
    I = (1, 0, 0, 1)
    A = (1, 1, 1, 0)
    def mmul(x, y):
        x00, x01, x10, x11 = x
        y00, y01, y10, y11 = y
        z00 = (x00 * y00 + x01 * y10) % mod
        z01 = (x00 * y01 + x01 * y11) % mod
        z10 = (x10 * y00 + x11 * y10) % mod
        z11 = (x10 * y01 + x11 * y11) % mod
        return z00, z01, z10, z11
    return power(A, n, mmul, I)[3]

print(fibonacci(N, M))
0