Dynamic Programming21 sections · 916 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 nummodKnum \mod K as you build. Since (10x+d)modK=(10xmodK10+d)modK(10 \cdot x + d) \mod K = (10x \mod K \cdot 10 + d) \mod K, it works.