Linear-Feedback Shift Register
InterviewTime limit1.5sMemory limit256 MB
Given the 36 feedback bits of an LFSR and up to 64 output bits, decide whether some 36-bit initial state produces them, and print the lexicographically smallest such state.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Math, Greedy, Implementation
- Solved
- No attempts yet
Problem
Do you know about the LFSR (Linear-Feedback Shift Register)? An LFSR is also used to generate pseudorandom values. We will skip studying it in detail here and instead give a brief definition in formulas. The LFSR in this problem is a 36-bit LFSR.
It is defined by an initial value r0 ~ r35 (ri = 0 or 1) and values a0 ~ a35 (ai = 0 or 1) that define which values are XORed to form the next Output. The Outputs of the LFSR are the r values from r36 onward. The r values after r36 are defined as follows. (k is a nonnegative integer)
rk+36 = (a35 · rk+35) ⊗ (a34 · rk+34) ⊗ ... ⊗ (a0 · rk)
(Bitwise AND is written as ·, and Bitwise XOR as ⊗.)
This problem asks the following: given a0 ~ a35, and given N Outputs, that is, the values from r36 to r35+N, determine whether there exists an initial value r0 ~ r35 that satisfies them.
Input
The first line gives a0 ~ a35 separated by spaces.
The second line gives the number of Outputs N(1 ≤ N ≤ 64) that will be provided as input.
The third line gives r36 ~ r35+N separated by spaces.
Output
On the first line, print YES or NO depending on whether a valid r0 ~ r35 exists. If YES, then on the second line print the lexicographically earliest combination among the valid r0 ~ r35 combinations, in the order r0, r1, ... , r35 separated by spaces, where r0r1r2...r35 is compared lexicographically. 000...000 is the lexicographically earliest, and 111...111 is the lexicographically latest.
Hint
The first example can be expanded as r36 = r35 ⊗ r34 ⊗ r33 = 1, r37 = r36 ⊗ r35 ⊗ r34 = 0, r38 = r37 ⊗ r36 ⊗ r35 = 1. The only r33, r34, r35 satisfying this are 0, 1, 0. r0 ~r32 can be anything. However, for the lexicographically earliest among these, we must have r0, r1, ... , r32 = 0.
In the second example, all ai values are 0, so the only possible Output is 0. However, 1 was given, so the answer is NO.