Dynamic Programming21 sections · 916 units
Open in Course

Range Queries

From [L,R] to prefix

Digit DP naturally counts from 00 to NN. But problems often ask for a range [L,R][L, R]. Use inclusion-exclusion: count(L,R)=count(0,R)count(0,L1)count(L, R) = count(0, R) - count(0, L-1) Compute the count up to RR, subtract the count up to L1L-1.

You run digit DP twice with different limits. Watch for edge cases: if L=0L = 0, you might need to handle it specially since L1=1L-1 = -1 is invalid. Some problems exclude 00 from the count.