"Matrix Multiply" "NOI 2012" "cogs963" random number generator

Source: Internet
Author: User
Tags generator time limit
963. [NOI2012] random number generator
★ ☆   Input File: randoma.in   output file: Randoma.out   Simple contrast
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"

40% of the data in M is prime
30% of the data in M and A-1 Coprime

50% of the data n<=10^6
100% n<=10^18

40% of data m,a,c,x[0]<=10^4
85% of the data in m,a,c,x[0]<=10^9
100% m,a,c,x[0]<=10^18

100% of the data g<=10^8

for all data, n>=1,m >=1,a>=0,c>=0,x[0]>=0,g>=1.

The following:

Compare simple matrix multiplication for two matrices:
a[a,c0,1] A \begin{bmatrix} A, c \ \ 0, 1 \ \end{bmatrix}
B[X[N−1]1] B \begin{bmatrix} x[n-1] \ 1 \ \end{bmatrix}
Obviously, X[n] can be multiplied by the two matrices:
A∗B=C[X[N]1] A*b=c \begin{bmatrix} x[n] \ 1 \ \end{bmatrix}
So for X[n], we can ask:
AN∗[X[0]1] a^n* \begin{bmatrix} x[0] \ 1 \ \end{bmatrix}
Compare deceptive is need to write fast multiply, because ordinary multiply will fry ...
(PS: Fast multiply almost and fast power to write up, just need to change * to +)

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath
> #include <algorithm> using namespace std;
Long Long a[2][2]={{0,0},{0,1}},b[2][2]={{0,0},{1,0}},c[2][2]={0};
Long Long n,g,m,nn;
    Long Long X,long (long long) {Long long z=0; X%=m;
    Y%=m;
    if (x<y) swap (x, y);
        while (y) {if (y&1) z= (z+x)%m;
        x= (2*x)%m;
    y>>=1;
} return Z;
    } int main () {scanf ("%lld%lld%lld%lld%lld%lld", &m,&a[0][0],&a[0][1],&b[0][0],&n,&g);
    Nn=n;
            while (NN) {if (nn&1) {memset (c,0,sizeof (C));
                        for (int i=0, i<2; i++) for (int j=0; j<2; j + +) for (int k=0; k<2; k++)
            C[i][j]= (C[I][J]+KC (a[i][k],b[k][j))%m;
        for (int i=0, i<2; i++) for (int j=0; j<2; j + +) B[i][j]=c[i][j]; } nn>>=1;
        memset (c,0,sizeof (C));
                    for (int i=0, i<2; i++) for (int j=0; j<2; j + +) for (int k=0; k<2; k++)
        C[i][j]= (C[I][J]+KC (a[i][k],a[k][j))%m;
    for (int i=0, i<2; i++) for (int j=0; j<2; j + +) A[i][j]=c[i][j];
    } printf ("%lld\n", b[0][0]%g);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.