using
System;
class
GFG{
static
void
power(
int
[,] I,
int
[,] a,
int
rows,
int
cols)
{
int
[,] res =
new
int
[rows, cols];
for
(
int
i = 0; i < rows; ++i)
{
for
(
int
j = 0; j < cols; ++j)
{
for
(
int
k = 0; k < rows; ++k)
{
res[i, j] += a[i, k] * I[k, j];
}
}
}
for
(
int
i = 0; i < rows; ++i)
{
for
(
int
j = 0; j < cols; ++j)
{
I[i, j] = res[i, j];
}
}
}
static
void
print(
int
[, ] a)
{
for
(
int
i = 0; i < a.GetLength(0); ++i)
{
for
(
int
j = 0; j < a.GetLength(1); ++j)
{
Console.Write(a[i, j] +
" "
);
}
Console.WriteLine();
}
}
public
static
void
multiply(
int
[, ] arr,
int
N)
{
int
[, ] I =
new
int
[arr.GetLength(0),
arr.GetLength(1)];
for
(
int
i = 0; i < arr.GetLength(0); ++i)
{
for
(
int
j = 0; j < arr.GetLength(1); ++j)
{
if
(i == j)
{
I[i, j] = 1;
}
else
{
I[i, j] = 0;
}
}
}
for
(
int
i = 1; i <= N; ++i)
{
power(I, arr, arr.GetLength(0),
arr.GetLength(1));
}
for
(
int
i = 0; i < arr.GetLength(0); ++i)
{
for
(
int
j = 0; j < arr.GetLength(1); ++j)
{
arr[i, j] = I[i, j];
}
}
print(arr);
}
public
static
void
Main()
{
int
[, ] arr = { { 1, 2, 3 },
{ 3, 4, 5 },
{ 6, 7, 9 } };
int
N = 2;
multiply(arr, N);
}
}