题解:P11961 [GESP202503 五级] 原根判断
Redshift_Shine · · 题解
明天就要月考了,但是看到这道题还是想写一下。
然后,思考时间 3min,切。
理论上,以下解法并不要求对“原根”这一概念有额外的事先理解。
首先,根据费马小定理,对于任意质数
随后,可以证明,对于任何小于
由此,解法显然。枚举
时间复杂度
// #define Redshift_Debug
#ifdef Redshift_Debug
#define debug(...) fprintf(stderr, __VA_ARGS__)
#include <chrono>
#else
#define debug(...)
#endif
#include <cstdio>
using namespace std;
const int N = 1e5 + 10;
int a, p;
inline int fsp(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = 1ll * res * x % p;
x = 1ll * x * x % p;
y >>= 1;
}
return res;
}
void init_global()
{
}
void init_local()
{
scanf("%d%d", &a, &p);
}
void run()
{
for (int i = 2; i * i <= p - 1; i++)
{
if ((p - 1) % i)
continue;
if (fsp(a, i) == 1 or fsp(a, (p - 1) / i) == 1)
{
puts("No");
return;
}
}
puts("Yes");
}
int main()
{
#ifdef Redshift_Debug
auto st = chrono::high_resolution_clock::now();
#endif
int T = 1;
scanf("%d", &T);
init_global();
while (T--)
{
init_local();
run();
}
#ifdef Redshift_Debug
auto ed = chrono::high_resolution_clock::now();
fprintf(stderr, "%.9lf\n", (ed - st).count() / 1e9);
#endif
}