2^x mod n = 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15197    Accepted Submission(s): 4695

Problem Description
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
 
Input
One positive integer on each line, the value of n.
 
Output
If the minimum x exists, print a line with 2^x mod n = 1.

Print 2^? mod n = 1 otherwise.

You should replace x and n with specific numbers.

 
Sample Input
2
5
 
Sample Output
2^? mod 2 = 1
2^4 mod 5 = 1
题目不难,就是要知道取模运算的基本法则这题主要是(a*b)%c=(a%c * b%c)%c.
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
int n;
while (cin>>n)
{
if(n%2==0||n==1)//n为1或者偶数一定无解
printf("2^? mod %d = 1\n",n);
else//奇数一定有解
{
int ans=1,t=2;
while (t%n!=1)
{
t=(t*2)%n;//每次都求余数据就不会溢出
ans++;
}
printf("2^%d mod %d = 1\n",ans,n);
}
}
return 0;
}

最新文章

  1. java遍历给定目录,树形结构输出所有文件,包括子目录中的文件
  2. Python爬虫利器三之Xpath语法与lxml库的用法
  3. 转!!log4j基础
  4. SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN
  5. PHP Startup: Unable to load dynamic library
  6. c#equals相关
  7. 总结几种C#窗体间通讯的处理方法
  8. POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
  9. POJ题目(转)
  10. gdb的多线程调试
  11. CefSharp Cookie独立 GetGlobalCookieManager
  12. .NET Core跨平台的奥秘[中篇]:复用之殇
  13. css 溢出overflow
  14. PL/SQL变量和类型
  15. gcc下inline的一个问题
  16. mysql学习【第3篇】:使用DQL查询数据
  17. day 11 前方高能-迭代器
  18. mfc 动态为控件添加事件2
  19. HDU 2235
  20. Solr4.0+IKAnalyzer中文分词安装

热门文章

  1. 问题驱动的Git学习
  2. XPath语法规则及实例
  3. Django 从0开始创建一个项目
  4. Bootstrap历练实例:下拉菜单插件方法的使用
  5. lua 使用正则表达式分割字符串
  6. UICollectionView实现无限轮播
  7. NOIP模拟赛 篮球比赛2
  8. 救援(BFS)
  9. 【Python学习之五】高级特性5(切片、迭代、列表生成器、生成器、迭代器)
  10. centos里没有pip命令怎么办?