BZOJ2875 NOI2012 随机数生成器 矩阵乘法+快速幂

题意:Xn+1=(aXn+c)%M,求XN%G

题解:矩阵乘法裸题

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long

ll M,X,N,G;
ll Mul(ll a,ll b){
    a%=M,b%=M;
    ll t=(b&1?a:0);
    while(b>>=1){
        a=(a+a)%M;
        if(b&1) t=(t+a)%M;
    }
    return t;
}
struct MATRIX{
    ll x,y;
    MATRIX(){}
    MATRIX(ll _x,ll _y):x(_x),y(_y){}
    MATRIX operator*(MATRIX a){ return MATRIX(Mul(x,a.x),(Mul(x,a.y)+y)%M);}
    MATRIX operator^(ll b){
        MATRIX a=MATRIX(x,y),t=(b&1?MATRIX(x,y):MATRIX(1,0));
        while(b>>=1){
            a=a*a;
            if(b&1) t=a*t;
        }
        return t;
    }
}A;

int main(){
    scanf("%lld %lld %lld %lld %lld %lld",&M,&A.x,&A.y,&X,&N,&G);

    A=A^N;
    printf("%lld\n",(Mul(X,A.x)+A.y)%M%G);

    return 0;
}
View Code

 

posted @ 2017-02-28 23:23  WDZRMPCBIT  阅读(205)  评论(0编辑  收藏  举报