洛谷 P3390
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190613154159753.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzgyMDYzMQ==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190613154225636.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzgyMDYzMQ==,size_16,color_FFFFFF,t_70)
#include<bits/stdc++.h>
using namespace std;
#define maxn 300005
#define ll long long
const int mod = 1e9+7;
int n;
ll k;
struct Mat{
ll m[105][105];
Mat(){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
m[j][i]=0;
}
inline void build(){
for(int i=1;i<=n;i++)m[i][i]=1;
}
}a;
Mat Mul(Mat x,Mat y)
{
Mat c;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
c.m[i][j]=(c.m[i][j]%mod+x.m[i][k]*y.m[k][j]%mod)%mod;
return c;
}
Mat poww(Mat x,ll y){
Mat ans;ans.build();
while(y){
if(y&1)ans=Mul(ans,x);
x=Mul(x,x);
y>>=1;
}
return ans;
}
int main()
{
scanf("%d %lld",&n,&k);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%lld",&a.m[j][i]);
Mat sum=poww(a,k);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
printf("%lld ",sum.m[j][i]);
printf("
");
}
return 0;
}
POJ 3070
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 300005
#define ll long long
const int mod = 10000;
ll n;
struct Mat{
ll m[3][3];
Mat(){
memset(m,0,sizeof(m));
}
inline void build(){
for(int i=1;i<=2;i++)m[i][i]=1;
}
}a;
Mat Mul(Mat x,Mat y){
Mat c;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
for(int k=1;k<=2;k++)
c.m[i][j]=(c.m[i][j]+x.m[i][k]*y.m[k][j]%mod)%mod;
return c;
}
Mat poww(Mat x,ll y){
Mat aa;aa.build();
while(y){
if(y&1)aa=Mul(aa,x);
x=Mul(x,x);
y>>=1;
}
return aa;
}
int main()
{
while(~scanf("%lld",&n)){
if(n==-1)break;
if(n==0)printf("0
");
else if(n==1||n==2)printf("1
");
else{
a.m[1][1]=1; //构建矩阵
a.m[2][1]=1;
a.m[1][2]=1;
a.m[2][2]=0;
Mat ans=poww(a,n-1);
printf("%lld
",ans.m[1][1]);
}
}
return 0;
}