結果
問題 | No.891 隣接3項間の漸化式 |
ユーザー |
|
提出日時 | 2021-01-20 19:15:52 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,684 bytes |
コンパイル時間 | 794 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 44,448 KB |
最終ジャッジ日時 | 2024-12-23 09:52:48 |
合計ジャッジ時間 | 26,150 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | RE * 3 |
other | RE * 39 |
ソースコード
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()) 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)