Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 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 descriptiongive a number N, find the minimum x (x> 0) that satisfies 2 ^ x mod n = 1.
Inputone positive integer on each line, the value of N.
Outputif the minimum x exists, print a line with 2 ^ x mod n = 1.
Print 2 ^? MOD n = 1 otherwise.
You shoshould replace X and N with specific numbers.
Sample input25 sample output2 ^? MoD 2 = 12 ^ 4 mod 5 = 1 question: Brute Force Search, find the appropriate x value, this question can be used in turn to find brute force, this is easy to understand. It should be noted that all output values must be changed. Just pay attention to the output. After all, I have 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\n",x,n);19 break;20 }21 x++;22 }23 }24 else25 printf ("2^? mod %d = 1\n",n);26 }27 return 0;28 }
HDU 1395 2 ^ x mod n = 1 (violent question)