文件详细信息
本文件的大小为 832 字节。
#include<cstdio>
typedef long long ll;
ll m,a,c,x0,n,g;
ll mul(ll x,ll y){
ll ans=0;
while(y){
if(y&1)ans=(ans+x)%m;
x=(x<<1)%m;
y>>=1;
}
return ans;
}
struct Matrix{
ll a[2][2];
Matrix(){
a[0][0]=a[0][1]=a[1][0]=a[1][1]=0;
}
ll*operator[](int x){
return a[x];
}
}I,init,trans;
Matrix operator*(Matrix a,Matrix b){
Matrix res;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
res[i][k]=(res[i][k]+mul(a[i][j],b[j][k]))%m;
return res;
}
Matrix qpow(Matrix a,ll x){
Matrix res=I;
while(x){
if(x&1)res=res*a;
a=a*a;
x>>=1;
}
return res;
}
int main(){
I[0][0]=I[1][1]=1;
scanf("%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x0,&n,&g);
init[0][0]=x0;
init[0][1]=1;
trans[0][0]=a;
trans[1][0]=c;
trans[1][1]=1;
Matrix ans=init*qpow(trans,n);
printf("%lld\n",ans[0][0]%g);
return 0;
}