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

题目链接: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过的。。。
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 int main ()
 6 {
 7     int n;
 8     while (cin>>n)
 9     {
10         if (n%2&&n>1)
11         {
12             int s=1,x=1;
13             while (x)
14             {
15                 s=s*2%n;
16                 if (s==1)
17                 {
18                     printf ("2^%d mod %d = 1
",x,n);
19                     break;
20                 }
21                 x++;
22             }
23         }
24         else
25             printf ("2^? mod %d = 1
",n);
26     }
27     return 0;
28 }
原文地址:https://www.cnblogs.com/qq-star/p/3930924.html