Dynamic Programming21 sections · 915 units
Open in Course

Pattern - When to Use Digit DP

Recognizing the pattern

Use digit DP when: counting integers in a range with digit-based constraints, and the range is too large to enumerate. Signals: "count numbers in [L, R]", "digit sum", "no repeated digits", "digits in increasing order", "divisible by K".

Won't work when: the constraint involves the number's value (not digits), like "count primes". Primality doesn't decompose by digits. Divisibility is a special case: track nummod  Knum \mod K as you build. Since (10⋅x+d)mod  K=(10xmod  K⋅10+d)mod  K(10 \cdot x + d) \mod K = (10x \mod K \cdot 10 + d) \mod K, it works.