Monitoring~30 min2s · 256 MBtarget O(n × b) time, O(b) spacebit-manipulation
You work on a monitoring platform. A fleet has 20 health checks, numbered 0 to 19, and every check starts green. Restarting a service flips some of the checks: its flip mask has bit c set when check c changes state.
A check that is flipped an odd number of times ends red, and one that is flipped an even number of times ends green. After an incident review, the team has a target pattern of red checks.
Your team lead asks: "How many different sets of services can we restart to get exactly that pattern?" The fleet can have 100,000 services, and the empty set of restarts counts too.
Write count_restart_sets(flips, target).
flips[i] is the flip mask of service i. Bit c of the mask is 1 when restarting the service flips check c.target is the wanted red pattern, in the same format.xor equal to target, modulo 1,000,000,007.Input: flips = [1, 2, 3], target = 3
Output: 2Three services flip the checks 0, 1 and both. Every subset of the three, with the combined mask:
| Restarted masks | Combined mask (xor) |
|---|---|
| {} | 0 |
| {1} | 1 |
| {2} | 2 |
| {1, 2} | 3 |
| {3} | 3 |
| {1, 3} | 2 |
| {2, 3} | 1 |
| {1, 2, 3} | 0 |
Two sets give 3: the service with mask 3 alone, and the services with masks 1 and 2.
Input: flips = [4, 4], target = 0
Output: 2Two equal masks cancel. The empty set and the set of both services leave every check green.
Input: flips = [2, 6], target = 1
Output: 0No mask flips check 0, so no set can turn it red.
1 ≤ len(flips) ≤ 100,0000 ≤ flips[i] ≤ 1,048,575 (20 bits)0 ≤ target ≤ 1,048,5751,000,000,007.Trying every subset takes 2 to the power 100,000 steps, and even 40 services give 1,099,511,627,776 subsets. The answer itself is too large to count one by one. The masks have only 20 bits, so there must be a structure that does not depend on the number of services.