結果

問題 No.981 一般冪乗根
ユーザー square1001square1001
提出日時 2020-02-10 19:12:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,410 bytes
コンパイル時間 1,035 ms
コンパイル使用メモリ 86,508 KB
実行使用メモリ 11,720 KB
最終ジャッジ日時 2024-04-17 21:52:13
合計ジャッジ時間 78,096 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5,995 ms
6,812 KB
testcase_01 AC 5,962 ms
6,812 KB
testcase_02 AC 5,927 ms
10,268 KB
testcase_03 TLE -
testcase_04 AC 5,952 ms
6,948 KB
testcase_05 AC 178 ms
6,944 KB
testcase_06 AC 156 ms
6,940 KB
testcase_07 AC 170 ms
6,944 KB
testcase_08 AC 159 ms
6,944 KB
testcase_09 AC 161 ms
6,940 KB
testcase_10 AC 168 ms
6,944 KB
testcase_11 AC 176 ms
6,940 KB
testcase_12 AC 177 ms
6,944 KB
testcase_13 AC 167 ms
6,944 KB
testcase_14 AC 160 ms
6,940 KB
testcase_15 AC 161 ms
6,944 KB
testcase_16 AC 160 ms
6,944 KB
testcase_17 AC 154 ms
6,944 KB
testcase_18 AC 166 ms
6,940 KB
testcase_19 AC 157 ms
6,944 KB
testcase_20 AC 155 ms
6,944 KB
testcase_21 AC 175 ms
6,944 KB
testcase_22 AC 162 ms
6,940 KB
testcase_23 AC 165 ms
6,940 KB
testcase_24 AC 156 ms
6,948 KB
testcase_25 AC 5,523 ms
6,940 KB
testcase_26 TLE -
testcase_27 AC 6 ms
6,944 KB
testcase_28 AC 4,392 ms
6,944 KB
evil_60bit1.txt TLE -
evil_60bit2.txt TLE -
evil_60bit3.txt TLE -
evil_hack AC 3 ms
6,944 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>
#include <unordered_map>
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;
		unordered_map<int, int> d;
		for (int i = 0; i < bucket; ++i) {
			if (d.find(mul) == d.end()) {
				d[mul] = i;
			}
			mul = 1LL * mul * b % P;
		}
		int pmul = 1, ans = -1;
		mul = binpow(mul, P - 2, P);
		for (int i = 0; i < bucket; ++i) {
			int target = 1LL * A * pmul % P;
			if (d.find(target) != d.end()) {
				ans = binpow(g, i * bucket + d[target], P);
				break;
			}
			pmul = 1LL * pmul * mul % P;
		}
		cout << ans << '\n';
	}
	return 0;
}
0