Post · 6 min read· pp. 64–67

Modular Multiplicative Inverse

Definition

A modular multiplicative inverse of an integer aa is an integer xx such that a⋅xa \cdot x is congruent to 11 modular some modulus mm. To write it in a formal way: we want to find an integer xx so that

a⋅x≡1mod  m.a \cdot x \equiv 1 \mod m.

We will also denote xx simply with a−1a^{-1}.

We should note that the modular inverse does not always exist. For example, let m=4m = 4, a=2a = 2. By checking all possible values modulo mm, it should become clear that we cannot find a−1a^{-1} satisfying the above equation. It can be proven that the modular inverse exists if and only if aa and mm are relatively prime (i.e. gcd⁡(a,m)=1\gcd(a, m) = 1).

In this article, we present two methods for finding the modular inverse in case it exists, and one method for finding the modular inverse for all numbers in linear time.

Finding the Modular Inverse using Extended Euclidean algorithm

Consider the following equation (with unknown xx and yy):

a⋅x+m⋅y=1a \cdot x + m \cdot y = 1

This is a Linear Diophantine equation in two variables. As shown in the linked article, when gcd⁡(a,m)=1\gcd(a, m) = 1, the equation has a solution which can be found using the extended Euclidean algorithm. Note that gcd⁡(a,m)=1\gcd(a, m) = 1 is also the condition for the modular inverse to exist.

Now, if we take modulo mm of both sides, we can get rid of m⋅ym \cdot y, and the equation becomes:

a⋅x≡1mod  ma \cdot x \equiv 1 \mod m

Thus, the modular inverse of aa is xx.

The implementation is as follows:

text
int x, y;
int g = extended_euclidean(a, m, x, y);
if (g != 1) {
    cout << "No solution!";
}
else {
    x = (x % m + m) % m;
    cout << x << endl;
}

Notice that the way we modify x. The resulting x from the extended Euclidean algorithm may be negative, so x % m might also be negative, and we first have to add m to make it positive.

Finding the Modular Inverse using Binary Exponentiation

Another method for finding modular inverse is to use Euler's theorem, which states that the following congruence is true if aa and mm are relatively prime:

aϕ(m)≡1mod  ma^{\phi (m)} \equiv 1 \mod m

ϕ\phi is Euler's Totient function. Again, note that aa and mm being relative prime was also the condition for the modular inverse to exist.

If mm is a prime number, this simplifies to Fermat's little theorem:

am−1≡1mod  ma^{m - 1} \equiv 1 \mod m

Multiply both sides of the above equations by a−1a^{-1}, and we get:

  • For an arbitrary (but coprime) modulus mm: aϕ(m)−1≡a−1mod  ma ^ {\phi (m) - 1} \equiv a ^{-1} \mod m
  • For a prime modulus mm: am−2≡a−1mod  ma ^ {m - 2} \equiv a ^ {-1} \mod m

From these results, we can easily find the modular inverse using the binary exponentiation algorithm, which works in O(log⁡m)O(\log m) time.

Even though this method is easier to understand than the method described in previous paragraph, in the case when mm is not a prime number, we need to calculate Euler phi function, which involves factorization of mm, which might be very hard. If the prime factorization of mm is known, then the complexity of this method is O(log⁡m)O(\log m).

Finding the modular inverse for prime moduli using Euclidean Division

Given a prime modulus m>am > a (or we can apply modulo to make it smaller in 1 step), according to Euclidean Division

m=k⋅a+rm = k \cdot a + r

where k=⌊ma⌋k = \left\lfloor \frac{m}{a} \right\rfloor and r=m mod ar = m \bmod a, then

  ⟹  0≡k⋅a+rmod  m  ⟺  r≡−k⋅amod  m  ⟺  r⋅a−1≡−kmod  m  ⟺  a−1≡−k⋅r−1mod  m\begin{align*} & \implies & 0 & \equiv k \cdot a + r & \mod m \\ & \iff & r & \equiv -k \cdot a & \mod m \\ & \iff & r \cdot a^{-1} & \equiv -k & \mod m \\ & \iff & a^{-1} & \equiv -k \cdot r^{-1} & \mod m \end{align*}

