2^x mod n = 1
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 12605 Accepted Submission (s): 3926
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
Author
MA, Xiao
Source
ZOJ Monthly, February 2003
To give you a number n, to determine if there is x, meet 2^x mod N = 1. If
Satisfied, for the minimum x that satisfies the condition, the output 2^x mod N = 1, otherwise the output
2^? MoD 2 = 1.
Idea: The law of multiplication inverse is used in number theory.
Multiplication inverse: For integers a, p if there is an integer b, satisfies a*b mod p = 1, it is said
b is the multiplicative inverse of the modulus p of a. A necessary and sufficient condition of the multiplicative inverse of the existential modulus P is gcd (a,p) = 1
In this problem, make a = 2^x,b = 1,p = N, then if X is present, 2^x mod n = 1,
Then gcd (2^x,n) = 1.
1> because n>0, when N is even, gcd (2^x,n) = 2*k (k=1,2,3 ...), does not meet
2> When N is odd, gcd (2^x,n) = 1 satisfies the condition.
3> When n is 1 o'clock, 2^x mod n = 0, does not meet the criteria
So n is odd, and not 1, satisfies 2^x mod n = 1, brute force solver.
Reference post: http://blog.csdn.net/mxway/article/details/8954364
#include <stdio.h>int main () { int n; while (~SCANF ("%d", &n)) { if (n==1 | |!) ( n&1) { printf ("2^? MoD%d = 1\n ", n); } else { int ans = 2,num = 1; while (ans!=1) { ans = ans * 2 n; num++; } printf ("2^%d mod%d = 1\n", num,n); } } return 0;}
HDU1395_2^X mod n = 1 "number theory" "Water problem"