963. [NOI2012] Random number generator
★ Import File: randoma.in
output file: randoma.out
Simple comparison
Time limit: 1 s memory limit: MB
"Problem description"
The building has recently been fascinated by random algorithms, and random numbers are the basis for generating random algorithms. Building ready to use linear congruential (Linear congruential method) to generate a random sequence, this method needs to set four non-negative integer parameters m,a,c,x[0], according to the following formula to generate a series of random number {xn}:x[n+1]= (ax[n]+c MoD m where mod m represents the remainder of the preceding number divided by M. As you can see 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, that is x[n] MoD g, you just need to tell the building the number he wants X[n] MoD g is as much as it can be.
"Input Format"
The input file randoma.in contains 6 integers separated by spaces m,a,c,x[0],n and G, where a,c,x[0] is a non-negative integer and M,n,g is a positive integer.
"Output Format"
Output to file Randoma.out, output a number, i.e. X[n] MoD g
"Sample Input"
11 8 7 1 5 3
"Sample Output"
2
"Sample description"
Calculated x[n]=x[5]=8, therefore (X[n] mod g) = (8 mod 3) = 2
"Data Size"
M is prime in 40% of the data
30% of the data in M and A-1 Coprime
50% of the data in N<=10^6
100% of the data in n<=10^18
40% of Data m,a,c,x[0]<=10^4
85% of Data m,a,c,x[0]<=10^9
100% of the data in m,a,c,x[0]<=10^18
100% of the data in G<=10^8
For all data, n>=1,m>=1,a>=0,c>=0,x[0]>=0,g>=1.
Matrix multiplication + fast power, longlong deposit, self-write multiplication.
1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Long Longm,a,c,x,n,g;6 Long LongMulLong LongXLong Longy) {7 Long Longret=0;8 for(; x;x>>=1, (y<<=1)%=m)9 if(x&1)Ten(ret+=y)%=m; One returnret; A } - structdata{ - Long Longmat[5][5]; the intr,c; -Dataoperator*(Data a) { - Data B; -memset (B.mat,0,sizeof(B.mat)); + for(intI=1; i<=r;i++) - for(intj=1; j<=a.c;j++) + for(intk=1; k<=c;k++) A(B.mat[i][j]+=mul (Mat[i][k],a.mat[k][j]))%=m; atB.r=R; -B.c=A.C; - returnb; - } -Dataoperator^(Long Longk) { - Data ret,x; inX.r=r;x.c=c;ret. R=r,ret. C=C; -memset (Ret.mat,0,sizeof(Ret.mat)); toret.mat[1][1]=ret.mat[2][2]=1; +memcpy (X.mat,mat,sizeof(MAT)); - while(k) { the if(k&1){ *ret=ret*x; $ }Panax Notoginsengk>>=1; -x=x*x; the } + returnret; A } the }a,b; + - intMain () { $Freopen ("randoma.in","R", stdin); $Freopen ("Randoma.out","W", stdout); -scanf"%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x,&n,&g); -B.r=b.c=2; theA.r=a.c=2; -b.mat[1][1]=a%m; b.mat[1][2]=c%m;Wuyib.mat[2][1]=0; b.mat[2][2]=1; b=b^N; thea.mat[1][1]=x; a.mat[1][2]=0; -a.mat[2][1]=1; a.mat[2][2]=0; a=b*A; Wuprintf"%lld\n", a.mat[1][1]%g); - return 0; About}
Matrix (Fast power): COGS 963. [NOI2012] Random number generator