51nod 1135 原根问题
来源:互联网 发布:开心贷网络借贷 编辑:程序博客网 时间:2024/06/02 06:53
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
题解:
参考http://blog.csdn.net/u013486414/article/details/47781857
百度百科讲解http://blog.csdn.net/u013486414/article/details/47781857
代码参考逐梦者
代码:
#include <stdio.h>#include <math.h>#define _MAX 10000005int P, tot;int A[_MAX];//已知质数P,无论g为何值,在指数为P-1时结果均为成立,//如果指数不为P-1时也想满足g^i = 1(mod P),则该指数一定是P-1的因子//并且指数要小于sqrt(P-1)void store(){ int tmp, now; tmp = (int)((double)sqrt(P - 1) + 1); tot = 0; now = P - 1; for (int i = 2; i <= tmp; i++) { if (now % i == 0) { A[tot++] = i; } while (now % i == 0) { now /= i; } } if (now != 1) { A[tot++] = now; } return ;}//快速幂取模Plong long QPow(long long x, long long n){ long long ret = 1; while (n) { if (n & 1) { ret = ret * x % P; } n >>= 1; x = x * x % P; } return ret;}//检测是否满足g^i = 1(mod P)当且仅当指数为P-1时成立,0<i<P,1<g<P//因为P为质数,所以可以肯定,无论g为什么,当指数为P-1时一定成立,省去判断//只需要判断其他情况不等于1int check(int g){ for (int i = 0; i < tot; i++) { if (QPow((long long)g, (long long)(P - 1) / A[i]) == 1) { return 0; } } return 1;}//封装函数,1<g<Pint solve(){ for (int g = 2; g < P; g++) { if (check(g)) { return g; } } return 0;}int main(){ while (~scanf("%d", &P)) { store(); printf("%d\n", solve()); } return 0;}
阅读全文
0 0
- 51nod 1135 原根问题
- 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 原根
- An introduction to my blog
- 【Java】并发基础知识总结
- java多线程详解(7)-线程池的使用
- SpringCloud基础(2)
- PAT 甲级 1009. Product of Polynomials (25)
- 51nod 1135 原根问题
- java调用命令行执行mysqldump和执行sql文件恢复命令
- 生成SGML格式,用于算BLEU,NIST,TER
- redis常用命令、常见错误、配置技巧等分享
- Tomcat的总结
- tomee 创建 xa 数据源 并 支持 JTA 事务
- 实体类编写规则
- iOS多线程NSThread/GCD/NSOperation区别和使用
- 基于PCL和Kinect的图像采集和点云生成