求m^n mod p,时间复杂度(logk)
//求 a^b
int qpow(int ,int b){
int res=1;
while(b>0){
if(b&1){
res*=a;
}
a*=a;
b>>1;
}
return res;
}
//求a^b % p 因为(a%p)^b%p
int qmi_p(int m,int k,int p){
int res=1,t=m;
while(k){
if(k&1) res=res*t%p;
t=t*t%p;
k >>=1;
}
return res;
}
//矩阵快速幂
const long long mod = 1e9+7;
struct Matrix {
long long a[2][2];
Matrix() {
memset(a,0,sizeof a);
}
Matrix operator *(const Matrix y){
Matrix ans;
for (int i = 0;i <= 1;i++)
for(int j = 0;j <= 1;j++){
for(int k = 0;k <= 1;k++){
ans.a[i][j] += a[i][k] * y.a[k][j];
}
ans.a[i][j] %= mod;
}
return ans;
}
void operator = (const Matrix b){
for(int i = 0;i <= 1; i++)
for(int j = 0;j <= 1; j++)
a[i][j]=b.a[i][j];
}
};
long long qmi_Matix(long long x){
Matrix ans,trs;
ans.a[0][0] = ans.a[1][1] = 1;
trs.a[0][0] = trs.a[0][1] = trs.a[1][0] = 1;
while(x){
if(x & 1){
ans = ans * trs;
}
trs = trs*trs;
x >>= 1;
}
return ans.a[0][0];
}