C - スイッチパネル / Switch Panel Editorial by admin
Claude 4.6 Opus (Thinking)Overview
This problem asks you to find the minimum number of switches that need to be pressed, selecting some subset of \(M\) switches, to light up exactly \(K\) light bulbs.
Analysis
- Each switch has two choices: “press” or “don’t press,” giving a total of \(2^M\) possible combinations.
- Light bulb states are binary (“on/off”), so toggling the same bulb an even number of times returns it to its original state. This can be represented using XOR (exclusive or).
- Given the constraints \(N \leq 23\), \(M \leq 23\), we have \(2^M \leq 2^{23} = 8{,}388{,}608\). Enumerating all combinations is fast enough.
Representing Bulb States with Bitmasks
We represent the on/off state of each bulb as a bit in a single integer. For example, if \(N = 4\) and bulbs 1 and 3 are lit, the state is 0101 (binary) = \(5\).
By also representing the set of bulbs affected by each switch as a bitmask, the operation of pressing a switch can be computed with a single XOR.
Algorithm
Preprocessing: For each switch \(j\), store the set of affected bulbs as a bitmask
masks[j].- Example: If a switch affects bulbs 1 and 3, then
masks[j] = 0b101 = 5
- Example: If a switch affects bulbs 1 and 3, then
Exhaustive Search: Enumerate all \(2^M\) possible ways to press switches.
- For each combination (subset) of switches, compute the final state of the bulbs.
- The initial state is \(0\) (all bulbs off). XOR the masks of the pressed switches one by one.
- Check whether the number of set bits in the final state (= number of lit bulbs) equals \(K\).
Update the Minimum: If the number of lit bulbs equals \(K\), check whether the number of pressed switches is the current minimum, and update the answer accordingly.
Output: If no valid combination is found, output
-1.
Concrete Example
For \(N=3\), \(M=2\), \(K=2\), where switch 1 affects bulbs {1, 2} and switch 2 affects bulbs {2, 3}:
| Pressed Switches | Bulb State (binary) | Lit Count | Switch Count |
|---|---|---|---|
| None | 000 |
0 | 0 |
| {1} | 011 |
2 | 1 |
| {2} | 110 |
2 | 1 |
| {1, 2} | 011 XOR 110 = 101 |
2 | 2 |
The minimum number of switches to achieve \(K=2\) is 1.
Complexity
- Time complexity: \(O(2^M \cdot M)\)
- We enumerate \(2^M\) subsets, and for each subset perform at most \(M\) XOR operations.
- Space complexity: \(O(M)\)
- Only an array to store the bitmask for each switch.
Implementation Notes
Bulb numbers are given in 1-indexed form, so when converting to bits, use
1 << (s - 1).bin(state).count('1')easily counts the number of lit bulbs (number of set bits). Python’sint.bit_count()(available from Python 3.10) can also be used.When enumerating subsets, use bit shifts to determine whether each switch is included, and if so, XOR its mask into the current state.
Source Code
import sys
def main():
input_data = sys.stdin.read().split()
idx = 0
N = int(input_data[idx]); idx += 1
M = int(input_data[idx]); idx += 1
K = int(input_data[idx]); idx += 1
masks = []
for j in range(M):
c = int(input_data[idx]); idx += 1
mask = 0
for _ in range(c):
s = int(input_data[idx]); idx += 1
mask |= (1 << (s - 1))
masks.append(mask)
ans = -1
# Enumerate all 2^M subsets of switches
for subset in range(1 << M):
state = 0
count = 0
tmp = subset
bit = 0
while tmp:
if tmp & 1:
state ^= masks[bit]
count += 1
tmp >>= 1
bit += 1
if bin(state).count('1') == K:
if ans == -1 or count < ans:
ans = count
print(ans)
main()
This editorial was generated by claude4.6opus-thinking.
posted:
last update: