Mathematical Algorithms/Combinatorics & Counting

Lesson 10.41,262 words

Combinatorics & Counting

Counting is the arithmetic of finite sets. We build up from permutations $n!

╌╌╌╌

Modular exponentiation and, through Fermat's little theorem, the modular inverse are the hinge on which all of practical combinatorics swings: almost every counting answer is a ratio of factorials, and a ratio modulo a prime is a product with an inverse. This lesson assembles the counting toolkit (permutations, combinations, Pascal's rule, stars and bars) and then shows how to evaluate those quantities modulo a prime in constant time after a linear precompute. We finish with two structural principles that recur everywhere: inclusion–exclusion for counting unions, and the Chinese Remainder Theorem for stitching together congruences.

Permutations and combinations

A permutation is an ordering of distinct objects. There are choices for the first position, for the second, and so on, giving

If we order only of the objects, we stop the product after factors:

A combination counts subsets of size , where orderings no longer matter. Each -subset can be ordered in ways, so dividing by removes the overcount:

The quantity , read choose , is the binomial coefficient.1 It is symmetric, (choosing which to include is the same as choosing which to exclude), and the two boundary values are .

Pascal's rule

Binomial coefficients satisfy a recurrence that lets us build them additively, with no division at all:

Combinatorial proof. Fix a distinguished element . Every -subset of either contains or does not. Those that contain are formed by choosing the remaining elements from the other , giving of them. Those that omit choose all elements from the other , giving . The two cases are disjoint and exhaustive, so their counts add.

Arranging these values in rows is Pascal's triangle: each interior entry is the sum of the two directly above it.

Pascal's triangle — each cell

