原根 51Nod - 1135

设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)

 
给出1个质数P,找出P最小的原根。

Input输入1个质数P(3 <= P <= 10^9)Output输出P最小的原根。Sample Input

3

Sample Output

2





只有1,2,4,p^a,2*p^a (p为奇素数)有原根

对phi(n)质因数分解
从2到phi(n)枚举,对于每一个i, 都有i^(phi(n)/(质因子))mod (n) != 1
则是一个 原根




 1 #include <stdio.h>
 2 #include <string.h>
 3 #include"vector"
 4 #include"iostream"
 5 #include <algorithm>
 6 using namespace std;
 7 #define ll long long
 8 #define int ll
 9 
10 vector<int >v;
11 
12 int ksm(int a,int b,int p)
13 {
14   int ans = 1;
15   for(;b;b>>=1,a*=a,a%=p)if(b&1)ans*=a,ans%=p;;
16   return ans;
17 }
18 signed main()
19 {
20     int n;cin>>n; n--;  int mod=n+1;
21     for(int i=2;i*i<=n;i++)
22     {
23         if(n%i==0)v.push_back(i);
24         while(n%i==0)n/=i;
25     }
26     if(n>1)v.push_back(n);
27    // for(auto i:v)cout<<i<<" ";
28    //cout<<ksm(2,5,11000);
29    for(int i=2;i;i++)
30    {
31        int f=1;
32        for(auto j:v)
33        {
34            if(ksm(i,(mod-1)/j,mod)==1){f=0;break;}
35        }
36        if(f==1){cout<<i;return 0;}
37    }
38 
39 
40 }





猜你喜欢

转载自www.cnblogs.com/zhangbuang/p/10929450.html