【HDU】 1395 2^x mod n = 1
2016-05-30 20:21
375 查看
2^x mod n = 1
题目链接
2^x mod n = 1题目大意
找到一个最小的x满足2x mod n = 1如果没有则输出2? mod n = 1题解
我们可以把式子化为(2∗2x−1) mod n = 1 首先可以看到2x是个偶数,要想让该式成立肯定n不能为偶数或者是1,有了这个结论后我们就可以把式子化为(2∗(2x−1 mod n))mod n = 1于是我们设F(x−1) = 2x−1mod n 所以我们现在有F(x)=(2∗F(x−1))mod n
一直推直到F(x)为1就行了。
代码
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int n; int main() { while(scanf("%d",&n)!=EOF) { if (n%2==0 || n==1) { printf("2^? mod %d = 1\n",n); continue ; } else { int ans=1,cnt=0; while (ans!=1 || cnt==0) { cnt++; ans=(ans*2)%n; } printf("2^%d mod %d = 1\n",cnt,n); } } return 0; }
相关文章推荐
- poj 1511 spfa入门
- 第2章:算法基础
- NoSql之Redis数据类型
- hdu-1071-The area-简单积分
- Web页面请求的历程(四)
- 用httping测试WEB页面响应时间
- Easy-题目34:119. Pascal's Triangle II
- python之路
- Dwarf Error: wrong version in compilation unit header (is 4, should be 2)
- CABasicAnimation动画--lable字体大小改变
- HDU 5713 & 2016"百度之星" 复赛(Astar Round3)1002 k个联通块
- linux文件系统启动流程 ---笔记整理
- 一定要战斗下去
- 《JavaScript学习笔记九》:延时框的实现
- pull解析
- hdu3437 划分树 区间内小于第K大的值得和
- MYSQL的创建数据表及测试数据类型
- Easy-题目33:102. Binary Tree Level Order Traversal
- 《JavaScript学习笔记八》:数字时钟的实现
- 在Ubuntu系统下装Win7并引导双系统