Note that this reasoning does not hold if mm is not prime, since the existence of a−1a^{-1} does not imply the existence of r−1r^{-1} in the general case. To see this, lets try to calculate 5−15^{-1} modulo 1212 with the above formula. We would like to arrive at 55, since 5⋅5≡1 mod 125 \cdot 5 \equiv 1 \bmod 12. However, 12=2⋅5+212 = 2 \cdot 5 + 2, and we have k=2k=2 and r=2r=2, with 22 being not invertible modulo 1212.

If the modulus is prime however, all aa with 0<a<m0 < a < m are invertible modulo mm, and we can have the following recursive function (in C++) for computing the modular inverse for number aa with respect to mm

text
int inv(int a) {
  return a <= 1 ? a : m - (long long)(m/a) * inv(m % a) % m;
}

The exact time complexity of the this recursion is not known. It's is somewhere between O(log⁡mlog⁡log⁡m)O(\frac{\log m}{\log\log m}) and O(m13−2177+ϵ)O(m^{\frac{1}{3} - \frac{2}{177} + \epsilon}). See On the length of Pierce expansions. In practice this implementation is fast, e.g. for the modulus 109+710^9 + 7 it will always finish in less than 50 iterations.

Applying this formula, we can also precompute the modular inverse for every number in the range [1,m−1][1, m-1] in O(m)O(m).

text
inv[1] = 1;
for(int a = 2; a < m; ++a)
    inv[a] = m - (long long)(m/a) * inv[m%a] % m;

Finding the modular inverse for array of numbers modulo mm

Suppose we are given an array and we want to find modular inverse for all numbers in it (all of them are invertible). Instead of computing the inverse for every number, we can expand the fraction by the prefix product (excluding itself) and suffix product (excluding itself), and end up only computing a single inverse instead.

xi−1=1xi=x1⋅x2⋯xi−1⏞prefixi−1⋅ 1 ⋅xi+1⋅xi+2⋯xn⏞suffixi+1x1⋅x2⋯xi−1⋅xi⋅xi+1⋅xi+2⋯xn=prefixi−1⋅suffixi+1⋅(x1⋅x2⋯xn)−1\begin{align} x_i^{-1} &= \frac{1}{x_i} = \frac{\overbrace{x_1 \cdot x_2 \cdots x_{i-1}}^{\text{prefix}_{i-1}} \cdot ~1~ \cdot \overbrace{x_{i+1} \cdot x_{i+2} \cdots x_n}^{\text{suffix}_{i+1}}}{x_1 \cdot x_2 \cdots x_{i-1} \cdot x_i \cdot x_{i+1} \cdot x_{i+2} \cdots x_n} \\ &= \text{prefix}_{i-1} \cdot \text{suffix}_{i+1} \cdot \left(x_1 \cdot x_2 \cdots x_n\right)^{-1} \end{align}

In the code we can just make a prefix product array (exclude itself, start from the identity element), compute the modular inverse for the product of all numbers and then multiply it by the prefix product and suffix product (exclude itself). The suffix product is computed by iterating from the back to the front.

text
std::vector<int> invs(const std::vector<int> &a, int m) {
    int n = a.size();
    if (n == 0) return {};
    std::vector<int> b(n);
    int v = 1;
    for (int i = 0; i != n; ++i) {
        b[i] = v;
        v = static_cast<long long>(v) * a[i] % m;
    }
    int x, y;
    extended_euclidean(v, m, x, y);
    x = (x % m + m) % m;
    for (int i = n - 1; i >= 0; --i) {
        b[i] = static_cast<long long>(x) * b[i] % m;
        x = static_cast<long long>(x) * a[i] % m;
    }
    return b;
}

Practice Problems