跳到正文

题解

题解:P11961 [GESP202503 五级] 原根判断

MitchellZhong约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年3月24日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P11961

版本

  1. v1当前

P11961 原根判断

这道题的题意很明确,aa 为质数 bb 的原根有以下条件:

  1. a<ba<b
  2. ab−1≡1( mod  b)a^{b-1}≡1\left(\bmod\ b \right)
  3. 在 i=1∼b−1i=1 \sim b-1 中,只有 i=b−1i=b-1 时满足 ai≡1( mod  b)a^i≡1\left(\bmod\ b \right)

会数学的应该都知道,如果 aa 和 bb 如果不互质,则第二个条件就不行。

接着,我们知道其实后两条指的是 aa 在模 bb 意义下的阶是 b−1b-1

我们还知道,aa 在模 bb 意义下的阶一定是 b−1b-1 的因数,所以枚举即可。

code

C++
#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;
}
原文