##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
You've handled linear and tree states. Now track subsets with bitmasks. Solve TSP and learn how to iterate over visited elements in exponential state spaces.
Subsets as binary numbers
TSP, assignments, partitions
n ≤ 20 and tracking used elements
Visit all cities, return home
n! to 2^n × n
dp[mask][i] = end at city i
Which city did we come from?
Start at city 0, mask = 1
O(2^n × n²) triple loop
2^20 × 20 fits in memory
Asymmetric distances? Self-loops?
Order within visited set doesn't matter
No return, any start city
Start leaning basics.
Start leaning basics.
AtCoder DP O - count matchings
Process men in order 0 to n-1
popcount(mask) = men matched so far
+= instead of min()
2^n states, O(n) per state
Count paths, not shortest path
Precompute suffix-prefix overlaps
TSP on overlap graph
computeOverlap for each pair
Reconstruct by backtracking
String concatenation = graph shortest path
Profile DP on grids
One bit at a time, O(n × 2^n)
1D array suffices
AND pairs, OR maximization
O(3^n) submask enumeration
Fit person or start new ride
(rides, capacity) pair as state
Tile m×n with dominoes
Split set into two parts
Is this bitmask DP?
dp[mask][last] counting
LC 1799 - pair for GCD × round
Pick pairs, multiply by round number
LC 526 - position divisibility
Fill positions left to right
Sum over all subsets of mask
n = 20 overflow, bit indexing
Bit shift vs popcount errors
Bit shift vs popcount errors
LC 1349
Solution approach
LC 1434
Solution approach
From TSP to SOS