0Pricing
Competitive Programming Academy · Lesson

nCr with Precomputed Factorials

Count combinations modulo a prime.

Counting Combinations

Many problems ask how many ways to choose r items from n, written nCr. Contests want that count under a prime modulus. 🧮

The Factorial Formula

The classic formula is nCr equals n factorial divided by r factorial times n minus r factorial. The catch is that division under a mod.

# nCr = n! / (r! * (n-r)!)

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