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;
}
View Code

 

posted @ 2017-12-21 10:58  啊宸  阅读(130)  评论(0编辑  收藏  举报