Given K consecutive outputs of a linear congruential generator with unknown prime modulus up to 10^D, decide the uniquely forced next term or report ambiguity.
Medium7Number theoryBrute forceMathNo attempts yetTime limit5sMemory limit512 MBI want to build an online poker site. The random number generator is an important part of such a system, and it has to be fast and random enough. Here is the compromise I came up with.
I need random numbers of at most D digits. First I pick a prime P with P≤10D. I also pick non-negative integers A and B. Finally I pick an integer seed S with 0≤S≤P−1.
The sequence comes out like this. First I output S, then I update S with
S:=(A×S+B)modP
I output the updated S as the next term of the sequence and update S again with the same formula. I repeat this as many times as I want.
Do you think this is a good random number generator? Write a program that reads K consecutive terms of a sequence made this way and prints the term that comes right after them.
The first line has the number of test cases T. Each test case then takes two lines. The first line has D and K. The second line has K consecutive terms of a sequence made by the generator described above.
For each test case, print one line in the form Case #x: y, where x is the test case number and starts from 1.
Consider every combination of a prime P≤10D and non-negative integers A and B that produces exactly the given K terms. If all of those combinations give the same next term, print that value as y. If two of them give different next terms, print I don't know. as y, period included.