結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー ninja-kidninja-kid
提出日時 2022-11-19 09:56:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 81,660 KB
実行使用メモリ 55,548 KB
最終ジャッジ日時 2023-10-20 15:55:06
合計ジャッジ時間 2,408 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#!/usr/bin/env python3

from bisect import bisect, bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush
from itertools import accumulate, combinations, combinations_with_replacement, product
from math import atan, cos, degrees, factorial, gcd, inf, pi


mod1 = 10**9 + 7
mod2 = 998244353
dpos4 = ((1, 0), (0, 1), (-1, 0), (0, -1))
dpos8 = ((0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1))


def main():
    N, M = map(int, input().split())

    def matrix_prod(A, B, mod=0):
        ret = [[0] * len(B[0]) for _ in range(len(A))]
        for i in range(len(A)):
            for j in range(len(B[0])):
                for k in range(len(B)):
                    ret[i][j] += A[i][k] * B[k][j]
                    if mod > 0:
                        ret[i][j] %= mod
        return ret

    def matrix_pow(A, K, mod=0):
        if K == 0:
            I = [[0] * len(A[0]) for _ in range(len(A))]
            for i in range(len(A)):
                I[i][i] = 1
            return I
        ret = matrix_pow(matrix_prod(A, A, mod), K // 2, mod)
        if K % 2 == 1:
            ret = matrix_prod(ret, A, mod)
        return ret

    A = [[1, 1], [1, 0]]
    val = matrix_pow(A, N - 1, M)
    print(val[1][0])


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