#P2044. [NOI2012] 随机数生成器

    ID: 986 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学贪心2012矩阵运算NOI 系列素数判断,质数,筛法矩阵乘法

[NOI2012] 随机数生成器

Description

Dongdong has recently become fascinated with randomized algorithms, and random numbers are the foundation for generating them. He plans to use the Linear Congruential Method to generate a sequence of random numbers. This method requires setting four non-negative integer parameters m,a,c,X0m, a, c, X_0, and generates a sequence {Xn}\{X_n\} according to the formula:

Xn+1=(aXn+c)modmX_{n+1} = (a X_n + c) \bmod m

Here, modm\bmod m denotes the remainder when the preceding number is divided by mm. From this equation, we can see that the next number in the sequence is always determined by the previous one.

Sequences generated by this method have properties of random sequences, so it is widely used. The standard library functions for generating random numbers in C++ and Pascal also use this method.

Dongdong knows that the sequence produced this way has good randomness, but he is impatient and wants to know XnX_n as soon as possible. Since the random numbers he needs are between 0,1,,g10, 1, \dots, g - 1, he will take XnX_n modulo gg to get the number he wants, i.e., XnmodgX_n \bmod g. You only need to tell Dongdong the value of XnmodgX_n \bmod g.

Input Format

One line contains 6 space-separated integers m,a,c,X0,nm, a, c, X_0, n and gg, where a,c,X0a, c, X_0 are non-negative integers, and m,n,gm, n, g are positive integers.

Output Format

Output a single number, which is XnmodgX_n \bmod g.

11 8 7 1 5 3
2

Hint

We compute Xn=X5=8X_n = X_5 = 8, thus (Xnmodg)=(8mod3)=2(X_n \bmod g) = (8 \bmod 3) = 2.

Constraints: For 100%100\% of the testdata, n,m,a,c,X01018n, m, a, c, X_0 \leq 10^{18}, 1g1081 \leq g \leq 10^8, n,m1n, m \geq 1, a,c,X00a, c, X_0 \geq 0.

Translated by ChatGPT 5