You can try like this
#include "stdio.h"
#include "string.h"
#define N 3
void copy(int A[N][N],int B[N][N])
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[i][j]=B[i][j];
}
void mul(int A[N][N],int B[N][N],int M[N][N])
{
int i,j,k;
for(i=0;i<N;i++)
for(j=0;j<N;j++) {
M[i][j]=0;
for(k=0;k<N;k++)
M[i][j]+=A[i][k]*B[k][j];
}
}
void exp(int M[N][N],int n)
{
int R[N][N];
int A[N][N];
int B[N][N];
int i;
memset(R,0,sizeof(R));
for(i=0;i<N;i++)
R[i][i]=1;
while(n>0) {
if(n%2==1) {
copy(A,R);
mul(M,A,R);
}
n=n>>1;
copy(A,M);
copy(B,M);
mul(A,B,M);
}
copy(M,R);
}
int main()
{
int i,j;
int M[N][N];
int n;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
scanf("%d",&M[i][j]);
scanf("%d",&n);
exp(M,n);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
printf("%d%c",M[i][j],j==N-1?'\n':' ');
return 0;
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.