I need to find Res = (A / B) % P. (P is prime).
I have Num = A % P, and Den = B % P.
Is there any way to find Res just by using Num, Den and P?
I came across this :
(a / b) mod p = ((a mod p) * (b^(-1) mod p)) mod p
i.e. Res = (Num * b ^ (p - 2) % p) % p
Now how can I find b^(p-2) % p using Den?
If you can provide me with a C++/C code, I would be more than happy, as I can then directly use it in my game, otherwise, please help me in finding a formula, so that I can obtain the Res on my own.
You can get the result of b^(p-2) % p using quick exponentiation.
int qexp (int b, int e) {
if (e == 0) return 1;
long long h = qexp(b, e/2); //long long to prevent overflow in next step
h *= h;
h %= p;
if (e % 2 == 0) return h;
else return (h*b)%p;
}
call using qexp(b, p-2);
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.