Блог пользователя cartesian_bearrr

Автор cartesian_bearrr, история, 3 года назад, По-английски

Recently, I saw a problem asking to output the fractional answer p/q as pq^−1 mod M, where p and q are relatively prime in the fraction and M was some prime modulus. My approach was to compute the numerator p and the denominator q separately, and then output p mod M * q^-1 mod M. However, how can I ensure that p and q is relatively prime with this approach? As p and q get gigantic, their factors are lost during the mod, and I can not ensure that p and q are relatively prime.

Thank for reading, and if the question is hard to understand I can clarify.

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Module inversion is your friend.

»
3 года назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится

It actually doesn't matter — p and q don't have to be relatively prime. Do you understand general multiplicative inversion concept? If not, i think, you have to read something about it (there are plenty of topics). Otherwise, write me back :)

»
3 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Lets call the numerator you calculate $$$m$$$ and the denominator you calculate $$$n$$$, and call $$$\frac{p}{q}$$$ the simplified form with $$$\frac{p}{q} = \frac{m}{n}$$$. Then, you don't actually have to do anything special since $$$mn^{-1} \equiv pq^{-1} \mod M$$$. Specifically you can just use $$$mn^{-1}$$$ mod $$$M$$$ and ignore the part saying that they have to be relatively prime, since the numbers that are relatively prime will get the same result.

This is because there is some $$$a$$$ such that $$$m = ap$$$ and $$$n = aq$$$. Since $$$a \cdot a^{-1} \equiv 1 \mod M$$$, $$$pq^{-1} \equiv (pq^{-1})\cdot(aa^{-1}) \equiv (pa)\cdot(qa)^{-1} \equiv mn^{-1} \mod M$$$.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Thank you so much guys!! This helps me so much and I understand it now.