結果

問題 No.891 隣接3項間の漸化式
ユーザー jensenjensen
提出日時 2021-01-20 19:30:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 1,696 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 10,972 KB
実行使用メモリ 29,800 KB
最終ジャッジ日時 2023-08-24 23:30:36
合計ジャッジ時間 9,318 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
29,788 KB
testcase_01 AC 133 ms
29,684 KB
testcase_02 AC 134 ms
29,668 KB
testcase_03 AC 134 ms
29,720 KB
testcase_04 AC 135 ms
29,716 KB
testcase_05 AC 134 ms
29,692 KB
testcase_06 AC 135 ms
29,668 KB
testcase_07 AC 133 ms
29,788 KB
testcase_08 AC 133 ms
29,696 KB
testcase_09 AC 134 ms
29,708 KB
testcase_10 AC 137 ms
29,800 KB
testcase_11 AC 134 ms
29,744 KB
testcase_12 AC 134 ms
29,708 KB
testcase_13 AC 134 ms
29,564 KB
testcase_14 AC 130 ms
29,496 KB
testcase_15 AC 133 ms
29,572 KB
testcase_16 AC 132 ms
29,592 KB
testcase_17 AC 133 ms
29,528 KB
testcase_18 AC 132 ms
29,664 KB
testcase_19 AC 133 ms
29,684 KB
testcase_20 AC 132 ms
29,588 KB
testcase_21 AC 136 ms
29,608 KB
testcase_22 AC 133 ms
29,436 KB
testcase_23 AC 136 ms
29,488 KB
testcase_24 AC 131 ms
29,672 KB
testcase_25 AC 133 ms
29,696 KB
testcase_26 AC 133 ms
29,696 KB
testcase_27 AC 135 ms
29,708 KB
testcase_28 AC 136 ms
29,720 KB
testcase_29 AC 133 ms
29,748 KB
testcase_30 AC 131 ms
29,632 KB
testcase_31 AC 133 ms
29,640 KB
testcase_32 AC 132 ms
29,656 KB
testcase_33 AC 131 ms
29,696 KB
testcase_34 AC 131 ms
29,660 KB
testcase_35 AC 134 ms
29,732 KB
testcase_36 AC 137 ms
29,688 KB
testcase_37 AC 139 ms
29,700 KB
testcase_38 AC 134 ms
29,780 KB
testcase_39 AC 133 ms
29,712 KB
testcase_40 AC 134 ms
29,748 KB
testcase_41 AC 131 ms
29,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

def matrix_power_mod(x, n, modulus):
    x = np.asanyarray(x)
    if len(x.shape) != 2:
        raise ValueError("input must be a matrix")
    if x.shape[0] != x.shape[1]:
        raise ValueError("input must be a square matrix")
    if not isinstance(n, int):
        raise ValueError("power must be an integer")

    if n < 0:
        x = np.linalg.inv(x)
        n = -n
    if n == 0:
        return np.identity(x.shape[0], dtype=x.dtype)
    y = None
    while n > 1:
        if n % 2 == 1:
            y = _matrix_mul_mod_opt(x, y, modulus=modulus)
        x = _matrix_mul_mod(x, x, modulus=modulus)
        n = n // 2
    return _matrix_mul_mod_opt(x, y, modulus=modulus)


def matrix_mul_mod(a, b, modulus):
    if len(a.shape) != 2:
        raise ValueError("input a must be a matrix")
    if len(b.shape) != 2:
        raise ValueError("input b must be a matrix")
    if a.shape[1] != a.shape[0]:
        raise ValueError("input a and b must have compatible shape for multiplication")
    return _matrix_mul_mod(a, b, modulus=modulus)


def _matrix_mul_mod_opt(a, b, modulus):
    if b is None:
        return a
    return _matrix_mul_mod(a, b, modulus=modulus)


def _matrix_mul_mod(a, b, modulus):
    r = np.zeros((a.shape[0], b.shape[1]), dtype=a.dtype)
    bT = b.T
    for rowindex in range(r.shape[0]):
        x = (a[rowindex, :] * bT) % modulus
        x = np.sum(x, 1) % modulus
        r[rowindex, :] = x
    return r


a,b,n=map(int,input().split())
mod=10**9+7
a%=mod
b%=mod
mod=10**9+7
p=np.array([[0,1],[b,a]])
if(n==0 or n==1):
    print(p[0][n])
elif(b==0):
    print(pow(a,n-1,mod))
else:
    p_n=matrix_power_mod(p,n-1,mod)
    print(p_n[1][1]%mod)
0