2^x mod n = 1(hd1395)

2^x mod n = 1

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


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
 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n,i,j;
 6     while(cin>>n)
 7     {
 8         int x=2;
 9         if(n==1||n%2==0)
10         {
11             cout<<"2^? mod "<<n<<" = 1\n";
12             continue;
13         }
14         for(i=0;;i++)
15         {
16             if(x==1)
17                 break;
18             else
19             {
20                 x=x*2;
21                 x=x%n;
22             }
23         }
24         cout<<"2^"<<i+1<<" mod "<<n<<" = 1"<<endl;
25     }
26 }

 

 
posted @ 2015-07-28 15:21  御心飞行  阅读(399)  评论(0编辑  收藏  举报