Build Pascal's triangle by addition, then answer C(n,k) mod p with a factorial / inverse-factorial table. Step through both.
| i | fact[i] = i! mod p | invFact[i] = (i!)⁻¹ mod p |
|---|
| Method | Setup | Per value / query | Modulus |
|---|---|---|---|
| Pascal triangle DP | O(n²) | O(1) lookup | any |
| Multiplicative formula | — | O(min(k,n−k)) | none |
| Factorials + inverse | O(N) | O(1) | prime, n<p |
| Inverse-factorial sweep | O(N + log p) | — | prime |
| Lucas (huge n) | O(p) | O(log_p n) | prime, n≥p |