快速幂模板

来源:互联网 发布:在绿城工作怎么样知乎 编辑:程序博客网 时间:2024/04/30 13:50
#define MOD 1000000007
struct Matrix
{
int row,col;
long long m[10][10];
Matrix(){}
Matrix(long long s,int x,int y):row(x),col(y) //构造单位矩阵或0矩阵
{
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
</pre><span style="white-space:pre"></span>m[i][j]=i==j?s:0;<pre name="code" class="plain">


}
Matrix(long long *a,int x,int y):row(x),col(y) //以一维数组为参数构造矩阵
{
for(int i=0;i<x;i++)
for(int j=0;j<y;j++)
m[i][j]=a[i*y+j];
}
Matrix operator * (const Matrix a)const //矩阵乘法(朴素O(n^3)算法)
{
Matrix res(0LL,row,a.col);
for(int i=0;i<row;i++)
for(int j=0;j<a.col;j++)
for(int k=0;k<col;k++)
res.m[i][j]=(res.m[i][j]+m[i][k]*a.m[k][j])%MOD;
return res;
}
Matrix operator ^ (long long n) //矩阵快速幂
{
Matrix ans(1LL,row,col);
while(n)
{
if(n&1)
ans=ans**this;
*this=*this**this;
n>>=1;
}
return ans;
}
};
1 0
原创粉丝点击