The highlighted cell is . Because each entry needs only the row above, the whole triangle up to row is an dynamic program, the right approach when is small or when no modulus is involved (and the basis for Pascal's Triangle II and grid-path problems like Unique Paths, whose answer is exactly ).

That grid-path count is Pascal's rule in disguise: label each lattice node with the number of monotone (right/down) paths reaching it, and each node is the sum of its left and top neighbours, exactly the additive recurrence. On a grid of nodes the corner reads .

Lattice paths on a grid: each node sums its left and top neighbour; corner is

The binomial theorem

The coefficients earn their name from the expansion

since each term of the product picks from of the factors and from the rest, and there are ways to make that choice. Setting gives : the number of subsets of an -set, counted by size.

Combinations with repetition: stars and bars

How many ways can we write a non-negative integer as an ordered sum of non-negative parts, with each ? Equivalently, how many multisets of size can we draw from distinct types?

Bijection. Lay out identical stars in a row and insert bars among them; the bars cut the stars into ordered groups, the -th group's size being . For example with , :

Every arrangement of stars and bars in a line of symbols yields exactly one solution and vice versa, so the count is the number of ways to choose which of the positions hold bars, namely .

Concretely, with and there are slots; choosing the of them that hold bars fixes the three part sizes at once, so the count is .

Stars and bars: stars and bars in slots encode

Computing

Competitive and large-scale problems ask for counts modulo a prime (typically ) because the true values are astronomically large. The factorial formula has a division by , and division is not defined modulo ; what stands in for it is multiplication by a modular inverse. Since is prime, Fermat gives for any , computed by the modular exponentiation routine.

The plan: precompute the factorials for all , and the inverse factorials . With both tables in hand, every binomial coefficient is a single product.

Algorithm:Precompute-Factorials(N,p)\textsc{Precompute-Factorials}(N, p)O(N)O(N) tables for O(1)O(1) queries
  1. 1
    fact[0]1\text{fact}[0] \gets 1
  2. 2
    for i1i \gets 1 to NN do
  3. 3
    fact[i]fact[i1]imodp\text{fact}[i] \gets \text{fact}[i-1] \cdot i \bmod p
  4. 4
    invfact[N]Mod-Pow(fact[N],p2,p)\text{invfact}[N] \gets \textsc{Mod-Pow}(\text{fact}[N],\, p-2,\, p)
    one Fermat inverse
  5. 5
    for iNi \gets N downto 11 do
  6. 6
    invfact[i1]invfact[i]imodp\text{invfact}[i-1] \gets \text{invfact}[i] \cdot i \bmod p
    peel a factor

The downward loop is the trick that keeps the precompute at rather than : only one modular exponentiation is needed, for ; each smaller inverse factorial follows from . Then each query is constant time:

This -precompute, -query scheme is exactly what Number of Music Playlists and Count Anagrams need, since both reduce to products and ratios of factorials modulo .

Inclusion–exclusion

To count a union of overlapping sets we cannot simply add their sizes, since elements in several sets get counted several times. Inclusion–exclusion corrects the overcount with alternating signs:

Inclusion–exclusion — add singles, subtract pairs, add the triple (in acc)

Worked example (counting coprime-to-a-set integers). How many integers in are divisible by none of ? Let be the multiples of respectively. Then ; ; and . So

leaving integers divisible by none of , which are exactly . The same alternating sum, applied with = maps position to itself, counts derangements .

The Chinese Remainder Theorem

Inclusion–exclusion combines counts; the Chinese Remainder Theorem (CRT) combines congruences. Given a system

with the moduli pairwise coprime, CRT guarantees a unique solution modulo .4 The construction is explicit and again uses the modular inverse. Let . Because the are coprime to , so is , hence has an inverse modulo ; call it (or when is prime). Then

Each term is (since there) and modulo every other (since ), so the sum satisfies all congruences simultaneously. Concretely, to solve and each term acts as a selector: one lands on its own residue and vanishes modulo the other, so adding them assembles the answer one congruence at a time.

CRT as a sum of selectors: each hits its own residue and is modulo the other, so the terms add to .
Algorithm:CRT(a[],m[])\textsc{CRT}(a[\,], m[\,]) — combine congruences with pairwise-coprime moduli
  1. 1
    MimiM \gets \prod_i m_i
  2. 2
    x0x \gets 0
  3. 3
    for i1i \gets 1 to nn do
  4. 4
    MiM/miM_i \gets M / m_i
  5. 5
    yiMod-Inverse(Mimodmi,  mi)y_i \gets \textsc{Mod-Inverse}(M_i \bmod m_i,\; m_i)
  6. 6
    x(x+aiMiyi)modMx \gets (x + a_i \cdot M_i \cdot y_i) \bmod M
  7. 7
    return xx

CRT is what lets us compute modulo a large composite by working independently in each prime-power factor and reassembling, the structural twin of how stars-and-bars or inclusion–exclusion decompose a hard count into manageable pieces.

Takeaways

  • Permutations count orderings (, or ); combinations count subsets, , dividing out the orderings.
  • Pascal's rule (element in or out) builds the triangle additively in with no division.
  • Stars and bars: the ordered non-negative solutions of number , via the bars-between-stars bijection.
  • To compute , precompute factorials and inverse factorials (one Fermat inverse, then peel factors) in , giving per query; use Lucas' theorem when .
  • Inclusion–exclusion counts unions by alternating add/subtract over all intersections; the alternating signs make each element net-counted exactly once.
  • The Chinese Remainder Theorem uniquely solves a system of congruences with coprime moduli via , the inverse again coming from Fermat or the extended gcd.

Footnotes

  1. CLRS, Appendix C — Counting and Probability (§C.1): permutations, combinations, and the binomial coefficient .
  2. Skiena, § — Combinatorics: Lucas' theorem reduces to a product of base- digit binomials when exceed .
  3. CLRS, Appendix C — Counting and Probability (§C.1): the inclusion–exclusion principle and the alternating-sign correction for unions.
  4. CLRS, Ch. 31 — Number-Theoretic Algorithms (§31.5): the Chinese Remainder Theorem and the constructive formula.
Practice

╌╌ END ╌╌