题目描述 Description
给你6个数,m, a, c, x0, n, g
Xn+1 = ( aXn + c ) mod m,求Xn
m, a, c, x0, n, g<=10^18
输入描述 Input Description
一行六个数 m, a, c, x0, n, g
输出描述 Output Description
输出一个数 Xn mod g
样例输入 Sample Input
11 8 7 1 5 3
样例输出 Sample Output
2
/*
这个题显然用矩阵乘法,公式也很好推。
*/
#include<cstdio>
#include<iostream>
#define lon long long
using namespace std;
lon m,a,c,x,n,g;
struct node{
lon v[][];
};
lon Mul(lon s1,lon s2){//快速乘
lon r=,base=s1;
while(s2){
if(s2&)r+=base;
base+=base;
r%=m;
base%=m;
s2>>=;
}
return r;
}
node cheng(node s1,node s2){
node s3;s3.v[][]=s3.v[][]=s3.v[][]=s3.v[][]=;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
for(int k=;k<=;k++){
s3.v[i][j]+=Mul(s1.v[i][k],s2.v[k][j]);//s1.v[i][k]*s2.v[k][j];
s3.v[i][j]%=m;
}
return s3;
}
node poww(node aa,lon bb){
node base=aa,r;
r.v[][]=r.v[][]=;
r.v[][]=r.v[][]=;
while(bb){
if(bb&)r=cheng(r,base);
base=cheng(base,base);
bb>>=;
}
return r;
}
int main(){
cin>>m>>a>>c>>x>>n>>g;
node s1,s2;
s1.v[][]=x;s1.v[][]=;
s1.v[][]=;s1.v[][]=;
s2.v[][]=a;s2.v[][]=;
s2.v[][]=c;s2.v[][]=;
node ans=poww(s2,n);
ans=cheng(s1,ans);
cout<<ans.v[][]%g;
return ;
}