結果
問題 | No.891 隣接3項間の漸化式 |
ユーザー |
|
提出日時 | 2021-01-20 19:08:33 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,430 bytes |
コンパイル時間 | 102 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 44,536 KB |
最終ジャッジ日時 | 2024-12-23 09:44:49 |
合計ジャッジ時間 | 28,337 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | RE * 3 |
other | RE * 39 |
ソースコード
import numpy as np from numpy.core.numeric import concatenate, isscalar, binary_repr, identity, asanyarray, dot from numpy.core.numerictypes import issubdtype def matrix_power(M, n, mod_val): # Implementation shadows numpy's matrix_power, but with modulo included M = asanyarray(M) if len(M.shape) != 2 or M.shape[0] != M.shape[1]: raise ValueError("input must be a square array") if not issubdtype(type(n), int): raise TypeError("exponent must be an integer") from numpy.linalg import inv if n==0: M = M.copy() M[:] = identity(M.shape[0]) return M elif n<0: M = inv(M) n *= -1 result = M % mod_val if n <= 3: for _ in range(n-1): result = dot(result, M) % mod_val return result # binary decompositon to reduce the number of matrix # multiplications for n > 3 beta = binary_repr(n) Z, q, t = M, 0, len(beta) while beta[t-q-1] == '0': Z = dot(Z, Z) % mod_val q += 1 result = Z for k in range(q+1, t): Z = dot(Z, Z) % mod_val if beta[t-k-1] == '1': result = dot(result, Z) % mod_val return result % mod_val 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(p,n-1,mod) print(p_n[1][1]%mod)