hey guys :D
I have this small math problem,
( a * b ) mod n = 1
Given a, n:
find the smallest value of 'b' that satisfies the equation above.
any Idea about how to find the answer better than linear time search.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
hey guys :D
I have this small math problem,
( a * b ) mod n = 1
Given a, n:
find the smallest value of 'b' that satisfies the equation above.
any Idea about how to find the answer better than linear time search.
Name |
---|
http://en.wikipedia.org/wiki/Modular_multiplicative_inverse
this is the same as finding the inverse of A (mod C) right ??
Yes, it's the same. Wikipedia article has some examples.
one more question please, who to choose the proper values for a, n
ikatanic any constraints on values other than 'a' and 'n' should be co primes ??
Nope. Extended Euclidean algorithm is used to solve the equation Ax+By=GCD(A,B) and since GCD(a,n)=1 the equation a*b-k*n=1=GCD(a,n) has infinitely many solutions.
But I found this statement on some website:
"Only the numbers coprime to C (numbers that share no prime factors with C) have a modular inverse (mod C)"
this is for the formula above in my post.
You are not right. GCD(a, n) must be equal to 1, otherwise it has no solution. You can read here
He knows that, I just said that there is not another constraint.
(a*b) mod n = 1 means that (a*b) = k*n+1 for some integer k>=0.
a*b-k*n = 1 is an equation of the form Ax+By=C which can be solved using Extended Euclidean algorithm.
Update: When I started writing my comment I didn't see that ikatanic has commented, sorry :)
P_Nyagolov thank you and ikatanic :D