Description
The building has recently been fascinated by random algorithms, and random number generation is the basis of random algorithms. Building preparation using linear Yufarai to generate a random sequence, this method needs to set four non-negative parameters m,a,c,x[0], according to the following formula to generate a series of random number <x[n]>
x[n+1]= (ax[n]+c) mod m
Where mod m represents the remainder of the preceding number divided by M. As can be seen from this equation, the next number of this sequence is always generated by the previous number.
The sequence generated by this method has the properties of a random sequence, so this method is widely used, including the usual C + + and Pascal generated random number of library functions used by this method.
The building knows that the resulting sequence has good randomness, but he still wants to know as soon as possible how much x[n] is. Since the random number required for the building is between 0,1,... g-1, he needs to divide x[n] by G to get the number he wants, i.e. X[n] MoD g, you just need to tell the building what he wants X[n] MoD g is
How much will it be.
formatInput Format
Contains 6 integers separated by spaces m,a,c,x0,n and G, where a,c,x0 is a non-negative integer and M,n,g is a positive integer.
output Format
Output a number, which is xn mod g.
Example 1sample input 1[copy]
11 8 7 1 5 3
sample Output 1[copy]
2
Limit
1s per test point
Tips
1:n<=100, m,a,c,x0<=100 m is prime
2:n<=1000, m,a,c,x0<=1000 m is prime
3:n<=10^4, m,a,c,x0<=10^4 m is prime
4:n<=10^4, m,a,c,x0<=10^4 m is prime
5:n<=10^5, m,a,c,x0<=10^4 m and A-1 Coprime
6:n<=10^5, m,a,c,x0<=10^4 m and A-1 Coprime
7:n<=10^5, m,a,c,x0<=10^4 m and A-1 Coprime
8:n<=10^6, M,a,c,x0<=10^4
9:n<=10^6, m,a,c,x0<=10^9 m is prime
10:n<=10^6, M,a,c,x0<=10^9
11:n<=10^12, m,a,c,x0<=10^9 m is prime
12:n<=10^12, m,a,c,x0<=10^9 m is prime
13:n<=10^16, m,a,c,x0<=10^9 m and A-1 Coprime
14:n<=10^16, m,a,c,x0<=10^9 m and A-1 Coprime
15:n<=10^16, M,a,c,x0<=10^9
16:n<=10^18, M,a,c,x0<=10^9
17:n<=10^18, M,a,c,x0<=10^9
18:n<=10^18, m,a,c,x0<=10^18 m is prime
19:n<=10^18, m,a,c,x0<=10^18 m and A-1 Coprime
20:n<=10^18, m,a,c,x0<=10^18
basic idea: Matrix + fast Power
This problem seems to have a general formula, but seemingly no one on the net said,, the idea of fast power not only to combine with the matrix, but also the multiplication needs. Because a long long directly by the positive explosion, so the multiplication to add .
Build a matrix
X0 C
A 0
1 1
- Note that the output should be "%LLD"
1#include <iostream>2#include <algorithm>3#include <cstring>4 5 using namespacestd;6 7 #definell Long Long8 9 ll M, A, C, N, G, X;Ten One ll multi (ll y, ll cnt) { A if(! cnt)return 0 ; - if(CNT = =1)returnY%m; -LL rec = multi (y, CNT/2 ) ; theREC = (rec + rec)%m; - if(CNT%2) Rec = (rec + y)%m; - returnRec; - } + - structMaxtrix { +ll a[2][2 ] ; A Maxtrix () { atMemset (A,0,sizeof(a)); - } - voidprint () { - for(inti =0; I <2; i + + ) { - for(intj =0; J <2; J + + ) { -cout << a[I [j] <<" " ; in } -cout <<Endl; to } + } - }; the * Maxtrix Multi (Maxtrix M1, Maxtrix m2) { $ Maxtrix Rec;Panax Notoginseng for(inti =0; I <2; i + + ) { - for(intj =0; J <2; J + + ) { the for(intK =0; K <2; K + + ) { +rec.a[I [j] + =multi (m1.a[I [K], m2.a[K] [j]); Arec.a[I [j]%=m; the } + } - } $ returnRec; $ } - - Maxtrix Pow (Maxtrix x, ll cnt) { the if(CNT = =1)returnx; -Maxtrix rec = Pow (x, CNT/2 ) ;WuyiREC =Multi (REC, rec); the if(CNT%2) rec =Multi (REC, x); - returnRec; Wu } - About intMain () { $CIN >> M >> a >> c >> x >> n >>G; - Maxtrix M1, M2; -m2.a[0][0] = A, m2.a[0][1] =0, m2.a[1][0] = C, m2.a[1][1] =1 ; -m1.a[0][0] = x, m1.a[0][1] =1 ; AMaxtrix ans =Multi (M1, Pow (M2, n)); +cout << ans.a[0][0]% G <<Endl; the return 0 ; -}
NOI 2012 Random Number generator