Shift Register

Time limit1sMemory limit128 MB

Summary
Given the first 2N output bits of a linear feedback shift register, recover the N switch values, choosing the lexicographically smallest valid setting or reporting -1.
Level

Medium7 of 10

Topics
Math, Bit manipulation, Brute force, Implementation
Solved
No attempts yet

Problem

A register stores NN bits used for computation. A shift register is a special kind of register in which every stored bit can be shifted one position.

A shift register can produce a binary pseudo-random sequence as follows. A shift register of size NN holds the bits a1,a2,…,aNa_1, a_2, \ldots, a_N. On every clock tick the register outputs its rightmost bit aNa_N, and every other bit moves one position to the right. The now-empty first position a1′a'_1 is filled as described below.

Each bit of the register is connected through a switch to a single XOR gate, as shown in the figure. Each bit ii has a switch si∈{0,1}s_i \in \{0, 1\} that decides whether its bit aia_i is fed into the XOR gate (si=1s_i = 1) or not (si=0s_i = 0). Let ki=si⋅aik_i = s_i \cdot a_i; then a1′a'_1 equals the XOR-gate output XOR(k1,…,kN)\mathrm{XOR}(k_1, \ldots, k_N). That is, it is 1 if the number of ones among k1,…,kNk_1, \ldots, k_N is odd, and 0 otherwise.

  • a1′=XOR(k1,…,kN)a'_1 = \mathrm{XOR}(k_1, \ldots, k_N)
  • ai′=ai−1a'_i = a_{i-1} for 2≤i≤N2 \le i \le N
  • output =aN= a_N

For example, in the state shown above the bit newly filled in on the first tick is XOR(1⋅1,0⋅0,1⋅1,1⋅1,0⋅0,1⋅0,1⋅1)=0\mathrm{XOR}(1 \cdot 1, 0 \cdot 0, 1 \cdot 1, 1 \cdot 1, 0 \cdot 0, 1 \cdot 0, 1 \cdot 1) = 0.

The first 2N2N values of the sequence produced by the shift register are given. Write a program that recovers the switch values s1,…,sNs_1, \ldots, s_N.

Input

The first line contains the size NN of the shift register (1≤N≤7501 \le N \le 750).

The second line contains the first 2N2N values output by the shift register, separated by spaces; each value is 0 or 1.

Output

If a switch setting that reproduces the given output sequence exists, print the lexicographically smallest such setting: the values s1,s2,…,sNs_1, s_2, \ldots, s_N separated by spaces on a single line. Among all valid settings, compare them entry by entry starting from s1s_1 and prefer the one that has 0 rather than 1 at the first position where they differ. If no setting can produce the given output, print -1.

Examples3

  1. Example 1

    Input
    7
    1 0 0 1 1 0 1 0 1 1 0 0 1 1
    
    Expected output
    1 0 1 1 0 1 1
    
  2. Example 2

    Input
    1
    1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    1 0
    
    Expected output
    0