結果

問題 No.950 行列累乗
ユーザー maspymaspy
提出日時 2019-12-13 23:45:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 4,177 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 11,564 KB
実行使用メモリ 61,000 KB
最終ジャッジ日時 2023-09-10 06:26:09
合計ジャッジ時間 18,788 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 152 ms
34,044 KB
testcase_01 AC 148 ms
34,252 KB
testcase_02 WA -
testcase_03 AC 140 ms
30,428 KB
testcase_04 AC 198 ms
30,196 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 137 ms
30,320 KB
testcase_18 WA -
testcase_19 AC 136 ms
30,268 KB
testcase_20 AC 138 ms
30,340 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 150 ms
34,856 KB
testcase_38 AC 152 ms
35,016 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 AC 154 ms
35,800 KB
testcase_53 AC 158 ms
35,784 KB
testcase_54 AC 137 ms
30,156 KB
testcase_55 AC 135 ms
30,252 KB
testcase_56 AC 155 ms
35,752 KB
testcase_57 WA -
testcase_58 AC 139 ms
30,200 KB
testcase_59 WA -
testcase_60 AC 154 ms
35,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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(int(a),M,MOD)
    d = pow(int(a),MOD-2,MOD)
    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 matrix_power(A,n,MOD):
    if n == 0:
        return np.eye(2,dtype=np.int64)
    B = matrix_power(A,n//2,MOD)
    B = np.dot(B,B) % MOD
    return np.dot(A,B) % MOD if n & 1 else B

def matrix_inverse(A,MOD):
    # powerでもよいが
    print(A)
    det = (A[0,0]*A[1,1] - A[1,0]*A[0,1]) % MOD
    B = np.array([
        [A[1,1],-A[0,1]],
        [-A[1,0],A[0,0]],
    ])
    x = pow(int(det),MOD-2,MOD)
    B *= x; B %= MOD
    assert np.all(np.dot(A,B) % MOD == np.eye(2,dtype=np.int64))
    return B

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 = BSGS(trA,k,MOD)
        return -1 if n == -1 else n + 1
    print('!!')
    # det A == 1 に帰着したい
    n = BSGS(detA,detB,MOD)
    if n == -1:
        return -1
    e = BSGS(detA,1,MOD)
    Ainv = matrix_inverse(A,MOD)
    B = np.dot(B,matrix_power(Ainv,n,MOD)) % MOD
    A = matrix_power(A,e,MOD)
    k = solve_SL2(A,B,MOD)
    if k == -1:
        return -1
    return e * k + n

def to_hash(A):
    A = A.astype(object)
    return (A[:,0,0] << 96) + (A[:,1,0] << 64) + (A[:,0,1] << 32) + (A[:,1,1])

def solve_SL2(A,B,MOD):
    """
    A の固有値を考えると、Aの位数は十分小さいことが分かる
    F_pで2固有値 → p-1周期
    F_{p^2}で2固有値 → ノルム1にしたのでp+1周期
    固有値が重複 → 2乗すると固有値が1 → 2p周期
    2p程度で、BGSGをすればよい
    """
    U = 2 * MOD
    M = int(U ** .5 + 100)
    c = matrix_power(A,M,MOD)
    d = matrix_inverse(A,MOD)
    Baby = make_power_mat(d,M,MOD)
    Giant = make_power_mat(c,M,MOD)
    BB = np.zeros((M,2,2),np.int64)
    for i,j in itertools.product(range(2),repeat=2):
        BB[:,i,j] = (Baby[:,i,:] * B[:,j][None,:] % MOD).sum(axis=1) % MOD
    Gh = to_hash(Giant).tolist()
    BBh = to_hash(BB).tolist()
    se = set(BBh)
    q = -1
    for i,g in enumerate(Gh):
        if g in se:
            q = i
            break
    if q == -1:
        return -1
    g = Gh[q]
    r = -1
    for i,b in enumerate(BBh):
        if b == g:
            r = i
            break
    return q * M + r

print(solve(A,B,MOD))

0