快速幂
LL fast_power_mod (LL ans ,LL base, LL pow_for_base, LL MOD) {
while(pow_for_base){
if(pow_for_base & 1) {
ans = ans * base % MOD;
}
base = base * base % MOD;
pow_for_base >>= 1;
}
return ans;
}
矩阵快速幂(3*3)
struct Matrix
{
long long elem[3][3];
};
Matrix Multi( Matrix M1, Matrix M2 )
{
Matrix Ans;
for( int k = 0; k < 3; k++ )
{
for( int i = 0; i < 3; i++ )
{
for( int j = 0; j < 3; j++ )
{
Ans.elem[i][j] = M1.elem[i][k] * M2.elem[k][i];
}
}
}
return Ans;
}
void Mat_Copy( Matrix *M1, Matrix M2 )
{
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 3; j++ )
M1->elem[i][j] = M2.elem[i][j];
}
Matrix Fast_Pow_Mod( Matrix Ans, Matrix Base, int power )
{
while( power )
{
if( power & 1 )
Mat_Copy(&Ans, Multi(Ans, Base));
Mat_Copy(&Base, Multi(Base, Base));
power >>= 1;
}
return Ans;
}