P11961 原根判断
这道题的题意很明确, 为质数 的原根有以下条件:
- 在 中,只有 时满足
会数学的应该都知道,如果 和 如果不互质,则第二个条件就不行。
接着,我们知道其实后两条指的是 在模 意义下的阶是
我们还知道, 在模 意义下的阶一定是 的因数,所以枚举即可。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int qpow(int x,int y,int r){//快速幂,用来验证a是否满足条件2
int res=1;
while(y){
if(y&1){
res=(res*x)%r;
}
x=(x*x)%r;
y>>=1;
}
return res;
}
signed main() {
int t;
cin>>t;
while(t--){
int a,p;
cin>>a>>p;
if(__gcd(a,p)!=1){
cout<<"No"<<endl;
continue;
}
int ans=2e9;
p--;//减一方便后续操作
for(int i=1;i*i<=p;i++){//枚举p-1的因数
if(qpow(a,i,p+1)==1){//分别判断i和(p-1)/i是否满足条件
ans=min(ans,i);//阶是因数中满足条件的最小值
}
if(qpow(a,p/i,p+1)==1){
ans=min(ans,p/i);
}
}
if(ans==p){//判断阶是否为p-1
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return 0;
}