51nod 1135 原根

//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<cmath>
typedef long long LL;
using namespace std;
int p,d[100],mod;
template<typename T> void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
LL ksm(LL a,LL b) {
LL base=a,res=1;
while(b) {
if(b&1) (res*=base)%=mod;
(base*=base)%=mod;
b>>=1;
}
return res;
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
read(p);
mod=p; p--;
int nn=sqrt(p),tp=p;
for(int i=2;i<=nn;i++)
if(tp%i==0) {
d[++d[0]]=i;
while(tp%i)
tp/=i;
}
if(tp!=1) d[++d[0]]=tp;
for(int i=2;i<=p;i++) {
int fl=1;
for(int j=1;j<=d[0];j++) {
if(ksm(i,p/d[j])==1) {
fl=0;
break;
}
}
if(fl) {
printf("%d\n",i);
break;
}
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步