結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー nadarenadare
提出日時 2018-07-27 23:33:10
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,093 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 11,016 KB
実行使用メモリ 8,400 KB
最終ジャッジ日時 2023-09-18 14:43:22
合計ジャッジ時間 1,408 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,276 KB
testcase_01 AC 16 ms
8,324 KB
testcase_02 AC 16 ms
8,400 KB
testcase_03 AC 16 ms
8,268 KB
testcase_04 AC 17 ms
8,276 KB
testcase_05 AC 17 ms
8,280 KB
testcase_06 AC 16 ms
8,360 KB
testcase_07 AC 16 ms
8,356 KB
testcase_08 AC 17 ms
8,356 KB
testcase_09 AC 16 ms
8,304 KB
testcase_10 AC 16 ms
8,216 KB
testcase_11 AC 16 ms
8,060 KB
testcase_12 AC 16 ms
8,256 KB
testcase_13 AC 17 ms
8,348 KB
testcase_14 AC 16 ms
8,268 KB
testcase_15 AC 16 ms
8,268 KB
testcase_16 AC 17 ms
8,272 KB
testcase_17 AC 16 ms
8,272 KB
testcase_18 AC 16 ms
8,396 KB
testcase_19 AC 16 ms
8,356 KB
testcase_20 AC 16 ms
8,264 KB
testcase_21 AC 16 ms
8,344 KB
testcase_22 AC 16 ms
8,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
"""
https://kukuruku.co/post/the-nth-fibonacci-number-in-olog-n/
からO(logn)のフィボナッチ数求めるやつを持ってきて改造した
"""
MOD = 10**9+7
class MatrixFibonacci:
    
    Q = [[1, 1],
         [1, 0]]

    def __init__(self):
        self.__memo = {}

    def __multiply_matrices(self, M1, M2):
        """Matrices miltiplication
        (the matrices are expected in the form of a list of 2x2 size)."""

        a11 = M1[0][0]*M2[0][0] + M1[0][1]*M2[1][0]
        a12 = M1[0][0]*M2[0][1] + M1[0][1]*M2[1][1]
        a21 = M1[1][0]*M2[0][0] + M1[1][1]*M2[1][0]
        a22 = M1[1][0]*M2[0][1] + M1[1][1]*M2[1][1]
        r = [[a11%MOD, a12%MOD], [a21%MOD, a22%MOD]]
        return r

    def __get_matrix_power(self, M, p):
        """Matrix exponentiation (it is expected that p that is equal to the power of 2)."""

        if p == 1:
            return M
        if p in self.__memo:
            return self.__memo[p]
        K = self.__get_matrix_power(M, int(p/2))
        R = self.__multiply_matrices(K, K)
        self.__memo[p] = R
        return R

    def get_number(self, n):
        """Getting the nth Fibonacci number
        (a non-negative integer number is expected as n)."""
        if n == 0:
            return 0
        if n == 1:
            return 1
        # Factoring down the passed power into the powers that are equal to the power of 2), 
        # i.e. 62 = 2^5 + 2^4 + 2^3 + 2^2 + 2^0 = 32 + 16 + 8 + 4 + 1.
        powers = [int(pow(2, b))
                  for (b, d) in enumerate(reversed(bin(n-1)[2:])) if d == '1']
        # The same, but less pythonic: http://pastebin.com/h8cKDkHX
        
        matrices = [self.__get_matrix_power(MatrixFibonacci.Q, p)
                    for p in powers]
        while len(matrices) > 1:
            M1 = matrices.pop()
            M2 = matrices.pop()
            R = self.__multiply_matrices(M1, M2)
            matrices.append(R)
        return matrices[0][0][0]

mfib = MatrixFibonacci()
N = int(input())
print((mfib.get_number(N+1)*mfib.get_number(N))%(10**9+7))

0