zoukankan      html  css  js  c++  java
  • 原根 51Nod

    设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 }





  • 相关阅读:
    小米6刷机开启root权限
    C标准库堆内存函数
    UE4打印到HUD的Stat命令
    Blueprint Stats插件
    PC与Mobile硬件架构对比
    atomic原子编程中的Memory Order
    Windows10下开启D3D11的Debug Layer
    【数据结构】树存储结构
    【数据结构】哈希表
    【算法】查找算法
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/10929450.html
Copyright © 2011-2022 走看看