bzoj2875 和 lg2044
矩阵快速幂…
[x01]
[
x
0
1
]
[ac01]n
[
a
0
c
1
]
n
要注意longlong吧
两个数乘的时候也要注意,会超出longlong,所以也用倍增
#include <cstdio>
#include <cstring>
#define N 100
long long m,a,c,x,n,g,t[5],s[5],b[5],d[5];
long long add(long long x,long long y){
long long s1=0,t1=y;
while(x){
if(x&1) s1=(s1+t1)%m;
t1=2*t1%m;
x>>=1;
}return s1;
}
void mul1(){
s[1]=(add(s[1],t[1])+add(s[2],t[2]))%m;
}
void mul2(){
t[2]=(add(t[2],t[1])+t[2])%m;
t[1]=add(t[1],t[1]);
}
int main(){
freopen("random.in","r",stdin);
scanf("%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x,&n,&g);
a%=m,c%=m,x%=m;
s[1]=x,s[2]=1;
t[1]=a,t[2]=c;
while(n){
if(n&1) mul1();
mul2();
n>>=1;
}printf("%lld",s[1]%g);
return 0;
}