Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 9

Modular Division Using Modular Inverse

Recommended resources

Welcome back! In our last lesson, you mastered a key technique in competitive programming: finding the modular multiplicative inverse for a prime modulus using Fermat's Little Theorem. You learned that for a prime , the inverse of is simply , and you know how to compute this efficiently with binary exponentiation.

Today, we will put that knowledge to work. This lesson connects the concept of an inverse to its primary purpose: performing modular division. You will learn how to compute expressions like , a task that frequently appears in competitive programming problems but cannot be done with simple division. By transforming division into multiplication, you will complete your toolkit for all four basic arithmetic operations in the world of modular arithmetic.

Division Through Multiplication

The core idea is simple and elegant. In standard arithmetic, dividing by a number is the same as multiplying by its reciprocal, . We apply the exact same logic in modular arithmetic. The expression is not computed directly. Instead, we calculate it as:

where is the modular multiplicative inverse of modulo .

Since you already know how to find when is prime, the process becomes straightforward. The following reading from a Codeforces blog post, written specifically for competitive programmers, formalizes this exact step.

Modular Arithmetic for Beginners

This blog post on Codeforces is an excellent resource for understanding modular arithmetic from a practical, problem-solving perspective. This particular section defines modular division.

Please read the two short paragraphs under the second pow_mod function. Focus on the final formula presented for the division operator. This is the central concept for this entire lesson.

As the text confirms, modular division is simply multiplication by the modular inverse. This elegantly sidesteps the ambiguity of what "division" means in a system of integer remainders.


The Complete Algorithm

Let's combine everything you've learned into a single, step-by-step procedure for calculating , where is a prime number.

  1. Find the Inverse of the Divisor: The first step is to find the modular multiplicative inverse of the denominator, . Using Fermat's Little Theorem, we know this is .
  2. Calculate the Inverse: Use the binary exponentiation algorithm (which you've named power in your C++ template) to compute power(b, p - 2, p).
  3. Perform Multiplication: Multiply the numerator, , by the inverse you just found, and take the result modulo . The final expression is (a * power(b, p - 2, p)) % p.

Implementation in C++

Here is how you can implement a modDivide function in C++. It relies on the power function from our lesson on binary exponentiation and the modInverse function from our last lesson.

// A prime modulus, e.g., 10^9 + 7
const int MOD = 1e9 + 7;

// Computes (base^exp) % mod in O(log exp)
long long power(long long base, long long exp) {
    long long res = 1;
    base %= MOD;
    while (exp > 0) {
        if (exp % 2 == 1) res = (res * base) % MOD;
        base = (base * base) % MOD;
        exp /= 2;
    }
    return res;
}

// Computes the modular inverse of n using Fermat's Little Theorem
long long modInverse(long long n) {
    return power(n, MOD - 2);
}

// Computes (a / b) % MOD
long long modDivide(long long a, long long b) {
    a %= MOD;
    long long inv_b = modInverse(b);
    return (a * inv_b) % MOD;
}

// Example usage:
// long long result = modDivide(100, 5); // Computes (100 / 5) % MOD

Note: This code assumes a fixed prime modulus MOD. In a contest, you might pass the modulus p as a parameter to these functions if it changes between problems. Also, remember to handle the intermediate multiplication with long long to prevent overflow, as in (res * base).


A Worked Example

Let's trace the algorithm with a concrete example. Suppose we need to calculate .

  • Here, , , and .
  1. Find the inverse of : We need to find .

  2. Calculate using Fermat's Little Theorem: The inverse is .

    • We use binary exponentiation to find .
    • in binary is 1011.
    • , so .
    • Thus, . (Let's check: . It's correct!)
  3. Perform the final multiplication:

    First, reduce : , so .


    To find , we can divide: .

    The final answer is 6. This means 6 is the number that, when multiplied by 4, gives 50 in modulo 13 arithmetic: , and , which is the same as . Our calculation is correct.


Application: A Glimpse into Combinatorics

Why is this so important in competitive programming? Many problems, especially in combinatorics, involve formulas with division. A classic example is the binomial coefficient "n choose k":

When a problem asks you to compute this value modulo a prime , you cannot simply compute the huge factorials and then divide. Instead, you compute the factorials modulo and then use modular division. The formula becomes:

This is a powerful application that we will explore in depth in a future module. The Codeforces article you read from earlier briefly touches upon this.

Modular Arithmetic for Beginners

This last reading gives a quick preview of why modular division is so essential.

In the same blog post, look at the paragraph that starts with You can also now take advantage. This shows the direct application of modular division to a common combinatorial formula, highlighting its practical importance.


Conclusion

You have now completed the quartet of modular arithmetic operations. By understanding that division is just multiplication by an inverse, you can solve a much broader class of problems.

Let's summarize today's key points:

  • Modular division is defined as multiplication by the modular inverse: .
  • For a prime modulus , the inverse is calculated as , using the binary exponentiation algorithm for efficiency.
  • The entire operation can be wrapped in a single function, modDivide(a, b), which calls your modInverse function.
  • This technique is fundamental for solving many combinatorics problems in a modular context.

In our next lesson, we will solve a slightly different but related problem: linear congruences. We will learn how to find solutions for in equations of the form . You will see that finding is actually a special case of solving this equation.