結果
問題 | No.950 行列累乗 |
ユーザー | maspy |
提出日時 | 2019-12-13 23:11:00 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,224 bytes |
コンパイル時間 | 101 ms |
コンパイル使用メモリ | 13,056 KB |
実行使用メモリ | 44,800 KB |
最終ジャッジ日時 | 2024-06-27 20:36:39 |
合計ジャッジ時間 | 34,510 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | AC | 570 ms
44,416 KB |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | RE | - |
testcase_31 | RE | - |
testcase_32 | RE | - |
testcase_33 | RE | - |
testcase_34 | RE | - |
testcase_35 | RE | - |
testcase_36 | RE | - |
testcase_37 | RE | - |
testcase_38 | RE | - |
testcase_39 | RE | - |
testcase_40 | RE | - |
testcase_41 | RE | - |
testcase_42 | RE | - |
testcase_43 | RE | - |
testcase_44 | RE | - |
testcase_45 | RE | - |
testcase_46 | RE | - |
testcase_47 | RE | - |
testcase_48 | RE | - |
testcase_49 | RE | - |
testcase_50 | RE | - |
testcase_51 | RE | - |
testcase_52 | RE | - |
testcase_53 | RE | - |
testcase_54 | AC | 507 ms
44,152 KB |
testcase_55 | AC | 506 ms
44,536 KB |
testcase_56 | RE | - |
testcase_57 | RE | - |
testcase_58 | AC | 502 ms
44,416 KB |
testcase_59 | RE | - |
testcase_60 | RE | - |
ソースコード
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np import itertools MOD = int(readline()) data = list(map(int,read().split())) A = np.int64(data[:4]).reshape(2,2) B = np.int64(data[4:]).reshape(2,2) def make_power_mat(A, L, MOD=MOD): N = A.shape[0] assert A.shape == (N,N) B = L.bit_length() x = np.empty((1<<B,N,N), np.int64) x[0] = np.eye(N,dtype=np.int64) X = A.copy() for n in range(B): for i,j in itertools.product(range(N),repeat=2): x[1<<n:1<<(n+1),i,j] = (x[:1<<n,i,:] * X[:,j] % MOD).sum(axis=1) % MOD Y = X.copy() for i,j in itertools.product(range(N),repeat=2): X[i,j] = (Y[i,:] * Y[:,j] % MOD).sum() % MOD return x[:L] def make_power(a, L, MOD=MOD): B = L.bit_length() x = np.empty((1<<B), np.int64) x[0] = 1 for n in range(B): x[1<<n:1<<(n+1)] = x[:1<<n] * a % MOD a *= a; a %= MOD return x[:L] def BSGS(a,b,MOD): assert a != 0 M = int(MOD ** .5 + 100) c = pow(a,M,MOD) d = pow(a,MOD-2,MOD) print(c,d,M) B = make_power(d,M,MOD) G = make_power(c,M,MOD) I = np.in1d(G, b * B % MOD) if not I.any(): return -1 q = np.where(I)[0][0] r = np.where(b * B % MOD == G[q])[0][0] return q * M + r def solve_naive(A,B,MOD): X = np.int64([[1,0],[0,1]]) for n in range(1,10**4): X = np.dot(X,A) % MOD if (X == B).all(): return n return -1 def solve(A,B,MOD): if MOD == 2: return solve_naive(A,B,MOD) detA = (A[0,0]*A[1,1] - A[1,0]*A[0,1]) % MOD detB = (B[0,0]*B[1,1] - B[1,0]*B[0,1]) % MOD trA = (A[0,0] + A[1,1]) % MOD if detA == 0 and trA == 0: # べき零 if (A == B).all(): return 1 if (B == 0).all(): return 2 return -1 if detA == 0: # A^n = t^{n-1}A I,J = np.where(A != 0); i = I[0]; j = J[0] a = A[i,j]; b = B[i,j] k = b * pow(int(a),MOD-2,MOD) % MOD assert a * k % MOD == b n = BGSG(trA,k,MOD) return -1 if n == -1 else n + 1 raise Exception print(solve(A,B,MOD))