結果
問題 | No.950 行列累乗 |
ユーザー | convexineq |
提出日時 | 2020-01-31 00:15:15 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,316 bytes |
コンパイル時間 | 387 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 118,400 KB |
最終ジャッジ日時 | 2024-09-16 04:11:19 |
合計ジャッジ時間 | 22,402 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 168 ms
84,540 KB |
testcase_01 | AC | 183 ms
84,480 KB |
testcase_02 | AC | 39 ms
53,504 KB |
testcase_03 | AC | 37 ms
53,248 KB |
testcase_04 | AC | 37 ms
52,736 KB |
testcase_05 | AC | 266 ms
87,936 KB |
testcase_06 | WA | - |
testcase_07 | AC | 56 ms
65,280 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 55 ms
65,536 KB |
testcase_11 | AC | 52 ms
63,872 KB |
testcase_12 | AC | 44 ms
60,416 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | AC | 39 ms
53,760 KB |
testcase_16 | WA | - |
testcase_17 | AC | 41 ms
53,120 KB |
testcase_18 | AC | 42 ms
59,264 KB |
testcase_19 | AC | 40 ms
53,760 KB |
testcase_20 | AC | 43 ms
53,632 KB |
testcase_21 | AC | 721 ms
113,152 KB |
testcase_22 | WA | - |
testcase_23 | AC | 554 ms
100,736 KB |
testcase_24 | AC | 573 ms
101,632 KB |
testcase_25 | AC | 469 ms
96,512 KB |
testcase_26 | AC | 590 ms
104,772 KB |
testcase_27 | AC | 587 ms
101,248 KB |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | AC | 625 ms
103,296 KB |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 607 ms
105,984 KB |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | AC | 171 ms
83,968 KB |
testcase_37 | AC | 170 ms
86,528 KB |
testcase_38 | AC | 176 ms
86,916 KB |
testcase_39 | AC | 173 ms
84,096 KB |
testcase_40 | AC | 612 ms
110,208 KB |
testcase_41 | AC | 609 ms
111,104 KB |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | AC | 755 ms
117,448 KB |
testcase_46 | WA | - |
testcase_47 | WA | - |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | WA | - |
testcase_51 | WA | - |
testcase_52 | AC | 233 ms
90,880 KB |
testcase_53 | AC | 234 ms
91,124 KB |
testcase_54 | AC | 38 ms
53,120 KB |
testcase_55 | AC | 37 ms
52,992 KB |
testcase_56 | AC | 201 ms
89,472 KB |
testcase_57 | AC | 329 ms
92,800 KB |
testcase_58 | WA | - |
testcase_59 | AC | 214 ms
90,752 KB |
testcase_60 | AC | 195 ms
91,136 KB |
ソースコード
def log_mod(a,b,MOD): a %= MOD; b %= MOD q = int(MOD**0.5)+2 # baby-step h = 1 if MOD != 1 else 0 memo = {} for i in range(q): if h==b: return i memo[h*b%MOD] = i h = h*a%MOD # giant-step #ここに来た時 h = a^q g = h for i in range(q): if g in memo: res = (i+1)*q-memo[g] if pow(a,res,MOD) == b: return res else: return -1 g = g*h%MOD return -1 #見つからない場合 def extgcd(x,y): if y==0: return 1,0 #g=x r0,r1,s0,s1 = x,y,1,0 while r1 != 0: r0,r1, s0,s1 = r1,r0%r1, s1,s0-r0//r1*s1 #g = r0 return s0,(r0-s0*x)//y def modinv(a,MOD): x,y = extgcd(a,MOD) return x%MOD def matmul(A,B,mod): # A,B: 行列 res = [[0]*len(B[0]) for _ in [None]*len(A)] for i, resi in enumerate(res): for k, aik in enumerate(A[i]): for j,bkj in enumerate(B[k]): resi[j] += aik*bkj resi[j] %= mod return res def matpow(A,p,M): #A^p mod M if p%2: return matmul(A, matpow(A,p-1,M), M) elif p > 0: b = matpow(A,p//2,M) return matmul(b,b,M) else: return [[1 if i == j else 0 for j in range(len(A))] for i in range(len(A))] # coding: utf-8 # Your code here! import sys sys.setrecursionlimit(10**6) readline = sys.stdin.readline read = sys.stdin.read def hash_projective(A): c = A[0]+A[1] for i in range(4): if c[i] != 0: break else: return((4,0,0,0)) cinv = modinv(c[i],MOD) res = [i] for j in range(4): if i != j: res.append(c[j]*cinv) return tuple(res) def hash_equal(A): return tuple(A[0]+A[1]) def discrete_logarithm(a,b,MOD,hashing,permit0): q = int(MOD**0.5)+3 # baby-step h = [[1 if i == j else 0 for j in range(2)] for i in range(2)] memo = {} for i in range(q): if hashing(h)==hashing(b) and (permit0 or i): return i memo[hashing(matmul(h,b,MOD))] = i h = matmul(h,a,MOD) # giant-step #ここに来た時 h = a^q g = [i[:] for i in h] for i in range(q): if hashing(g) in memo: res = (i+1)*q-memo[hashing(g)] if matpow(a,res,MOD) == b: return res else: return -1 g = matmul(g,h,MOD) return -1 #見つからない場合 def det(A): return (A[0][0]*A[1][1]-A[1][0]*A[0][1])%MOD MOD = int(input()) A = [[int(i) for i in readline().split()] for _ in range(2)] B = [[int(i) for i in readline().split()] for _ in range(2)] if det(A) == 0: print(discrete_logarithm(A,B,MOD,hash_equal,0)) else: E = [[1 if i == j else 0 for j in range(2)] for i in range(2)] # A^ai == r E , A^bi == s B # A^(xai+bi) == B なら r^x E= (B (A^bi)^{-1}) ai = discrete_logarithm(A,E,MOD,hash_projective,0) Ar = matpow(A,ai,MOD) r = Ar[0][0] bi = discrete_logarithm(A,B,MOD,hash_projective,1) C = matpow(A,bi,MOD) dd = modinv(det(C),MOD) #print(C,B) Cinv = [[C[1][1]*dd%MOD,-C[0][1]*dd%MOD], [-C[1][0]*dd%MOD,C[0][0]*dd%MOD]] B = matmul(B,Cinv,MOD) s = B[0][0] #print(ai,bi) #print(Ar,B) # r^x = s mod P を解く x = log_mod(r,s,MOD) if x == -1: print(-1) else: print(x*ai+bi)