"Matrix multiplication + fast multiplication" bzoj2875-[noi2012] random number generator

Source: Internet
Author: User

"The main topic"

Knownx + = (a xn +c) mod m, ask Xn mod g.

Ideas

Get to the correct method of longlong multiplication, fast multiply. What is a fast ride?

Simply put, the fast power is the vertical multiplication that simulates the binary. Such as:

10101x1011 = 10101*1+10101*2^1*1+10101*2^2*0+10101*2^3*1

The code is as follows:

Long LongMultiLong LongALong LongBLong Longm) {Long Longans=0;  while(b) {if(b&1) (Ans+=a)%=m; (A=a*2) %=m; b/=2; }    returnans;}

The next method of the subject is the fast power of matrix multiplication.

(A 0

C 1) squared n times

Notice in the function also don't forget to open longlong. At first, the N in my function was written as a int,wa.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong Longll;7ll matrix[2][2],ans_matrix[2][2];8 ll m,n,a,c,g,x0;9 Ten ll KSC (ll A,ll b) One { All ans=0;  -      while(b) -     { the         if(b&1) ans= (ans+a)%m; -A= (a<<1)%m; -b>>=1; -     } +     returnans; - } +  A voidKSM (ll N) at { -ans_matrix[0][0]=ans_matrix[1][1]=1; -ans_matrix[0][1]=ans_matrix[1][0]=0; -      while(n) -     { -         if(n&1) in         { -ans_matrix[0][0]=KSC (ans_matrix[0][0],matrix[0][0]); toans_matrix[1][0]= (KSC (ans_matrix[1][0],matrix[0][0]) +matrix[1][0])%m; +         } -n>>=1; thell TMP1=KSC (matrix[0][0],matrix[0][0]); *ll tmp2= (KSC (matrix[1][0],matrix[0][0]) +matrix[1][0])%m; $matrix[0][0]=tmp1,matrix[1][0]=TMP2;Panax Notoginseng     } - } the  + voidInit () A { thescanf"%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x0,&n,&g);  +matrix[0][0]=a%m,matrix[0][1]=0, matrix[1][0]=c%m,matrix[1][1]=1; - } $  $ voidGet_ans () - { -ll ans= (KSC (ans_matrix[0][0],x0) +ans_matrix[1][0])%m; theans%=G; -printf"%lld", ans);Wuyi } the  - intMain () Wu { -     //freopen ("randoma.in", "R", stdin); About     //freopen ("Randoma.out", "w", stdout); $ init (); - KSM (n); - Get_ans (); -     return 0; A}

"Matrix multiplication + fast multiplication" bzoj2875-[noi2012] random number generator

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.