原文:

给定一个正方形mat【】【】和一个整数 n ,任务是将矩阵乘以后。

示例:

输入: mat[][] = {{1,2,3},{3,4,5},{6,7,9}},n = 2 t3】输出:t5】25 31 40 45 57 74 81 103 134

输入: mat[][] = {{1,2},{3,4}},n = 3 t3】输出:t5】37 54 81 118

进场:思路是使用 。即 a = iaa = ai ,其中 an * m 阶次尺寸的矩阵, i 为尺寸 m * n 的恒等式矩阵,其中 n 为总行数, m 为矩阵中的总列数。

其思路是迭代范围【1,n】,用 a.i 更新身份矩阵,这样在计算出a2t9】的值后= a.aa3t15】就可以计算为a . a2t19】等等,直到 a 【t21**

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function for matrix multiplication
void power(vector>& i,
           vector>& a,
           int rows, int cols)
{
    // stores the resultant matrix
    // after multiplying a[][] by i[][]
    vector> res(rows, vector(cols));
    // matrix multiplication
    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];
            }
        }
    }
    // updating identity element
    // of a matrix
    for(int i = 0; i < rows;   i)
    {
        for(int j = 0; j < cols;   j)
        {
            i[i][j] = res[i][j];
        }
    }
}
// function to print the given matrix
void print(vector >& a)
{
    // traverse the row
    for(int i = 0; i < a.size();   i)
    {
        // traverse the column
        for(int j = 0; j < a[0].size();   j)
        {
            cout << a[i][j] << " ";
        }
        cout << "\n";
    }
}
// function to multiply the given
// matrix n times
void multiply(vector >& arr, int n)
{
    // identity element of matrix
    vector> i(arr.size(),
                          vector(arr[0].size()));
    // update the identity matrix
    for(int i = 0; i < arr.size();   i)
    {
        for(int j = 0; j < arr[0].size();   j)
        {
            // for the diagonal element
            if (i == j)
            {
                i[i][j] = 1;
            }
            else
            {
                i[i][j] = 0;
            }
        }
    }
    // multiply the matrix n times
    for(int i = 1; i <= n;   i)
    {
        power(i, arr, arr.size(), arr[0].size());
    }
    // update the matrix arr[i] to
    // to identity matrix
    for(int i = 0; i < arr.size();   i)
    {
        for(int j = 0; j < arr[0].size();   j)
        {
            arr[i][j] = i[i][j];
        }
    }
    // print the matrix
    print(arr);
}
// driver code
int main()
{
    // given 2d array
    vector> arr = { { 1, 2, 3 },
                                { 3, 4, 5 },
                                { 6, 7, 9 } };
    // given n
    int n = 2;
    // function call
    multiply(arr, n);
    return 0;
}
// this code is contributed by akhilsaini

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.io.*;
class gfg {
    // function for matrix multiplication
    static void power(int i[][], int a[][],
                      int rows, int cols)
    {
        // stores the resultant matrix
        // after multiplying a[][] by i[][]
        int res[][] = new int[rows][cols];
        // matrix multiplication
        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];
                }
            }
        }
        // updating identity element
        // of a matrix
        for (int i = 0; i < rows;   i) {
            for (int j = 0; j < cols;   j) {
                i[i][j] = res[i][j];
            }
        }
    }
    // function to print the given matrix
    static void print(int a[][])
    {
        // traverse the row
        for (int i = 0;
             i < a.length;   i) {
            // traverse the column
            for (int j = 0;
                 j < a[0].length;   j) {
                system.out.print(a[i][j]
                                   " ");
            }
            system.out.println();
        }
    }
    // function to multiply the given
    // matrix n times
    public static void multiply(
        int arr[][], int n)
    {
        // identity element of matrix
        int i[][]
            = new int[arr.length][arr[0].length];
        // update the identity matrix
        for (int i = 0; i < arr.length;   i) {
            for (int j = 0;
                 j < arr[0].length;   j) {
                // for the diagonal element
                if (i == j) {
                    i[i][j] = 1;
                }
                else {
                    i[i][j] = 0;
                }
            }
        }
        // multiply the matrix n times
        for (int i = 1; i <= n;   i) {
            power(i, arr, arr.length,
                  arr[0].length);
        }
        // update the matrix arr[i] to
        // to identity matrix
        for (int i = 0;
             i < arr.length;   i) {
            for (int j = 0;
                 j < arr[0].length;   j) {
                arr[i][j] = i[i][j];
            }
        }
        // print the matrix
        print(arr);
    }
    // driver code
    public static void main(string[] args)
    {
        // given 2d array
        int arr[][]
            = { { 1, 2, 3 },
                { 3, 4, 5 },
                { 6, 7, 9 } };
        // given n
        int n = 2;
        // function call
        multiply(arr, n);
    }
}

