51Nod 1135 原根
来源:互联网 发布:php unpack 二进制 编辑:程序博客网 时间:2024/06/08 03:37
1135 原根
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
收藏
关注
设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)
给出1个质数P,找出P最小的原根。
Input
输入1个质数P(3 <= P <= 10^9)
Output
输出P最小的原根。
Input示例
3
Output示例
2
附上链接:点击打开链接
结论为:原根为a,若a^((p-1)/pk)恒不等于1,pk为p的质因子,则a为p的原根;
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int MAX=1000010;int prime[MAX];int sprime[MAX];int pri[MAX];int b,v;void isprime(){memset(pri,1,sizeof(pri));for(int i=2;i<MAX;i++){if(pri[i]){prime[b++]=i;for(int j=i+i;j<MAX;j+=i)pri[j]=0;}}}void DIVIDE(int n){v=0;int t=(int)sqrt(1.0*n);for(int i=0;i<t;i++){if(n%prime[i]==0){sprime[v++]=prime[i];while(n%prime[i]==0) n/=prime[i];}}if(n>1) sprime[v++]=n;} ll KSM(ll a,ll b,ll c){ll ans=1;a=a%c;while(b>0){if(b&1)ans=(ans*a)%c;b/=2;a=(a*a)%c;}return ans;}int main(){int p;isprime();scanf("%d",&p);DIVIDE(p-1);for(int i=2;i<p;i++){int flag=1;for(int j=0;j<v;j++){int k=(p-1)/sprime[j];if(KSM(i,k,p)==1) {flag=0;break;}}if(flag){int u=i;printf("%d\n",u);break;}}return 0;}
阅读全文
0 0
- 51nod 1135 原根
- 51nod 1135 原根
- 【51nod 1135 原根】
- 51nod-1135:原根
- 51nod 1135 原根
- 51nod 1135 原根
- 51nod 1135 原根
- 51nod 1135 原根
- 51 Nod 1135 原根
- 51 NOD 1135 原根
- 51nod 1135原根
- 51Nod 1135 原根
- 51nod 1135 原根
- 51nod 1135 原根
- 51Nod-1135-原根
- 51nod 1135 原根
- 51Nod 1135 原根
- 51Nod-1135-原根
- Codeforces Round #420 (Div. 2) D. Okabe and City(最短路)
- 算法-->反序(插入)
- Centos中Qt编译问题(/usr/bin/ld: 找不到 -lpulse-mainloop-glib,/usr/bin/ld: 找不到 -lpulse...)
- POJ
- URG-PSH
- 51Nod 1135 原根
- Java中状态模式介绍
- Linux——文件系统中inode的工作
- Git版本控制工具和Github代码托管平台
- POJ
- require js(一)
- Django笔记
- 机器学习
- LAMP架构学习(2) PHP的配置与使用