代码:
#include <cstdio>
#include <cstring>
#define MOD 1000000007
#define LL long long
#define N 10
struct node
{
int mat[N][N];
};
int n;
node xx;
node mul(node a, node b)
{
node c;
memset(c.mat, 0, sizeof(c.mat));
for(int i= 0; i< n; i++)
for(int j= 0; j< n; j++)
for(int k= 0; k< n; k++)
c.mat[i][j]+= a.mat[i][k]* b.mat[k][j];
return c;
}
node fun(node a, int b)
{
node ans= xx;
node t= a;
while(b)
{
if(b&1)
ans=mul(ans, t);
t= mul(t, t);
b/= 2;
}
return ans;
}
int main()
{
memset(xx.mat, 0, sizeof(xx.mat));
for(int i= 0; i< N; i++)
xx.mat[i][i]= 1;
node a;
int b;
while(scanf("%d %d",&n,&b)!=EOF)
{
for(int i= 0; i< n; i++)
for(int j= 0; j< n; j++)
scanf("%d",&a.mat[i][j]);
node ans= fun(a, b);
for(int i= 0; i< n; i++)
{
for(int j= 0; j< n; j++)
printf("%d ",ans.mat[i][j]);
printf("\n");
}
}
return 0;
}
POJ 3070
代码:
#include <cstdio>
#include <cstring>
#define MOD 10000
#define LL long long
#define N 10
struct node
{
int mat[N][N];
};
int n;
node xx;
node mul(node a, node b)
{
node c;
memset(c.mat, 0, sizeof(c.mat));
for(int i= 0; i< n; i++)
for(int j= 0; j< n; j++)
for(int k= 0; k< n; k++)
c.mat[i][j]= (c.mat[i][j]+ a.mat[i][k]* b.mat[k][j])%MOD;
return c;
}
node fun(node a, int b)
{
node ans= xx;
node t= a;
while(b)
{
if(b&1)
ans=mul(ans, t);
t= mul(t, t);
b/= 2;
}
return ans;
}
int main()
{
memset(xx.mat, 0, sizeof(xx.mat));
for(int i= 0; i< N; i++)
xx.mat[i][i]= 1;
node a;
int b;
a.mat[0][0]= a.mat[0][1]= a.mat[1][0]= 1;
a.mat[1][1]= 0;
n= 2;
while(scanf("%d",&b)!=EOF && b!=-1)
{
if(b== 0)
{
printf("0\n");
continue;
}
if(b== 1 || b== 2)
{
printf("1\n");
continue;
}
node ans= fun(a, b-2);
printf("%d\n",(ans.mat[0][0] + ans.mat[0][1])%MOD);
}
return 0;
}