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