2015
09-17

# Random Number Generator

A random number generator (RNG) is a computational or physical device designed to generate a sequence of numbers or symbols that lack any pattern, i.e. appear random. Many of these have existed since ancient time, including dices, coin flipping, the shuffling of playing cards, and many other techniques. These methods depend on the measurement of some physical phenomenon which is expected to be random, and are still widely used today. On the other hand, deterministic computational algorithms were introduced into random number generation. Despite such algorithms’ ability to produce apparently random results, they are in fact determined by a shorter initial value, known as a seed or key. These algorithms are often called pseudorandom number generators. They can also be called RNG customarily, but actually differ with real RNG significantly.
Now considering a simple RNG, whose algorithm has two positive integer parameters A, B and a prime parameter M (2 ≤ A, B < M ≤ 1018). To run the algorithm, a seed X0 (0 ≤ X0 < M) is required, and the algorithm produces a integer sequence Xn satisfying the condition Xn = (A × Xn – 1 + B) mod M for any positive integer n.
An application implemented this algorithm. This application has another two parameters S (S ≤ M) and K (K ≤ 105), and will use this RNG in such a way. Firstly, the application generates the first K integers in the random sequence including the seed, and these numbers modulo S are stored in another number sequence D, i.e. Di = Xi mod S for any integer i in [0, K - 1]. Then, another random integer XK is produced, and the application chooses the ((XK mod K) + 1)-th number in sequence D, i.e. DXk mod K as its output C. If an output C (0 ≤ C < S) is observed in a certain run, and parameters A, B, M, S, K is known, your task is determining a possible X0 which leads to the output C.

There are at most 200 test cases. Each test case is a single line containing six integers, A, B, M, S, K and C, seperated by space. The meaning of these numbers are described above.
The input is ended by 0 0 0 0 0 0

There are at most 200 test cases. Each test case is a single line containing six integers, A, B, M, S, K and C, seperated by space. The meaning of these numbers are described above.
The input is ended by 0 0 0 0 0 0

2 2 97 5 3 2
2 2 97 5 3 3
0 0 0 0 0 0

2
8

1. 张鸣先生，其实你也够勇敢的了，每日一呼，如果钱杨做到你这一点，他们就太伟大了，而他们连这点都做不到，确实犬儒，他们自己行为犬儒，还怕别人说或者不许别人说么？

2. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

3. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

4. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

5. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

6. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

7. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

8. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

9. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

10. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。

11. 世上人间，有万岁的人民，无万岁的政权。历来如此，继续如此，永远如此，难能抗拒。尽管专政权力自夸多少个自信，也难逃脱复亡命运。