結果

問題 No.981 一般冪乗根
ユーザー square1001square1001
提出日時 2020-02-10 19:08:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5,147 ms / 6,000 ms
コード長 1,587 bytes
コンパイル時間 1,181 ms
コンパイル使用メモリ 78,488 KB
実行使用メモリ 10,272 KB
最終ジャッジ日時 2024-04-18 06:10:06
合計ジャッジ時間 184,705 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,986 ms
8,576 KB
testcase_01 AC 4,900 ms
8,576 KB
testcase_02 AC 4,783 ms
8,576 KB
testcase_03 AC 4,946 ms
8,576 KB
testcase_04 AC 4,963 ms
8,704 KB
testcase_05 AC 101 ms
8,704 KB
testcase_06 AC 98 ms
8,576 KB
testcase_07 AC 104 ms
8,704 KB
testcase_08 AC 103 ms
8,576 KB
testcase_09 AC 103 ms
8,704 KB
testcase_10 AC 108 ms
8,704 KB
testcase_11 AC 103 ms
8,576 KB
testcase_12 AC 97 ms
10,272 KB
testcase_13 AC 102 ms
8,704 KB
testcase_14 AC 100 ms
5,376 KB
testcase_15 AC 97 ms
5,376 KB
testcase_16 AC 102 ms
5,376 KB
testcase_17 AC 102 ms
5,376 KB
testcase_18 AC 105 ms
5,376 KB
testcase_19 AC 98 ms
5,376 KB
testcase_20 AC 96 ms
5,376 KB
testcase_21 AC 99 ms
5,376 KB
testcase_22 AC 97 ms
5,376 KB
testcase_23 AC 100 ms
5,376 KB
testcase_24 AC 99 ms
5,376 KB
testcase_25 AC 3,956 ms
5,376 KB
testcase_26 AC 5,147 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 2,415 ms
5,376 KB
evil_60bit1.txt TLE -
evil_60bit2.txt TLE -
evil_60bit3.txt TLE -
evil_hack AC 2 ms
5,376 KB
evil_hard_random TLE -
evil_hard_safeprime.txt TLE -
evil_hard_tonelli0 TLE -
evil_hard_tonelli1 TLE -
evil_hard_tonelli2 TLE -
evil_hard_tonelli3 TLE -
evil_sefeprime1.txt TLE -
evil_sefeprime2.txt TLE -
evil_sefeprime3.txt TLE -
evil_tonelli1.txt TLE -
evil_tonelli2.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int binpow(int a, int b, int mod) {
	int ans = 1;
	while (b > 0) {
		if (b & 1) ans = 1LL * ans * a % mod;
		a = 1LL * a * a % mod;
		b >>= 1;
	}
	return ans;
}
int find_primitive_root(int p) {
	// Time complexity: O(p^(1/4+eps))
	int x = p - 1;
	vector<int> checks;
	for (int i = 2; i * i <= x; ++i) {
		if (x % i == 0) {
			while (x % i == 0) x /= i;
			checks.push_back((p - 1) / i);
		}
	}
	if (x > 1) {
		checks.push_back((p - 1) / x);
	}
	for (int i = 2; i < p; ++i) {
		bool ok = true;
		for (int j : checks) {
			if (binpow(i, j, p) == 1) {
				ok = false;
				break;
			}
		}
		if (ok) return i;
	}
	return -1;
}
int main() {
	int Q;
	cin >> Q;
	while (Q--) {
		int P, K, A;
		cin >> P >> K >> A;
		int g = find_primitive_root(P);
		int b = binpow(g, K, P);
		int bucket = 0;
		while (bucket * bucket < P) {
			++bucket;
		}
		int mul = 1;
		vector<int> val(bucket), perm(bucket);
		for (int i = 0; i < bucket; ++i) {
			val[i] = mul;
			perm[i] = i;
			mul = 1LL * mul * b % P;
		}
		sort(perm.begin(), perm.end(), [&](int i, int j) { return val[i] < val[j]; });
		int pmul = 1, ans = -1;
		mul = binpow(mul, P - 2, P);
		for (int i = 0; i < bucket; ++i) {
			int target = 1LL * A * pmul % P;
			int l = 0, r = bucket;
			while (r - l > 1) {
				int m = (l + r) >> 1;
				if (val[perm[m]] <= target) l = m;
				else r = m;
			}
			if (val[perm[l]] == target) {
				ans = binpow(g, i * bucket + perm[l], P);
				break;
			}
			pmul = 1LL * pmul * mul % P;
		}
		cout << ans << '\n';
	}
	return 0;
}
0