[Bzoj2875] [noi2012] random number generator

Source: Internet
Author: User
Description

Dong has recently become fascinated by random algorithms, and random number generation is the basis of Random Algorithms. Dong is going to use the linear congruential method to generate a random series. This method requires four non-negative integer parameters m, a, c, x0, generate a series of random numbers according to the following formula <XN>:
Xn + 1 = (AXN + C) mod m
MoD M indicates dividing the previous number by the remainder of M. We can see from this formula that the next number of this sequence is always generated by the previous number.
This method is widely used because the sequence generated by a method has the property of a random sequence. This method is also used by library functions that commonly use C ++ and Pascal to generate random numbers.
Knowing that such a sequence has good randomness, he still wants to know the number of XN as soon as possible. The random number required by the building is 0, 1 ,..., For the value between g−1, The XN must be divided by G. Obtain the number he wants from the remainder, that is, xn modg. You only need to tell Dongdong how many XN mod g he wants.

Input

Contains 6 spaces separated by m, a, c, x0, N and G, where a, c, x0 is a non-negative integer, M, N, G is a positive integer.

Output

Output a number, that is, xn mod g

Sample input11 8 7 1 5 3
Sample output2
Hint

1 <= n, m, a, c, x0 <= 10 ^ 18, 1 <= G <= 10 ^ 8

 

Solution

Bare matrix fast power + fast multiplication. Note that the matrix of 2*2 has a row with a constant of 0, 1, which can save half of the time. The answer is % m first and then % G.

T * 4 is because ll is converted into Int =. It is time to reflect on this inexplicable mistake even if I click on this question...

 

 1 #include<cstdio> 2 typedef long long ll; 3 struct M{ll x,y;}t,f; 4 ll m,x0,n,g; 5 ll mult(ll t,ll k) 6 { 7     ll f=0; 8     for(;k;k>>=1,t=(t+t)%m)if(k&1)f=(f+t)%m; 9     return f;10 }11 M operator*(const M&a,const M&b)12 {13     return (M){mult(a.x,b.x),(mult(a.x,b.y)+a.y)%m};14 }15 M operator^(M t,ll k)16 {17     M f=t;for(--k;k;k>>=1,t=t*t)if(k&1)f=f*t;18     return f;19 }20 int main(){21     scanf("%lld%lld%lld%lld%lld%lld",&m,&t.x,&t.y,&x0,&n,&g);22     t=t^n;printf("%lld\n",(mult(x0,t.x)+t.y)%m%g);23 }
View code

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.