結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー hiroHoshiihiroHoshii
提出日時 2020-06-10 21:42:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 598 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 86,608 KB
実行使用メモリ 81,556 KB
最終ジャッジ日時 2023-09-05 21:21:21
合計ジャッジ時間 4,737 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 196 ms
81,512 KB
testcase_01 AC 197 ms
81,408 KB
testcase_02 AC 198 ms
81,556 KB
testcase_03 AC 198 ms
81,400 KB
testcase_04 AC 196 ms
81,380 KB
testcase_05 AC 203 ms
81,352 KB
testcase_06 AC 193 ms
81,420 KB
testcase_07 AC 191 ms
81,320 KB
testcase_08 AC 197 ms
81,420 KB
testcase_09 AC 192 ms
81,404 KB
testcase_10 AC 192 ms
81,236 KB
testcase_11 AC 197 ms
81,340 KB
testcase_12 AC 199 ms
81,424 KB
testcase_13 AC 198 ms
81,432 KB
testcase_14 AC 198 ms
81,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()

    x = [[1,1],[1,0]]
    y = [[1,0],[0,1]]
    n = N - 3

    def mul(a, b):
        z = [[0,0],[0,0]]
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    z[i][j] = (z[i][j] + a[i][k] * b[k][j]) % M
        return z

    while n > 0:
        if n % 2:
            y = mul(x,y)
        x = mul(x, x)
        n //= 2

    print(sum(y[0]) % M)

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