杭电 HDU ACM 1395 2^x mod n = 1
时间:2015-04-05 09:06:40
收藏:0
阅读:104
2^x mod n = 1
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13610 Accepted Submission(s): 4208
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.
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
Author
MA, Xiao
对x递增,每步取模,ls数组记录是否此余数出现过,如果出现过,那么接下来必将循环,所以出现重复即跳出即可。
#include<iostream> using namespace std; int main() { int n,i,j; while(cin>>n) { int ls[10000];int sum=1; memset(ls,0,sizeof(ls)); for(i=1; ;i++) { sum*=2; sum%=n; if(sum==1) { cout<<"2^"<<i<<" mod "<<n<<" = 1"<<endl; break; } if(ls[sum]) { cout<<"2^? mod "<<n<<" = "<<"1"<<endl; break; } ls[sum]=1; } } return 0; }
评论(0)