Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 09 · DSA · Four patterns the ladders lean on · about 23 min to read

Play it as a journeyTutor comes with a plan

How many ways: counting, nCr and inclusion-exclusion

The DP Table
The DP Table · Open board

Shows a table where each cell is built from cells already filled in. Counting problems fill the same kind of table, and this lesson is how to skip the table when a formula can jump straight to the cell.

Before you start. The clock that wraps around gave you the modulus, a fixed clock size where only the remainder is kept, and the modular inverse, a multiply that undoes a multiply when the clock size is prime. Clinic #31: Pow(x, n) gave you fast power, which finds an inverse as a^(p − 2) mod p. Clinic #21: Unique Paths showed that counting routes across a grid fills Pascal’s triangle. The running total (prefix sums and difference arrays) cut a rectangle out of a grid by adding and subtracting, which is a small case of this lesson’s last big idea.

Lesson 78 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • 4 September 1751: Euler asks how many ways to cut a polygon
  • Factorial: lining people up
  • Combination: choosing without caring about order
  • The trouble: the numbers do not fit
  • The factorial table: one inverse, a million answers
  • Inclusion-exclusion: counting a crowd without counting anyone twice
  • Catalan number: the queue that never runs short
  • The factorial and inverse-factorial tables
  • Inclusion-exclusion as a loop over subsets
  • The Catalan numbers, and the reflection
  • The totient, and the Chinese remainder theorem
  • Two free LeetCode problems
  • Which formula for which question

Terms it explains: modulus, modular inverse, factorial, combination, inclusion-exclusion, Catalan numbers, Common mistakes, derangements, Euler totient, 9+7) =", phi(10, Euler's theorem, Chinese remainder theorem, LC 2514 Count Anagrams.

Lab

The same recursion runs three ways. Which one solves ways(3) fifty-five times, and which solves it once?

Stay on Ask again every time, press Show next step twenty times, and watch calls made in The bill so far.

Open The DP table

Review DSA