2 ^ x mod n = 1
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 11542 accepted submission (s): 3577
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 Input
25
Sample output
2^? mod 2 = 12^4 mod 5 = 1
Authorma, Xiao
Sourcezoj monthly, February 2003
Recommendignatius. L | we have carefully selected several similar problems for you: 2462 2421 1695 2466 2447 // similar to the previous question. This question tells us that the base number is 2 and the minimum number is 2 ^ xmod n = 1 and the solution is the same.
# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> using namespace STD; const int max = 1000010; int prime [Max], phi [Max]; // Euler's function for saving all values void fun () // Euler's function for finding all values from 1 to max {int I, J; prime [0] = prime [1] = 0; for (I = 2; I <= max; I ++) prime [I] = 1; for (I = 2; I * I <= max; I ++) if (prime [I]) for (Int J = I * I; j <= max; j + = I) prime [J] = 0; for (I = 1; I <= max; I ++) Phi [I] = I; for (I = 2; I <= max; I ++) if (prime [I]) for (j = I; j <= max; j + = I) Phi [J] = Phi [J]/I * (I-1);} int Mod (int A, int B, int c) // fast idempotent modulo {int ans = 1; _ int64 AA = A; while (B) {If (B % 2) ans = ans * Aa % C; AA = AA * Aa % C; B/= 2;} return ans;} int main () {// freopen ("a.txt ", "r", stdin); // freopen ("B .txt", "W", stdout); fun (); int A, N, I; while (scanf ("% d", & N )! = EOF) {int Sn = (INT) SQRT (PHI [N]), ANS = N; // enumerate the Euler's factor of N between 1-sn (I = 1; I <= Sn; I ++) // find all the factors in the Euler's function that meet the conditions and are the smallest. {If (PHI [N] % I = 0) // If I is a factor of Phi, update the minimum value {If (mod (2, I, n) = 1 & Ans> I) ans = I; If (mod (2, Phi [N]/I, n) = 1 & Ans> Phi [N]/I) ans = Phi [N]/I;} If (ANS = N) printf ("2 ^? MoD % d = 1 \ n ", n); else printf (" 2 ^ % d mod % d = 1 \ n ", ANS, N ); // cout <ans <Endl;} return 0 ;}
Hdu-1395 2 ^ x mod n = 1