bzoj2875: [Noi2012]随机数生成器

x[n] = {x[0],1} * ( {a,0} ^ n )

{b,1}

noi里的大水题。我居然

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
const int maxn = 2;

LL a,b,mod,g,x,n,ans;

LL mul(long long a,long long b) {
a%=mod; b%=mod;
long long res=0;
while(b) {
if(b&1) res=(res+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return res;
}

struct Matrix {
const static LL n = 1;
LL a[maxn][maxn];

inline LL* operator [] (int x) {
return a[x];
}

Matrix operator* (Matrix b) {
Matrix res;
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
for(int k=0;k<=n;k++)
res[i][k]=(res[i][k]+mul(a[i][j],b[j][k]))%mod;
return res;
}

void build() {
for(int i=0;i<=n;i++) a[i][i]=1;
}

Matrix operator^ (long long e) {
Matrix res,tmp=*this;
res.build();
while(e) {
if(e&1) res=res*tmp;
tmp=tmp*tmp;
e>>=1;
}
return res;
}

Matrix () {
memset(a,0,sizeof(a));
}
}res;

int main() {
cin >> mod >> a >> b >> x >> n >> g;
res[0][0]=a; res[1][0]=b; res[1][1]=1;
res=res^n;
ans=(mul(x,res[0][0])+res[1][0])%mod;
ans=ans%g;
cout << ans <<'\n';
return 0;
}

posted @ 2016-06-16 20:26  invoid  阅读(26)  评论(0编辑  收藏