結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー kimiyukikimiyuki
提出日時 2018-07-28 01:34:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 640 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 29,672 KB
最終ジャッジ日時 2023-09-19 05:51:33
合計ジャッジ時間 5,123 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
29,600 KB
testcase_01 AC 140 ms
29,504 KB
testcase_02 AC 139 ms
29,620 KB
testcase_03 AC 145 ms
29,488 KB
testcase_04 AC 144 ms
29,672 KB
testcase_05 AC 140 ms
29,552 KB
testcase_06 AC 140 ms
29,528 KB
testcase_07 AC 141 ms
29,508 KB
testcase_08 AC 141 ms
29,660 KB
testcase_09 AC 140 ms
29,608 KB
testcase_10 AC 140 ms
29,596 KB
testcase_11 AC 139 ms
29,520 KB
testcase_12 AC 139 ms
29,652 KB
testcase_13 AC 139 ms
29,636 KB
testcase_14 AC 139 ms
29,664 KB
testcase_15 AC 139 ms
29,648 KB
testcase_16 AC 140 ms
29,616 KB
testcase_17 AC 138 ms
29,660 KB
testcase_18 AC 139 ms
29,660 KB
testcase_19 AC 137 ms
29,556 KB
testcase_20 AC 136 ms
29,628 KB
testcase_21 AC 138 ms
29,576 KB
testcase_22 AC 139 ms
29,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import numpy as np

def powmod(f, n, mod):
    g = np.identity(4, dtype=np.uint64)
    for p in map(int, reversed(bin(n)[2 :])):
        if p:
            g = g * f % mod
        f = f * f % mod
    return g

def solve(n):
    # \begin{pmatrix} F_{i+1}^2 \\ F_{i+1} F_i \\ F_i^2 \\ \sum_{j \le i} F_j^2 \end{pmatrix}
    x = np.matrix([ 1, 0, 0, 0 ], dtype=np.uint64).transpose()
    f = np.matrix([
        [ 1, 2, 1, 0 ],
        [ 1, 1, 0, 0 ],
        [ 1, 0, 0, 0 ],
        [ 1, 0, 0, 1 ],
    ], dtype=np.uint64)
    mod = 10 ** 9 + 7
    return (powmod(f, n, mod) * x % mod)[3, 0]

print(solve(int(input())))
0