結果

問題 No.1648 Sum of Powers
ユーザー tamatotamato
提出日時 2021-08-13 23:04:10
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,998 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 82,624 KB
実行使用メモリ 92,376 KB
最終ジャッジ日時 2024-04-14 20:03:59
合計ジャッジ時間 16,579 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 187 ms
91,520 KB
testcase_01 AC 161 ms
76,904 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
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 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 WA -
testcase_38 WA -
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 RE -
testcase_48 AC 164 ms
76,724 KB
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    def matmul(A, B):
        C = [[0] * len(B[0]) for _ in range(len(A))]
        for i in range(len(A)):
            for k in range(len(B)):
                for j in range(len(B[0])):
                    C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod
        return C

    def matpow(A, p):
        n = len(A)
        B = [[0] * n for _ in range(n)]
        for i in range(n):
            B[i][i] = 1
        while p > 0:
            if p & 1:
                B = matmul(B, A)
            A = matmul(A, A)
            p >>= 1
        return B

    # x**k == y % mod
    # O(mod**0.5)
    def DiscreteLog(x, y, mod):
        m = int(mod ** 0.5) + 1
        dic = {1: 0}

        # baby-step
        xi = 1
        for i in range(1, m + 1):
            xi = (xi * x) % mod
            dic[xi] = i
        if y in dic:
            return dic[y]

        # giant-step
        r = pow(xi, mod - 2, mod)
        for a in range(1, m + 1):
            y = (y * r) % mod
            if y in dic:
                return a * m + dic[y]

        return -1

    A, B, P, Q = map(int, input().split())

    if B == 0:
        print(DiscreteLog(A, P, mod))
        assert False
        exit()

    M = [[A, -B], [1, 0]]
    B_inv = pow(B, mod-2, mod)
    M_inv = [[0, 1], [(-B_inv)%mod, (A * B_inv)%mod]]

    M_pow = {}
    M_cur = [[1, 0], [0, 1]]
    initial_vec = [[A], [2]]
    for i in range(1, 10 ** 5 + 1):
        M_cur = matmul(M_cur, M)
        U = matmul(M_cur, initial_vec)
        U_hash = U[0][0] * mod + U[1][0]
        M_pow[U_hash] = i

    M_inv_cur = [[1, 0], [0, 1]]
    last_vec = [[P], [Q]]
    for i in range(1, 10 ** 5 + 1):
        M_inv_cur = matmul(M_inv_cur, M_inv)
        V = matmul(M_inv_cur, last_vec)
        V_hash = V[0][0] * mod + V[1][0]
        if V_hash in M_pow:
            j = M_pow[V_hash]
            print(i + j + 1)
            exit()


if __name__ == '__main__':
    main()
0