E-Casino
Time limit2sMemory limit512 MB
Find the smallest shift k where the observed bits match the public array up to a repeating mask.
- Level
Easy3 of 10
- Topics
- String matching, Brute force
- Solved
- No attempts yet
Problem
A pseudo random number generator (PRNG) is a program that takes an input called a seed and outputs a sequence of random bits , where every bit is 0 or 1.
Fix one PRNG and one seed. A gambler is a person (or a computer) who has observed the first output bits . The gambler knows the algorithm of that PRNG and therefore knows how the bits are produced. The seed is the only secret. From the algorithm and the observed prefix, the gambler wants to predict the bits that come next, .
Designing a PRNG that resists this is not easy. The company E-Casino uses the following method. By observing some natural phenomenon, the company first produced a long sequence of random bits . The array is public, so gamblers can read it too. The seed is a pair made of an integer with and an -bit sequence . For the output bit is
where is the exclusive or operation: for bits and , .
The company always uses and . Every morning the managers of E-Casino meet and pick the secret seed that generates the sequence for that day.
You are the gambler. On one particular day you have observed the first output bits , and you also know the array . You do not know or . Recovering the later bits starts with recovering , so that is the job here. Find the smallest that agrees with the bits you observed.
Input
The input is a single line. Its first characters are the observed bits , each written as 0 or 1, the very first character being and the next one . One % character follows and marks the start of the array . The character right after % is , the next one is , and continues in the same order to the end of the line. The line therefore holds characters.
and are not given separately. The position of % is , and the number of characters after it is . E-Casino runs with and , which makes a line of 2113 characters, but inputs built from smaller settings also appear. In every case .
The observed bits always come from some seed, so at least one valid exists.
Output
Print the smallest value that can take in the secret seed.
Hint
The table below follows the computation of for , , , and . The third row holds the array . The values of through are left out to keep the table short.
j | 6 7 8 9 0 1 2 3 4 5
(k + j) mod N | 0 1 2 3 4 5 6 7 8 9
---------------------------------------
S[(k + j) mod N] | 1 1 1 0 0 1 0 1 0 1
m[j mod M] | 1 0 1 1 0 1
---------------------------------------
r[j] | 1 1 1 0 0 0
The first six output bits are 1, 1, 1, 0, 0, 0. A gambler who sees only those six bits, and knows neither nor , can still show that has to be 4: write down every combination of and and check which one produces those six bits. For and that exhaustive search is hopeless.