Print the forced next term of K consecutive outputs from a linear generator modulo an unknown prime up to 10^D, or report that it is unknown.
Medium6Brute forceNumber theoryMathNo attempts yetTime limit5sMemory limit512 MBI want to build an online poker site. One important part of such a system is the random number generator. It has to be fast and random enough. Here is the compromise I came up with.
I need to generate random numbers of length at most D. First I pick a prime P with P≤10D. Then I pick non-negative integers A and B. Finally I pick an integer seed S with 0≤S≤P−1.
To print my sequence of pseudo-random numbers I first print S, then update S with
S:=(A×S+B)modP
I print the updated S as the next term of the sequence and update it again with the same formula. I repeat that as often as I want.
Do you think this is a good random number generator? Write a program that reads K consecutive terms of a sequence produced this way and prints the term that comes next.
The first line contains the number of test cases T. The first line of each test case contains D and K. The next line contains K consecutive terms produced by a random number generator of the kind described above.
For each test case print one line in the form Case #x: y, where x is the test case number starting from 1.
Consider every choice of a prime P≤10D and non-negative integers A and B that produces the K given terms. If all of those choices give the same next term, then y is that value. If two or more different next terms are possible, then y is I don't know.