結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー maspymaspy
提出日時 2020-03-24 02:36:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,236 bytes
コンパイル時間 883 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 29,632 KB
最終ジャッジ日時 2023-08-28 19:11:14
合計ジャッジ時間 5,540 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
29,524 KB
testcase_01 AC 122 ms
29,380 KB
testcase_02 AC 121 ms
29,436 KB
testcase_03 AC 122 ms
29,600 KB
testcase_04 AC 124 ms
29,628 KB
testcase_05 AC 122 ms
29,492 KB
testcase_06 AC 125 ms
29,444 KB
testcase_07 AC 124 ms
29,492 KB
testcase_08 AC 124 ms
29,632 KB
testcase_09 AC 122 ms
29,592 KB
testcase_10 AC 123 ms
29,404 KB
testcase_11 AC 125 ms
29,548 KB
testcase_12 AC 125 ms
29,472 KB
testcase_13 AC 125 ms
29,608 KB
testcase_14 AC 124 ms
29,440 KB
testcase_15 AC 123 ms
29,464 KB
testcase_16 AC 123 ms
29,624 KB
testcase_17 AC 124 ms
29,400 KB
testcase_18 AC 123 ms
29,596 KB
testcase_19 AC 122 ms
29,436 KB
testcase_20 AC 123 ms
29,608 KB
testcase_21 AC 122 ms
29,456 KB
testcase_22 AC 124 ms
29,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

N, M = map(int, read().split())
MOD = 10 ** 9 + 7


def coef_of_generating_function(P, Q, N):
    """compute the coefficient [x^N] P/Q of rational power series.

    Parameters
    ----------
    P : np.ndarray
        numerator.
    Q : np.ndarray
        denominator
        Q[0] == 1 and len(Q) == len(P) + 1 is assumed.
    N : int
        The coefficient to compute.
    """
    def convolve(f, g):
        return np.convolve(f, g) % MOD

    while N:
        Q1 = Q.copy()
        Q1[1::2] = np.negative(Q1[1::2])
        if N & 1:
            P = convolve(P, Q1)[1::2]
        else:
            P = convolve(P, Q1)[::2]
        Q = convolve(Q, Q1)[::2]
        N >>= 1
    return P[0]


def fibonacci(N):
    P = np.array([0, 1], np.int64)
    Q = np.array([1, -1, -1], np.int64)
    return coef_of_generating_function(P, Q, N)


A = fibonacci(M)
B = fibonacci(M + M)
L = B * pow(int(A), MOD - 2, MOD) % MOD

P = np.array([0, A], np.int64)
Q = np.array([1, -L, (-1) ** M], np.int64)
Q = np.convolve(Q, [1, -1])
answer = coef_of_generating_function(P, Q, N)
print(answer)
0