Submit Info #7053

Problem Lang User Status Time Memory
Kth Root(Mod) java 37zigen AC 434 ms 76.23 MiB

ケース詳細
Name Status Time Memory
Tonelli-Shanks_worstcase_00 AC 428 ms 66.37 MiB
Tonelli-Shanks_worstcase_01 AC 418 ms 75.29 MiB
Tonelli-Shanks_worstcase_02 AC 434 ms 76.23 MiB
Tonelli-Shanks_worstcase_03 AC 399 ms 72.18 MiB
Tonelli-Shanks_worstcase_04 AC 402 ms 66.08 MiB
example_00 AC 104 ms 12.11 MiB
max_random_00 AC 272 ms 28.51 MiB
max_random_01 AC 281 ms 28.41 MiB
max_random_02 AC 271 ms 28.52 MiB
max_random_03 AC 276 ms 28.38 MiB
max_random_04 AC 273 ms 28.54 MiB
random_00 AC 191 ms 17.46 MiB
random_01 AC 215 ms 22.64 MiB
random_02 AC 224 ms 26.51 MiB
random_03 AC 180 ms 17.16 MiB
random_04 AC 156 ms 15.80 MiB
safe_prime_00 AC 262 ms 27.92 MiB
safe_prime_01 AC 255 ms 27.93 MiB
safe_prime_02 AC 259 ms 27.98 MiB
safe_prime_03 AC 252 ms 27.93 MiB
safe_prime_04 AC 251 ms 27.92 MiB
small_00 AC 187 ms 19.90 MiB

import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; class Main { public static void main(String[] args) throws Exception { new Main().run(); } void run() { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int T = sc.nextInt(); for (int i = 0; i < T; ++i) { long k = sc.nextLong(); long a = sc.nextLong(); long p = sc.nextLong(); long kthroot = kth_root(a, k, p); pw.println(kthroot); } pw.close(); } long kth_root(long a, long k, long p) { if (k == 0) return a == 1 ? 1 : -1; if (k >0 && a == 0) return 0; long g = gcd(k, p - 1); if (pow(a, (p - 1) / g, p) != 1) return -1; a = pow(a, inv(k / g, (p - 1) / g), p); for (long div = 2; div * div <= g; ++div) { int sz = 0; while (g % div == 0) { g /= div; ++sz; } if (sz > 0) { long b = peth_root(a, div, sz, p); a = b; } } if (g > 1) a = peth_root(a, g, 1, p); return a; } long peth_root(long a, long p, int e, long mod) { long q = mod - 1; int s = 0; while (q % p == 0) { q /= p; ++s; } long pe = pow(p, e, mod); long ans = pow(a, ((pe - 1) * inv(q, pe) % pe * q + 1) / pe, mod); long c = 2; while (pow(c, (mod - 1) / p, mod) == 1) ++c; c = pow(c, q, mod); HashMap<Long, Integer> map = new HashMap<>(); long add = 1; int v = (int) Math.sqrt(p) + 1; long mul = pow(c, v * pow(p, s - 1, mod - 1), mod); for (int i = 0; i <= v; ++i) { map.put(add, i); add = add * mul % mod; } mul = inv(pow(c, pow(p, s - 1, mod - 1), mod), mod); out: for (int i = e; i < s; ++i) { long err = inv(pow(ans, pe, mod), mod) * a % mod; long target = pow(err, pow(p, s - 1 - i, mod - 1), mod); for (int j = 0; j <= v; ++j) { if (map.containsKey(target % mod)) { int x = map.get(target % mod); ans = ans * pow(c, (j + v * x) * pow(p, i - e, mod - 1) % (mod - 1), mod) % mod; continue out; } target = target * mul % mod; } throw new AssertionError(); } return ans; } int cnt(long a, long base, long p) { int ret = 0; while (a != 1) { a = pow(a, base, p); ++ret; } return ret; } long inv(long a, long p) { a %= p; long u = 1, v = 0; long b = p; while (b > 0) { long q = a / b; a %= b; u -= v * q % p; u %= p; { u ^= v; v ^= u; u ^= v; a ^= b; b ^= a; a ^= b; } } return u < 0 ? u + p : u; } long pow(long a, long n, long p) { n %= p - 1; long r = 1; for (; n > 0; n >>= 1, a = a * a % p) if (n % 2 == 1) r = r * a % p; return r; } long gcd(long a, long b) { return a == 0 ? b : gcd(b % a, a); } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }