Pseudo-random Numbers
Time limit1sMemory limit128 MB
Given the first L digits of a pseudo-random sequence generated by repeatedly summing adjacent base-B digits, decide whether the T-th element is forced, or report impossible or unpredictable.
- Level
Hard8 of 10
- Topics
- Math, Implementation, Number theory, Brute force
- Solved
- No attempts yet
Problem
Many applications need good random numbers, cryptography most of all. Radioactive decay is sometimes used as a source of true randomness, but it delivers numbers slowly. Many applications also have to reproduce the same "random" sequence in two different places. A pseudo-random sequence is used instead. Such a sequence is not random, yet it is very hard to tell apart from a truly random one, and it should be hard to predict: knowing the first few elements should not make it easy to work out a later element nobody has seen yet.
The Association of Cryptographic Machinery (ACM) designed an algorithm that produces pseudo-random sequences, and nobody there knows how good it really is, so they want you to test it.
Every element the algorithm produces is an integer between and inclusive. The algorithm works as follows.
- Start with any seed written in base . The seed is a nonempty string of base digits, it may begin with zeros, and it can be hundreds of digits long.
- Output the last digit (the least significant one) as the next element of the sequence.
- Build a new number by writing down the sums of neighbouring digits from left to right. Each sum is written in base , so a sum of or more takes two digits. With the number becomes , because and .
- Repeat steps 2 and 3 until the number has a single base digit. That digit is the last element of the sequence and the algorithm stops.
With and the seed the numbers are , , ( and ), ( and ) and (). The last one has a single digit, so the algorithm stops there. The pseudo-random digits it produced are , , , and .
You test the generator like this. You are given the first elements the generator produced and an integer . Decide whether the first elements completely determine the first elements. If some seed that produces the given elements yields a sequence of fewer than elements, then the first elements are not determined. The ACM also slipped in a few impossible sequences that no seed can produce, to see how robust your test is.
Input
The first line has one positive integer , the number of test cases. Each test case takes three lines. The first line has the base (). The second line has an integer () followed by the first elements of some sequence; the elements are written in base 10 and lie between and inclusive. The third line has the integer (), the position of the element to predict.
Output
For each test case print one line:
impossibleif no seed produces the given sequence;unpredictableif some seed produces the given sequence but the first elements do not completely determine the first elements;- otherwise the -th element of the sequence, written in base 10.