Visit submasks and larger bitsets
Choose between a machine-word mask, a fixed bitset, and an exponential search.
Understand the idea
To enumerate submasks of mask, start at mask and repeatedly use (sub − 1) & mask. Process zero once and stop before updating it, or unsigned wraparound restarts the cycle. A fixed std::bitset<N> stores more than one machine word and supports whole-set operations.
Trace a small example
The submasks of 1010 are 1010, 1000, 0010, and 0000. For bounded nonnegative subset sums, start with reachable[0] = 1 and update reachable |= reachable << value once per item. The highest represented sum must cover every query you intend to answer.
Conditions and cost
There are 2^popcount(mask) submasks, and all-mask/all-submask enumeration totals 3^n states. Bitset operations cost work across their storage words. Optional extensions in the PDF cover per-bit pair contributions, copied-mask backtracking, and subset-total recurrences.
What happens if the submask loop updates zero before stopping?
Turn the idea into code
List the reachable subset sums of [2, 3] using a bitset trace.
Give me a hint
Begin with sum 0 reachable, then shift by each value.
Show the explained solution
Start {0}; after 2 obtain {0, 2}; after 3 obtain {0, 2, 3, 5}. Each update combines sums without and with the new item.
Your notes
Mark complete when you can explain it yourself.