0PricingLogin
Competitive Programming Academy · Lesson

Modular Inverse via Fermat

Divide under a modulus safely.

Division Breaks Under Mod

Add, subtract, and multiply behave nicely under a modulus, but plain division does not. You cannot just divide and take the remainder. ⚠️

Replace Divide with Multiply

The fix is the modular inverse: dividing by x becomes multiplying by the inverse of x. So a / b mod m turns into a times b's inverse.

All lessons in this course

  1. Work Modulo a Prime
  2. Fast Modular Exponentiation
  3. Modular Inverse via Fermat
  4. nCr with Precomputed Factorials
← Back to Competitive Programming Academy