結果

問題 No.950 行列累乗
ユーザー convexineqconvexineq
提出日時 2020-01-31 01:11:24
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,334 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 87,316 KB
実行使用メモリ 122,520 KB
最終ジャッジ日時 2023-10-14 09:34:47
合計ジャッジ時間 20,508 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
87,296 KB
testcase_01 AC 179 ms
87,232 KB
testcase_02 AC 63 ms
71,748 KB
testcase_03 AC 66 ms
71,908 KB
testcase_04 AC 62 ms
71,780 KB
testcase_05 AC 247 ms
92,164 KB
testcase_06 AC 76 ms
76,880 KB
testcase_07 AC 73 ms
76,836 KB
testcase_08 AC 73 ms
76,780 KB
testcase_09 AC 69 ms
76,504 KB
testcase_10 AC 65 ms
75,804 KB
testcase_11 AC 74 ms
76,708 KB
testcase_12 AC 73 ms
76,376 KB
testcase_13 AC 75 ms
76,884 KB
testcase_14 AC 75 ms
76,980 KB
testcase_15 AC 64 ms
71,616 KB
testcase_16 AC 71 ms
76,560 KB
testcase_17 AC 62 ms
71,912 KB
testcase_18 AC 68 ms
76,200 KB
testcase_19 AC 60 ms
71,460 KB
testcase_20 AC 61 ms
71,812 KB
testcase_21 AC 585 ms
110,312 KB
testcase_22 AC 517 ms
111,824 KB
testcase_23 AC 466 ms
106,544 KB
testcase_24 AC 441 ms
105,572 KB
testcase_25 AC 389 ms
100,444 KB
testcase_26 AC 513 ms
108,208 KB
testcase_27 AC 494 ms
103,956 KB
testcase_28 AC 461 ms
110,580 KB
testcase_29 AC 510 ms
105,336 KB
testcase_30 AC 486 ms
106,344 KB
testcase_31 AC 457 ms
106,092 KB
testcase_32 AC 612 ms
118,812 KB
testcase_33 AC 507 ms
105,484 KB
testcase_34 WA -
testcase_35 AC 490 ms
108,712 KB
testcase_36 AC 163 ms
85,996 KB
testcase_37 AC 159 ms
88,544 KB
testcase_38 AC 165 ms
88,820 KB
testcase_39 AC 174 ms
86,256 KB
testcase_40 AC 537 ms
117,228 KB
testcase_41 AC 564 ms
117,192 KB
testcase_42 AC 665 ms
115,800 KB
testcase_43 AC 578 ms
121,404 KB
testcase_44 WA -
testcase_45 AC 623 ms
116,056 KB
testcase_46 AC 627 ms
121,304 KB
testcase_47 AC 65 ms
71,912 KB
testcase_48 WA -
testcase_49 AC 642 ms
121,524 KB
testcase_50 AC 655 ms
121,084 KB
testcase_51 AC 658 ms
122,520 KB
testcase_52 AC 206 ms
93,364 KB
testcase_53 AC 219 ms
93,088 KB
testcase_54 AC 62 ms
71,672 KB
testcase_55 AC 64 ms
71,980 KB
testcase_56 AC 182 ms
91,588 KB
testcase_57 AC 289 ms
96,116 KB
testcase_58 WA -
testcase_59 AC 226 ms
93,168 KB
testcase_60 AC 191 ms
93,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def log_mod(a,b,MOD,permit0):
    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 and (permit0 or i): 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:
            cinv = modinv(c[i],MOD)
            return tuple([i*cinv%MOD for i in c])    
    else: return((0,0,0,0))
    

def hash_equal(A):
    return tuple(A[0]+A[1])

def discrete_logarithm(a,b,MOD,hashing,permit0):
    q = int(MOD**0.5)+10
    # baby-step
    h = [[1,0],[0,1]]
    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 hashing(matpow(a,res,MOD)) == hashing(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)]
E = [[1,0],[0,1]]

if det(A) == 0:
    print(discrete_logarithm(A,B,MOD,hash_equal,0))

else:
    # 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(Ar,B)
    if bi == -1:
        print(-1)
        exit()

    # r^x = s mod P を解く
    x = log_mod(r,s,MOD,1)
    #print(r,s,x,ai,bi)
    if x == -1: print(-1)
    else:
        ans = x*ai+bi
        if ans: print(ans)
        else: print(log_mod(r,s,MOD,0))

    



0