結果

問題 No.981 一般冪乗根
ユーザー mine691
提出日時 2020-02-07 21:57:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,262 bytes
コンパイル時間 1,599 ms
コンパイル使用メモリ 170,248 KB
実行使用メモリ 10,148 KB
最終ジャッジ日時 2024-10-09 14:16:16
合計ジャッジ時間 98,506 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 36 TLE * 8
権限があれば一括ダウンロードができます

ソースコード

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