hdu 1395 2^x mod n = 1(暴力题)

时间:2021-08-19 07:24:23

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395

2^x mod n = 1

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

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
 
题目大意:暴力搜索,找到合适的X值,这一题可以采取反过来暴力寻找,这一简单易懂些。
要注意的是输出的值时都要变化的,输出注意一下就好了,毕竟我是wa过的。。。
 #include <iostream>
#include <cstdio>
using namespace std; int main ()
{
int n;
while (cin>>n)
{
if (n%&&n>)
{
int s=,x=;
while (x)
{
s=s*%n;
if (s==)
{
printf ("2^%d mod %d = 1\n",x,n);
break;
}
x++;
}
}
else
printf ("2^? mod %d = 1\n",n);
}
return ;
}