結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー kimiyukikimiyuki
提出日時 2018-07-28 01:38:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 740 bytes
コンパイル時間 1,240 ms
コンパイル使用メモリ 10,932 KB
実行使用メモリ 29,208 KB
最終ジャッジ日時 2023-09-19 05:52:21
合計ジャッジ時間 6,435 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
28,868 KB
testcase_01 AC 139 ms
29,168 KB
testcase_02 AC 139 ms
29,056 KB
testcase_03 AC 139 ms
29,180 KB
testcase_04 AC 136 ms
29,208 KB
testcase_05 AC 138 ms
29,056 KB
testcase_06 AC 136 ms
29,152 KB
testcase_07 AC 137 ms
29,092 KB
testcase_08 AC 138 ms
29,112 KB
testcase_09 AC 137 ms
28,996 KB
testcase_10 AC 137 ms
29,040 KB
testcase_11 AC 138 ms
29,092 KB
testcase_12 AC 138 ms
29,164 KB
testcase_13 AC 138 ms
29,124 KB
testcase_14 AC 137 ms
28,964 KB
testcase_15 AC 139 ms
29,120 KB
testcase_16 AC 136 ms
28,968 KB
testcase_17 AC 137 ms
29,132 KB
testcase_18 AC 137 ms
29,048 KB
testcase_19 AC 135 ms
29,024 KB
testcase_20 AC 135 ms
29,056 KB
testcase_21 AC 136 ms
29,044 KB
testcase_22 AC 136 ms
29,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

def powmod(f, n, mod):
    g = np.identity(3, 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, m):
    # \begin{pmatrix} F_{2i+1} \\ F_{2i} \\ \sum_{j \le i} F_{2j} \end{pmatrix}
    x = np.matrix([ 1, 0, 0 ], dtype=np.uint64).transpose()
    f = np.matrix([
        [ 1, 1, 0 ],
        [ 1, 0, 0 ],
        [ 0, 0, 1 ],
    ], dtype=np.uint64)
    g = np.matrix([
        [ 1, 0, 0 ],
        [ 0, 1, 0 ],
        [ 0, 1, 1 ],
    ], dtype=np.uint64)
    mod = 10 ** 9 + 7
    return (powmod(g * powmod(f, m, mod) % mod, n, mod) * x % mod)[2, 0]

print(solve(*map(int, input().split())))
0