简体   繁体   中英

How to convert a double value into P.Q^-1 modulo MOD directly, where q is coprime with MOD

I am dealing with a probability question where the probability can be expressed as a fraction P/Q, where P and Q are integers (P≥0, Q>0) and Q is co-prime with 998,244,353. You should compute P⋅Q^−1 modulo 998,244,353.

You can't. Floating point values are not exact, so this is like trying to convert a decimal into a fraction after you rounded it. You need to do your calculations mod 998244353 starting from the beginning, and instead of dividing you multiply by the modular inverse. It can be proven that doing this is equivalent to doing all the calculations using exact fractions and converting to modular form at the very end.

you can use Fermat's little theorem

this might be helpful if you are looking for code

This can be solved using Extended Euclidean Algorithm . You can visit the following link for further info and code solutions:

https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM