Dynamic Programming21 sections · 915 units
Open in Course

Challenge: Digit DP on Multiple Numbers

Comparing two numbers

Count pairs (a,b)(a, b) where a≤Xa \leq X, b≤Yb \leq Y, and a+ba + b has some property. This is 2D digit DP. Build aa and bb simultaneously, digit by digit.

State: (pos,tightA,tightB,carry,...)(pos, tightA, tightB, carry, ...). The carry matters: when checking if a+ba + b has no digit > 5, you need to know if there's a carry from previous positions. Complexity explodes: O(n⋅2⋅2⋅2⋅additionalStates)O(n \cdot 2 \cdot 2 \cdot 2 \cdot additionalStates). Use only when necessary.