Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Math & Number Theory/Math / Number Theory

Technique

Combinatorics mod p (nCr, Catalan, stars and bars)

Count arrangements with factorials and inverse factorials mod p, stars and bars, Catalan numbers, and bijections.

Learn firstMath / Number TheoryBinary Exponentiation (fast power)UnlocksContribution Technique (sum over all subarrays/pairs)Probability & Expected-Value DP (linearity of expectation)Connected-Component DP (insertion DP)
0%
mastery
0/10 solved
Practise nextCount Numbers with Unique Digits
IdeaPracticePitfalls

Recognise it

The signal

number of ways ... modulo 1e9+7

  1. 1Brute force

    Enumerate all arrangements or DP over counts.

  2. 2The observation

    Enumeration: a closed form counts them directly.

  3. 3Why it works

    C(n, k) = n! / (k! (n-k)!) mod p with Fermat inverses when p is prime.

Reach for it when

  • +grid paths with only right/down moves
  • +distribute identical items into boxes
  • +balanced bracket sequences

Not when

  • ×constraints break symmetry (needs DP or inclusion-exclusion)

Practice

Next

Count Numbers with Unique Digits

0/10 solved
  • Count Numbers with Unique DigitsMedium
  • Binomial CoefficientsCSESMedium
  • Creating Strings IICSESMedium
  • Distributing ApplesCSESMedium
  • Bracket Sequences ICSESMedium
  • Grid Paths IICSESMedium
  • Unique PathsJane StreetMedium
  • Count Ways to Build Rooms in an Ant ColonyHard
  • Number of Ways to Reorder Array to Get Same BSTHard
  • Bracket Sequences IICSESHard

Pitfalls

Misconceptions

  • !dividing under a modulus without inverses

Looks like it, isn't

  • Count Special Integers needs Digit DP
    Why
    Counting distinct-digit numbers <= n needs a tight-prefix walk plus a used-digit mask (digit DP with bitmask). source ↗
Go deepervariants and techniques · 3 combinations · resources

Variants and techniques

  • Inclusion-Exclusion PrincipleCount a union by alternately adding and subtracting intersections, often iterating over subsets of a small set of conditions.
  • Lagrange InterpolationCP 2400+If the answer f(n) is a polynomial of degree d in n (sum of i^k has degree k+1), compute f(0..d) directly. Then evaluate f at a huge n with Lagrange's formula in O(d), using prefix and suffix products.
  • Burnside's Lemma / Polya CountingCP 2000The number of objects distinct up to a symmetry group is the average, over the group, of the number of objects each symmetry fixes. For necklaces under rotation: (1/n) * sum over k of m^gcd(k, n).

Combinations

  • Mobius Inversion & Multiplicative Functions+Sieve of Eratosthenes / Smallest Prime Factor+Combinatorics mod p (nCr, Catalan, stars and bars)

    count pairs/subsequences with gcd 1 (or gcd exactly g), values <= 1e6

    Mobius Inversion & Multiplicative Functions:
    invert 'divisible by d' counts into 'gcd exactly 1'
    Sieve of Eratosthenes / Smallest Prime Factor:
    a linear sieve for mu (and the multiples counts)
    Combinatorics mod p (nCr, Catalan, stars and bars):
    count pairs C(c_d, 2) or subsets 2^c_d - 1 from c_d multiples

    Counting Coprime PairsGCD SubsetsCoprime Subsequences

  • DP on Trees+Combinatorics mod p (nCr, Catalan, stars and bars)

    count the orders in which tree nodes can be added parent-first (or BST insert orders)

    DP on Trees:
    subtree sizes and the ways for each child
    Combinatorics mod p (nCr, Catalan, stars and bars):
    interleave the children's orders with multinomials, i.e. n! / product of subtree sizes

    Count Ways to Build Rooms in an Ant ColonyNumber of Ways to Reorder Array to Get Same BST

  • Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem+Lagrange Interpolation

    count spanning trees sharing exactly k edges with a given tree, n <= 100

    Gaussian Elimination (mod p / reals) & Matrix-Tree Theorem:
    matrix-tree determinants for spanning trees with weighted edges
    Lagrange Interpolation:
    recover the polynomial in x from n evaluations

    Stranger Trees

Resources

  • Binary exponentiationcp-algorithms · article
  • Sieve of Eratosthenescp-algorithms · article
  • Modular inversecp-algorithms · article
  • DivisibilityUSACO Guide · course
  • CombinatoricsUSACO Guide · course
  • Math cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems