Dynamic Programming21 sections · 916 units
Open in Course

Challenge: Digit DP on Multiple Numbers

Comparing two numbers

Count pairs (a,b)(a, b) where aXa \leq X, bYb \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(n222additionalStates)O(n \cdot 2 \cdot 2 \cdot 2 \cdot additionalStates). Use only when necessary.