NOI '12 P1 - Random Number Generator

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 0.6s
Memory limit: 512M

Problem type
National Olympiad in Informatics, China, 2012

DongDong recently became obsessed with randomization, and random number generators are the foundation of randomization. DongDong plans to use the linear congruential method to generate a number sequence. This method requires nonnegative integer parameters m, a, c, X0. Then, a random sequence Xn can be generated using the following formula:

Xn+1=(aXn+c)modm

where "modm" means to take the remainder after the left hand expression has divided m. It can be observed from this equation that the next number in a sequence will always be based on the current number.

Sequences generated this way are very random in nature, which is why this method has been used extensively. Even randomization functions in the widely used standard libraries of C++ and Pascal employ this method.

DongDong knows that sequences produced this way are very random, but he is also impatient about wanting to know the value Xn as soon as possible. Since DongDong needs a random number that's one of 0,1,,g1, he will have to modulo the number Xn by g to get the final result. All you have to do is determine the value of Xnmodg for DongDong.

Input Specification

The input consists of 6 space-separated integers m, a, c, X0, n, and g. Here, a, c, and X0 are nonnegative while m, n, g are positive.

Output Specification

Output a single integer, the value of Xnmodg.

Sample Input

Copy
11 8 7 1 5 3

Sample Output

Copy
2

Explanation

The first few terms of Xn are:

k 0 1 2 3 4 5
Xk 1 4 6 0 7 8

Thus the answer is X5modg=8mod3=2.

Constraints

Test Case n m,a,c,X0 m,a g
1 n100 m,a,c,X0100 m is prime g108
2 n1000 m,a,c,X01000 m is prime
3 n104 m,a,c,X0104 m is prime
4 n104 m,a,c,X0104 m is prime
5 n105 m,a,c,X0104 m and a1 are prime
6 n105 m,a,c,X0104 m and a1 are prime
7 n105 m,a,c,X0104 m and a1 are prime
8 n106 m,a,c,X0104 /
9 n106 m,a,c,X0109 m is prime
10 n106 m,a,c,X0109 /
11 n1012 m,a,c,X0109 m is prime
12 n1012 m,a,c,X0109 m is prime
13 n1016 m,a,c,X0109 m and a1 are prime
14 n1016 m,a,c,X0109 m and a1 are prime
15 n1016 m,a,c,X0109 /
16 n1018 m,a,c,X0109 /
17 n1018 m,a,c,X0109 /
18 n1018 m,a,c,X01018 m is prime
19 n1018 m,a,c,X01018 m is prime
20 n1018 m,a,c,X01018 /

For all of the test cases: n,m,g1 and a,c,X00.

Problem translated to English by Alex.


Comments

There are no comments at the moment.