python 3

# python3 program for the above approach
# function for matrix multiplication
def power(i, a, rows, cols):
    # stores the resultant matrix
    # after multiplying a[][] by i[][]
    res = [[0 for i in range(cols)]
              for j in range(rows)]
    # matrix multiplication
    for i in range(0, rows):
        for j in range(0, cols):
            for k in range(0, rows):
                res[i][j]  = a[i][k] * i[k][j]
    # updating identity element
    # of a matrix
    for i in range(0, rows):
        for j in range(0, cols):
            i[i][j] = res[i][j]
# function to print the given matrix
def prints(a):
    # traverse the row
    for i in range(0, len(a)):
        # traverse the column
        for j in range(0, len(a[0])):
            print(a[i][j], end = ' ')
        print()
# function to multiply the given
# matrix n times
def multiply(arr, n):
    # identity element of matrix
    i = [[1 if i == j else 0 for i in range(
                  len(arr))] for j in range(
                  len(arr[0]))]
    # multiply the matrix n times
    for i in range(1, n   1):
        power(i, arr, len(arr), len(arr[0]))
    # update the matrix arr[i] to
    # to identity matrix
    for i in range(0, len(arr)):
        for j in range(0, len(arr[0])):
            arr[i][j] = i[i][j]
    # print the matrix
    prints(arr)
# driver code
if __name__ == '__main__':
    # given 2d array
    arr = [ [ 1, 2, 3 ],
            [ 3, 4, 5 ],
            [ 6, 7, 9 ] ]
    # given n
    n = 2
    # function call
    multiply(arr, n)
# this code is contributed by akhilsaini

c

// c# program for the above approach
using system;
class gfg{
// function for matrix multiplication
static void power(int[,] i, int[,] a,
                  int rows, int cols)
{
    // stores the resultant matrix
    // after multiplying a[][] by i[][]
    int[,] res = new int[rows, cols];
    // matrix multiplication
    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];
            }
        }
    }
    // updating identity element
    // of a matrix
    for(int i = 0; i < rows;   i)
    {
        for(int j = 0; j < cols;   j)
        {
            i[i, j] = res[i, j];
        }
    }
}
// function to print the given matrix
static void print(int[, ] a)
{
    // traverse the row
    for(int i = 0; i < a.getlength(0);   i)
    {
        // traverse the column
        for(int j = 0; j < a.getlength(1);   j)
        {
            console.write(a[i, j]   " ");
        }
        console.writeline();
    }
}
// function to multiply the given
// matrix n times
public static void multiply(int[, ] arr, int n)
{
    // identity element of matrix
    int[, ] i = new int[arr.getlength(0),
                        arr.getlength(1)];
    // update the identity matrix
    for(int i = 0; i < arr.getlength(0);   i)
    {
        for(int j = 0; j < arr.getlength(1);   j)
        {
            // for the diagonal element
            if (i == j)
            {
                i[i, j] = 1;
            }
            else
            {
                i[i, j] = 0;
            }
        }
    }
    // multiply the matrix n times
    for(int i = 1; i <= n;   i)
    {
        power(i, arr, arr.getlength(0),
                      arr.getlength(1));
    }
    // update the matrix arr[i] to
    // to identity matrix
    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 the matrix
    print(arr);
}
// driver code
public static void main()
{
    // given 2d array
    int[, ] arr = { { 1, 2, 3 },
                    { 3, 4, 5 },
                    { 6, 7, 9 } };
    // given n
    int n = 2;
    // function call
    multiply(arr, n);
}
}
// this code is contributed by akhilsaini

output: 

25 31 40 
45 57 74 
81 103 134

时间复杂度:o(n3) 辅助空间: o(n)