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 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 should replace x and n with specific numbers.
Sample Input
25
Sample Output
2^? MoD 2 = 12^4 MoD 5 = 1
Authorma, Xiao to x increments, each step modulo, ls array records whether this remainder has occurred, if there has been, then the next bound to loop, so there is a repetition can jump.
#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;}
Hangzhou Electric HDU ACM 1395 2^x mod n = 1