結果
問題 | No.1648 Sum of Powers |
ユーザー | tamato |
提出日時 | 2021-08-13 23:34:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 280 ms / 2,000 ms |
コード長 | 2,293 bytes |
コンパイル時間 | 368 ms |
コンパイル使用メモリ | 82,252 KB |
実行使用メモリ | 91,916 KB |
最終ジャッジ日時 | 2024-10-03 23:45:11 |
合計ジャッジ時間 | 10,782 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
52,352 KB |
testcase_01 | AC | 42 ms
52,608 KB |
testcase_02 | AC | 54 ms
63,616 KB |
testcase_03 | AC | 52 ms
63,616 KB |
testcase_04 | AC | 54 ms
63,872 KB |
testcase_05 | AC | 53 ms
63,744 KB |
testcase_06 | AC | 54 ms
63,872 KB |
testcase_07 | AC | 53 ms
63,488 KB |
testcase_08 | AC | 54 ms
63,744 KB |
testcase_09 | AC | 54 ms
63,872 KB |
testcase_10 | AC | 54 ms
63,872 KB |
testcase_11 | AC | 52 ms
63,488 KB |
testcase_12 | AC | 56 ms
63,744 KB |
testcase_13 | AC | 52 ms
63,616 KB |
testcase_14 | AC | 51 ms
63,488 KB |
testcase_15 | AC | 53 ms
63,488 KB |
testcase_16 | AC | 53 ms
63,488 KB |
testcase_17 | AC | 53 ms
63,744 KB |
testcase_18 | AC | 51 ms
63,232 KB |
testcase_19 | AC | 52 ms
63,744 KB |
testcase_20 | AC | 51 ms
63,360 KB |
testcase_21 | AC | 52 ms
64,128 KB |
testcase_22 | AC | 241 ms
91,664 KB |
testcase_23 | AC | 238 ms
91,392 KB |
testcase_24 | AC | 241 ms
91,656 KB |
testcase_25 | AC | 238 ms
91,620 KB |
testcase_26 | AC | 239 ms
91,532 KB |
testcase_27 | AC | 238 ms
91,524 KB |
testcase_28 | AC | 249 ms
91,528 KB |
testcase_29 | AC | 241 ms
91,652 KB |
testcase_30 | AC | 235 ms
91,788 KB |
testcase_31 | AC | 248 ms
91,780 KB |
testcase_32 | AC | 238 ms
91,912 KB |
testcase_33 | AC | 235 ms
91,784 KB |
testcase_34 | AC | 241 ms
91,656 KB |
testcase_35 | AC | 243 ms
91,404 KB |
testcase_36 | AC | 238 ms
91,528 KB |
testcase_37 | AC | 262 ms
91,532 KB |
testcase_38 | AC | 254 ms
91,792 KB |
testcase_39 | AC | 271 ms
91,788 KB |
testcase_40 | AC | 249 ms
91,912 KB |
testcase_41 | AC | 280 ms
91,916 KB |
testcase_42 | AC | 253 ms
91,784 KB |
testcase_43 | AC | 238 ms
91,660 KB |
testcase_44 | AC | 247 ms
91,660 KB |
testcase_45 | AC | 236 ms
91,500 KB |
testcase_46 | AC | 232 ms
91,664 KB |
testcase_47 | AC | 50 ms
63,360 KB |
testcase_48 | AC | 38 ms
52,480 KB |
testcase_49 | AC | 48 ms
63,744 KB |
testcase_50 | AC | 48 ms
63,488 KB |
testcase_51 | AC | 45 ms
58,368 KB |
testcase_52 | AC | 40 ms
52,608 KB |
testcase_53 | AC | 37 ms
52,608 KB |
testcase_54 | AC | 38 ms
52,608 KB |
testcase_55 | AC | 230 ms
91,784 KB |
testcase_56 | AC | 235 ms
91,524 KB |
testcase_57 | AC | 44 ms
58,368 KB |
ソースコード
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: ans = DiscreteLog(A, P, mod) if 2 <= ans <= 10 ** 18: print(ans) else: print(ans + mod - 1) exit() if (A * A - 2 * B)%mod == P and A == Q: print(2) 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) if U[0][0] == P and U[1][0] == Q: print(i + 1) exit() U_hash = U[0][0] * mod + U[1][0] M_pow[U_hash] = i M_inv_p = matpow(M_inv, 10 ** 5) 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_p) 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 * (10 ** 5) + j + 1) exit() if __name__ == '__main__': main()