What is Modular Multiplicative Inverse? #
If a⋅x≡1(modp), x is called a inverse of a(modulo p), referred to as a−1. We usually use the minimum positive inverse.
The use of Inverse #
The inverse is used when calculating the modulo of division.
ba≡a⋅b−1(modp)
The ways to calculate the inverse of a number #
The Extended Euclidean algorithm #
We can rewrite a⋅x≡1(modp) as a⋅x+p⋅k≡gcd(p,a)(modp) which can be solved using the Extended Euclidean algorithm.
The Fermat’s Little Theorem #
According to Fermat’s Little Theorem ap−1≡1(modp), thus a⋅x≡ap−1(modp), x≡ap−2(modp). We can calculate it using Exponentiation by squaring.
Calculate consecutive inverses in linear time #
Modulo of Combinations #
Calculate (mn)modp
When n and m are not too big #
We can use the inverse to calculate m!⋅(n−m)!n!≡(n!modp⋅(m!modp)−1⋅((n−m)!modp)−1)(modp)
Calculate the inverse of factorial #
∵n!⋅(n!)−1≡1(modp)
∴(n−1)!⋅(n⋅(n!)−1)≡1(modp)
Therefore(n⋅(n!)−1)is an inverse of (n−1)!.
When n and m are really big but p is not too big #
(mn)modp=(⌊pm⌋⌊pn⌋)(mmodpnmodp)modp