結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー ryu19ryu19
提出日時 2020-10-24 13:54:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 573 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 7,884 KB
最終ジャッジ日時 2023-09-28 20:24:26
合計ジャッジ時間 1,474 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,752 KB
testcase_01 AC 17 ms
7,752 KB
testcase_02 AC 17 ms
7,876 KB
testcase_03 AC 16 ms
7,792 KB
testcase_04 AC 16 ms
7,844 KB
testcase_05 AC 16 ms
7,752 KB
testcase_06 AC 16 ms
7,844 KB
testcase_07 AC 16 ms
7,780 KB
testcase_08 AC 16 ms
7,872 KB
testcase_09 AC 16 ms
7,884 KB
testcase_10 AC 16 ms
7,816 KB
testcase_11 AC 16 ms
7,752 KB
testcase_12 AC 16 ms
7,832 KB
testcase_13 AC 16 ms
7,764 KB
testcase_14 AC 17 ms
7,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())
if N < 3:
    print(N-1)
    exit()
N -= 2
a, b, c, d = [1, 0, 0, 1]
na, nb, nc, nd = [1, 1, 1, 0]
while N > 0:
    if N % 2 > 0:
        a, b, c, d = [na*a % M+nb*c % M, na*b %
                      M + nb * d % M, a * nc % M + c * nd % M, d * nd % M + b * nc % M]
        a %= M
        b %= M
        c %= M
        d %= M

    na, nb, nc, nd = [na*na % M+nb*nc % M, na*nb %
                      M + nb * nd % M, na * nc % M + nc * nd % M, nd * nd % M + nb * nc % M]
    na %= M
    nb %= M
    nc %= M
    nd %= M
    N //= 2
print(a)
0