結果

問題 No.981 一般冪乗根
ユーザー 37zigen37zigen
提出日時 2020-02-11 10:20:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 410 ms / 6,000 ms
コード長 3,221 bytes
コンパイル時間 2,777 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 133,324 KB
最終ジャッジ日時 2024-04-18 06:12:21
合計ジャッジ時間 97,213 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 351 ms
89,348 KB
testcase_01 AC 340 ms
133,324 KB
testcase_02 AC 345 ms
88,420 KB
testcase_03 AC 341 ms
65,284 KB
testcase_04 AC 338 ms
52,496 KB
testcase_05 AC 328 ms
47,136 KB
testcase_06 AC 328 ms
47,320 KB
testcase_07 AC 332 ms
46,912 KB
testcase_08 AC 337 ms
47,004 KB
testcase_09 AC 335 ms
47,164 KB
testcase_10 AC 331 ms
47,008 KB
testcase_11 AC 332 ms
46,720 KB
testcase_12 AC 328 ms
46,844 KB
testcase_13 AC 333 ms
47,144 KB
testcase_14 AC 325 ms
47,048 KB
testcase_15 AC 346 ms
46,784 KB
testcase_16 AC 325 ms
46,868 KB
testcase_17 AC 334 ms
46,512 KB
testcase_18 AC 329 ms
46,440 KB
testcase_19 AC 330 ms
47,548 KB
testcase_20 AC 333 ms
47,256 KB
testcase_21 AC 339 ms
47,144 KB
testcase_22 AC 345 ms
47,460 KB
testcase_23 AC 336 ms
47,224 KB
testcase_24 AC 332 ms
46,996 KB
testcase_25 AC 369 ms
46,692 KB
testcase_26 AC 347 ms
47,148 KB
testcase_27 AC 321 ms
47,184 KB
testcase_28 AC 410 ms
47,484 KB
evil_60bit1.txt AC 510 ms
50,092 KB
evil_60bit2.txt AC 505 ms
49,836 KB
evil_60bit3.txt AC 524 ms
50,180 KB
evil_hack AC 142 ms
41,956 KB
evil_hard_random AC 524 ms
49,708 KB
evil_hard_safeprime.txt AC 630 ms
49,816 KB
evil_hard_tonelli0 TLE -
evil_hard_tonelli1 TLE -
evil_hard_tonelli2 AC 1,607 ms
73,744 KB
evil_hard_tonelli3 TLE -
evil_sefeprime1.txt AC 632 ms
49,924 KB
evil_sefeprime2.txt AC 617 ms
50,060 KB
evil_sefeprime3.txt AC 626 ms
50,140 KB
evil_tonelli1.txt TLE -
evil_tonelli2.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

class Main {
	public static void main(String[] args) throws Exception {
		long curtime=System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis()-curtime);
	}

	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 p = sc.nextLong();
			long k = sc.nextLong();
			long a = sc.nextLong();
			long kthroot = kth_root(a, k, p);
			System.out.println(kthroot);
//			pw.println(kthroot);
		}
		pw.close();
	}

	long kth_root(long a, long k, long p) {
		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, (mul((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((double)(s-e)*p) + 1;
		long mul = pow(c, mul(v, pow(p, s - 1, mod - 1),mod-1), mod);
		for (int i = 0; i <= v; ++i) {
			map.put(add, i);
			add = mul(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 = mul(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)) {
					int x = map.get(target);
					ans = mul(ans , pow(c, mul((j + v * x) , pow(p, i - e, mod - 1) , (mod - 1)), mod) , mod);
					continue out;
				}
				target = mul(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 -=mul(v , q , p);
			u = (u%p+p)%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;
		BigInteger r=BigInteger.ONE;
		BigInteger a_=BigInteger.valueOf(a);
		BigInteger p_=BigInteger.valueOf(p);
		for (; n > 0; n >>= 1, a_=a_.multiply(a_).remainder(p_))
			if (n % 2 == 1)
				r = r.multiply(a_).remainder(p_);
		return r.longValue();
	}

	long gcd(long a, long b) {
		return a == 0 ? b : gcd(b % a, a);
	}
	
	long mul(long a,long b,long p) {
		return BigInteger.valueOf(a).multiply(BigInteger.valueOf(b)).mod(BigInteger.valueOf(p)).longValue();
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0