結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー toyuzukotoyuzuko
提出日時 2020-05-15 21:21:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 655 bytes
コンパイル時間 118 ms
コンパイル使用メモリ 11,900 KB
実行使用メモリ 10,148 KB
最終ジャッジ日時 2023-10-19 12:14:46
合計ジャッジ時間 1,364 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,148 KB
testcase_01 AC 30 ms
10,148 KB
testcase_02 AC 31 ms
10,148 KB
testcase_03 AC 28 ms
10,148 KB
testcase_04 AC 27 ms
10,148 KB
testcase_05 AC 28 ms
10,148 KB
testcase_06 AC 29 ms
10,148 KB
testcase_07 AC 28 ms
10,148 KB
testcase_08 AC 27 ms
10,148 KB
testcase_09 AC 28 ms
10,148 KB
testcase_10 AC 27 ms
10,148 KB
testcase_11 AC 27 ms
10,148 KB
testcase_12 AC 27 ms
10,148 KB
testcase_13 AC 28 ms
10,148 KB
testcase_14 AC 28 ms
10,148 KB
権限があれば一括ダウンロードができます

ソースコード

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