Bit Manipulation · Pattern 5 of 6
Sum over subsets (SOS) DP
For every mask, add up the values stored at all of its submasks. Handling one bit at a time does this in O(B · 2^B) instead of O(3^B), and it answers "how many items fit inside this mask?" for every mask at once.
Watch the lesson
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
When to use it
- Values are small (under 2^16 or 2^20), so you can index an array by them
- Count pairs or triples whose AND (or OR) is zero
- For each mask, a sum or count over all its submasks or supermasks
The template
// f[mask] becomes the sum of the original f[sub] over every submask sub of mask
int[] subsetSums(int[] f, int bits) {
for (int b = 0; b < bits; b++)
for (int mask = 0; mask < (1 << bits); mask++)
if ((mask & (1 << b)) != 0) f[mask] += f[mask ^ (1 << b)]; // add the side without bit b
return f;
}
int countTriplets(int[] nums) { // ordered (i, j, k) with nums[i] & nums[j] & nums[k] == 0
int bits = 16, full = (1 << bits) - 1;
int[] f = new int[1 << bits];
for (int a : nums) for (int b : nums) f[a & b]++; // pairs grouped by their AND
subsetSums(f, bits);
int res = 0;
for (int c : nums) res += f[full & ~c]; // the pair AND must avoid every bit of c
return res;
} def subset_sums(f, bits): # f[mask] -> sum of f[sub] over submasks of mask
for b in range(bits):
step = 1 << b
for mask in range(1 << bits):
if mask & step:
f[mask] += f[mask ^ step] # add the side without bit b
return f
def count_triplets(nums): # ordered (i, j, k) with AND of the three == 0
bits = 16
full = (1 << bits) - 1
f = [0] * (1 << bits)
for a in nums:
for b in nums:
f[a & b] += 1 # pairs grouped by their AND
subset_sums(f, bits)
return sum(f[full & ~c] for c in nums) # the pair AND must avoid every bit of c // f[mask] becomes the sum of the original f[sub] over every submask sub of mask
vector<int> subsetSums(vector<int> f, int bits) {
for (int b = 0; b < bits; b++)
for (int mask = 0; mask < (1 << bits); mask++)
if (mask & (1 << b)) f[mask] += f[mask ^ (1 << b)]; // add the side without bit b
return f;
}
int countTriplets(vector<int>& nums) { // ordered (i, j, k) with AND of the three == 0
int bits = 16, full = (1 << bits) - 1;
vector<int> f(1 << bits);
for (int a : nums) for (int b : nums) f[a & b]++; // pairs grouped by their AND
f = subsetSums(f, bits);
int res = 0;
for (int c : nums) res += f[full & ~c]; // the pair AND must avoid every bit of c
return res;
} // f[mask] becomes the sum of the original f[sub] over every submask sub of mask
function subsetSums(f, bits) {
for (let b = 0; b < bits; b++)
for (let mask = 0; mask < 1 << bits; mask++)
if (mask & (1 << b)) f[mask] += f[mask ^ (1 << b)]; // add the side without bit b
return f;
}
function countTriplets(nums) { // ordered (i, j, k) with AND of the three == 0
const bits = 16, full = (1 << bits) - 1;
const f = new Array(1 << bits).fill(0);
for (const a of nums) for (const b of nums) f[a & b]++; // pairs grouped by their AND
subsetSums(f, bits);
let res = 0;
for (const c of nums) res += f[full & ~c]; // the pair AND must avoid every bit of c
return res;
} The variations
-
1Count items that fit inside a mask
x & c == 0means x is a submask of the complement of c. Count every pair by its AND, run SOS once so each mask knows how many pair ANDs fit inside it, then each third number is one lookup atfull & ~c.
Mistakes to avoid
- Putting the mask loop outside the bit loop: each bit must be fully processed before the next, or submasks get counted more than once.
- Forgetting
full & ~c:~calone is negative and is not a valid index. - Picking
bitstoo small for the values, so some values index past the array.
Mark it done once you can write the template from memory and you have solved the worked examples yourself.