Party Lamps
InterviewTime limit1sMemory limit128 MB
Given N lamps all starting ON and a press count C, list every distinct final lamp configuration reachable with exactly C presses of four fixed toggle buttons and consistent with up to two ON and two OFF constraints.
- Level
Medium6 of 10
- Topics
- Brute force, Bit manipulation, Math, Sorting
- Solved
- No attempts yet
Problem
There are coloured lamps numbered from to . The lamps are wired to four buttons:
- Button 1 — toggles every lamp: lamps that are ON turn OFF, and lamps that are OFF turn ON.
- Button 2 — toggles the state of every odd-numbered lamp.
- Button 3 — toggles the state of every even-numbered lamp.
- Button 4 — toggles the state of every lamp whose number has the form (with ), i.e. lamps
A counter records the total number of button presses.
When the event starts, every lamp is ON and the counter is .
You are given the value of the counter and information about the final state of some of the lamps. Determine every possible final configuration of the lamps that is consistent with the given information, listing each distinct configuration exactly once.
Input
The input consists of four lines describing the number of lamps , the number of button presses , and the known final states of some lamps.
- The first line contains the integer .
- The second line contains the final value of the counter .
- The third line lists the numbers of the lamps known to be ON in the final configuration, separated by single spaces and terminated by the integer .
- The fourth line lists the numbers of the lamps known to be OFF in the final configuration, separated by single spaces and terminated by the integer .
Constraints:
- At most lamps are declared ON.
- At most lamps are declared OFF.
- At least one valid final configuration always exists.
Output
Output every possible final configuration of the lamps that is consistent with the input, with no repetitions.
Each configuration is written on its own line as a string of characters, where the -th character is the state of lamp : 0 means OFF and 1 means ON.
Print the configurations in lexicographically ascending order (so 0000000000 comes before 0101010101).