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 | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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