Two tables, then reads
transcript
A hundred thousand prize draws arrive. Each pool has up to a million entries. Count the possible sets of winners. Answer modulo a prime, and answer fast.
There are a hundred thousand draws. Each could multiply a million terms. That is a hundred billion products in all. Far too slow.
Build the factorials once. Build their inverses once. Then each draw is a few reads.
Take one inverse, for the top factorial. Multiply it by the top index. That gives the next inverse down. Repeat, walking down to index zero. The whole table is done in one pass.
Read the factorial of n. Multiply by the inverse factorial of k. Multiply by the inverse factorial of n minus k.
Each table has a million entries. Each fills in a single pass. Then every draw is a few reads.
Two tables, built once. Each draw is three reads. A hundred thousand draws stay cheap. Reduce after every product. Then the answers are exact. Full breakdown in the lecture.