题意:
给出一个矩阵A,求A^x。
思路:
把矩阵当作整数进行快速幂。
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define GETMOD %1000000007
#define LL long long
using namespace std;
struct node
{
int xcnt,ycnt;
LL a[11][11];
node()
{
memset(a,0,sizeof(a));
}
};
node cf(node x,node y)
{
node ans;ans.xcnt=x.xcnt;ans.ycnt=y.ycnt;
for(int i=1;i<=ans.xcnt;i++)
for(int j=1;j<=ans.ycnt;j++)
for(int k=1;k<=x.ycnt;k++)
ans.a[i][j]=(ans.a[i][j]+x.a[i][k]*y.a[k][j]GETMOD)GETMOD;
return ans;
}
int main()
{
int n;LL m;node pre;
scanf("%d%lld",&n,&m);pre.xcnt=n;pre.ycnt=n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%lld",&pre.a[i][j]);
node ans;ans.xcnt=n;ans.ycnt=n;
for(int i=1;i<=n;i++)
ans.a[i][i]=1;
while(m>0)
{
if(m%2==1)ans=cf(pre,ans);
pre=cf(pre,pre);
m/=2;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
printf("%lld ",ans.a[i][j]);
printf("\n");
}
return 0;
}