This page is still under construction.

Parts of this page are still being built. What you see may change.

Party Lamps

Interview

Time limit1sMemory limit128 MB

Summary
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 NN coloured lamps numbered from 11 to NN. 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 3K+13K+1 (with K≥0K \ge 0), i.e. lamps 1,4,7,…1, 4, 7, \dots

A counter CC records the total number of button presses.

When the event starts, every lamp is ON and the counter CC is 00.

You are given the value of the counter CC and information about the final state of some of the lamps. Determine every possible final configuration of the NN 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 NN, the number of button presses CC, and the known final states of some lamps.

  • The first line contains the integer NN.
  • The second line contains the final value of the counter CC.
  • 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 −1-1.
  • 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 −1-1.

Constraints:

  • 10≤N≤10010 \le N \le 100
  • 1≤C≤100001 \le C \le 10000
  • At most 22 lamps are declared ON.
  • At most 22 lamps are declared OFF.
  • At least one valid final configuration always exists.

Output

Output every possible final configuration of the NN lamps that is consistent with the input, with no repetitions.

Each configuration is written on its own line as a string of NN characters, where the ii-th character is the state of lamp ii: 0 means OFF and 1 means ON.

Print the configurations in lexicographically ascending order (so 0000000000 comes before 0101010101).

Examples3

  1. Example 1

    Input
    10
    1
    -1
    7 -1
    
    Expected output
    0000000000
    0101010101
    0110110110
    
  2. Example 2

    Input
    10
    2
    -1
    -1
    
    Expected output
    0000000000
    0011100011
    0101010101
    1001001001
    1010101010
    1100011100
    1111111111
    
  3. Example 3

    Input
    10
    1
    1 -1
    -1
    
    Expected output
    1010101010