Sum over Subsets DP
Authors: Siyong Huang, Aakash Gokhale
Taking bitmask DP to the next level.
Prerequisites
Resources | ||||
---|---|---|---|---|
CF | Good explanation + problem list | |||
GFG | Goes over brute force solutions | |||
CF | Characterizing SOS DP as multidimensional prefix sums |
Sum over subsets (SOS) DP is a trick that allows you to efficiently compute the sum of all the subsets of an array.
The naive solution would be to iterate through every pair of masks and check if one of them is a subset of the other.
C++
// # of elements in the list for which you want to find the sum over all subsetsint n = 20;// the list for which you want to find the sum over all subsetsvector<int> a(1 << n);// answer for sum over subsets of each subsetvector<int> sos(1 << n);for (int i = 0; i < (1 << n); i++) {// iterate over all other sets and checks whether they're a subset of ifor (int j = 0; j < (1 << n); j++) {if ((i & j) == j) { sos[i] += a[j]; }}}
We can speed this up if we iterate over only subsets of the current mask and add up all of the those values to get the sum over subsets for a particular mask.
The difference comes from the fact that in the first example we iterate over every pair of subsets which takes time and the second we iterate directly over the subsets for each mask. This means each mask is only visited by other masks where k is the number of elements of the mask. This means that the total time complexity is O).
C++
int n = 20;vector<int> a(1 << n);vector<int> sos(1 << n);for (int i = 0; i < (1 << n); i++) {// iterate over all subsets of i directlyfor (int j = (i - 1) & i; j >= 0; j = (j - 1) & i) { sos[i] += a[j]; }}
Notice how in both of these examples we don't seem to be saving much information between different subsets which is the essence of DP. Define to be the sum of subsets of mask such that the first bits of the subset are identical to the first bits of mask. For example, includes the subsets , , , which all have the same common prefix of .
Let's try to figure out the transitions between different states.
If the th bit of mask is a then we can only transition to this state if the th bit of the subset is also . If the th bit of the subset mask was a then it would no longer be a subset.
If the th bit of mask is a then we can transition to this state from both subsets where the th bit is turned off and turned on because both of these cases would be subsets.
More formally,
C++
int n = 20;vector<int> a(1 << n);// keeps track of the sum over subsets// with a certain amount of matching bits in the prefixvector<vector<int>> dp(1 << n, vector<int>(n));vector<int> sos(1 << n);for (int mask = 0; mask < (1 << n); mask++) {dp[mask][-1] = a[mask];
Additional Problems
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
CSES | Easy | ||||
Platinum | Easy | Show TagsCombo, DP | |||
CF | Normal | Show TagsBitmasks, DP | |||
Platinum | Normal | Show TagsNT, Prefix Sums | |||
kilonova | Hard | Show TagsBitmasks, DP, NT | |||
JOI | Hard | Show TagsSOS DP | |||
CF | Insane | Show TagsBitmasks, DP, SOS DP |
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!