Shift Register
Time limit1sMemory limit128 MB
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 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 holds the bits . On every clock tick the register outputs its rightmost bit , and every other bit moves one position to the right. The now-empty first position 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 has a switch that decides whether its bit is fed into the XOR gate () or not (). Let ; then equals the XOR-gate output . That is, it is 1 if the number of ones among is odd, and 0 otherwise.
- for
- output

For example, in the state shown above the bit newly filled in on the first tick is .
The first values of the sequence produced by the shift register are given. Write a program that recovers the switch values .
Input
The first line contains the size of the shift register ().
The second line contains the first 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 separated by spaces on a single line. Among all valid settings, compare them entry by entry starting from 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.