結果

問題 No.981 一般冪乗根
ユーザー mine691mine691
提出日時 2020-02-07 21:57:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,262 bytes
コンパイル時間 1,936 ms
コンパイル使用メモリ 166,156 KB
実行使用メモリ 12,320 KB
最終ジャッジ日時 2024-04-17 20:12:32
合計ジャッジ時間 97,636 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
evil_60bit1.txt TLE -
evil_60bit2.txt TLE -
evil_60bit3.txt TLE -
evil_hack WA -
evil_hard_random TLE -
evil_hard_safeprime.txt TLE -
evil_hard_tonelli0 WA -
evil_hard_tonelli1 WA -
evil_hard_tonelli2 WA -
evil_hard_tonelli3 WA -
evil_sefeprime1.txt TLE -
evil_sefeprime2.txt TLE -
evil_sefeprime3.txt TLE -
evil_tonelli1.txt WA -
evil_tonelli2.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define digit(N) cout << fixed << setprecision((N))

using ll = long long;

template <typename T>
T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }

ll powmod(ll a, ll b, ll p)
{
	ll res = 1;
	while (b)
		if (b & 1)
			res = ll(res * 1ll * a % p), --b;
		else
			a = ll(a * 1ll * a % p), b >>= 1;
	return res;
}

ll generator(ll p)
{
	vector<ll> fact;
	ll phi = p - 1, n = phi;
	for (int i = 2; i * i <= n; ++i)
		if (n % i == 0)
		{
			fact.push_back(i);
			while (n % i == 0)
				n /= i;
		}
	if (n > 1)
		fact.push_back(n);

	for (int res = 2; res <= p; ++res)
	{
		bool ok = true;
		for (size_t i = 0; i < fact.size() && ok; ++i)
			ok &= powmod(res, phi / fact[i], p) != 1;
		if (ok)
			return res;
	}
	return -1;
}

int main()
{
	int Q;
	cin >> Q;
	ll p, n, a;
	while (Q--)
	{
		cin >> p >> n >> a;
		ll d = gcd(n, p - 1);
		ll k = powmod(a, (p - 1) / d, p);
		if (a == 0)
		{
			cout << 0 << "\n";
		}
		else if (k != 0 && k != 1)
		{
			cout << -1 << "\n";
		}
		else
		{
			ll g = generator(p);
			cout << g << "\n";
		}
	}
}
0