1 /* 2 题意:求一个数x的原根 3 题解:x=p1^e1 * p2^e2 * p3^e3 * pm^em 4 依次判断 a^((x-1)/pi) == 1 mod x 是否等于1,如果都不等于1那么a是原根。 5 时间:2018.07.26 6 */ 7 8 #include <bits/stdc++.h> 9 using namespace std; 10 11 typedef long long LL; 12 const int MAXN = 100005; 13 const LL MOD7 = 1e9+7; 14 15 LL pow(LL a,LL b, LL P) 16 { 17 // printf("a=%lld b=%lld P=%lld\n",a,b,P); 18 LL res=1LL; 19 LL ans=a%P; 20 while (b) 21 { 22 if (b&1) res=res*ans%P; 23 ans=ans*ans%P; 24 b>>=1; 25 } 26 return res; 27 } 28 29 30 int prime[MAXN]; 31 int cnt; 32 33 void initPrime() 34 { 35 cnt=0; 36 for (int i=2;i<MAXN;++i) 37 { 38 if (!prime[i]) prime[cnt++]=i; 39 for (int j=0;j<cnt&&i*prime[j]<MAXN;++j) 40 { 41 prime[i*prime[j]]=1; 42 if (i%prime[j]==0) break; 43 } 44 } 45 } 46 47 int fac[200]; 48 int num[200]; 49 int fnum; 50 51 void Fac(int x) 52 { 53 fnum=0; 54 for (int i=0;i<cnt&&prime[i]<=x;++i) 55 { 56 int tmp=0; 57 while (x%prime[i]==0) 58 { 59 ++tmp; 60 x/=prime[i]; 61 } 62 if (tmp) 63 { 64 fac[fnum] = prime[i]; 65 num[fnum++] = tmp; 66 } 67 } 68 if (x!=1) 69 { 70 fac[fnum]=x; 71 num[fnum++]=1; 72 } 73 } 74 75 bool check(int a,int x) 76 { 77 for (int i=0;i<fnum;++i) 78 { 79 if (pow((LL)a,(LL)(x-1)/fac[i],(LL)x)==1LL) return false; 80 } 81 return true; 82 } 83 84 int x; 85 86 87 int main() 88 { 89 #ifndef ONLINE_JUDGE 90 // freopen("test.txt","r",stdin); 91 #endif // ONLINE_JUDGE 92 initPrime(); 93 while (scanf("%d",&x)!=-1) 94 { 95 Fac(x-1); 96 for (int i=2;i<=x-1;++i) 97 { 98 if (check(i, x)) 99 { 100 printf("%d\n",i); 101 break; 102 } 103 } 104 // printf("%d\n",x); 105 } 106 return 0; 107 }
求原根
猜你喜欢
转载自www.cnblogs.com/LeeSongt/p/9374531.html
今日推荐
周排行