Blue Bridge Cup test questions basic practice matrix multiplication (detailed)

Resource limit

Time limit: 1.0s Memory limit: 512.0MB

Problem Description

  Given an N-order matrix A, output A to the power of M (M is a non-negative integer)
  For example:
  A =
  1 2
  3 4
  A to the power of 2
  7 10
  15 22

Input format

  The first line is a positive integer N, M (1<=N<=30, 0<=M<=5), which represents the order of matrix A and the required power. The
  next N rows, each with N absolute values A non-negative integer not exceeding 10, describing the value of matrix A

Output format

  The output has a total of N rows, and each row has N integers, representing the matrix corresponding to the M power of A. Use a space to separate adjacent numbers

Sample input

2 2
1 2
3 4

Sample output

7 10
15 22

 Ideas:

Matrix product: almost means:

Give a chestnut : when n=2;

The first number is the number of the first horizontal line and the number of the first vertical line, which are correspondingly multiplied and added.

The second number in the same line as the first number is also the corresponding multiplication and addition of the first horizontal line and the second vertical line.

The third number is not on the same line as the first two numbers, because it is the number in the first line that rounds all the vertical rows that can be calculated. So start the calculation of the second horizontal line and the first vertical line, and then calculate the second vertical line. Until this horizontal line is also calculated.

Baidu's picture is like this: (reference)

The order of A , this means:

At level 0: the diagonal is 1. Other positions are 0

At the time of level 1: for itself

At the second level: Just follow the above.

3rd order: 2nd order calculated number and 1st order to calculate the matrix product

4th order: 3rd order result and 1st order calculation

.......

So we understand the general idea, let's start code simulation (*^▽^*)


 Implementation code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
	int i,j,k,z;
	int a[35][35],b[35][35],c[35][35];
	
	cin>>n>>m;
	
	
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			cin>>a[i][j];//输入 
			b[i][j]=a[i][j];
			c[i][j]=0;//初始化 
		}
	}
	
	if(m==0)
	{
		for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
		     if(i==j)
			 b[i][j]=1;
			 else
			 b[i][j]=0; 
		}
	}
	}
	else if(m==1)
	{
		for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			b[i][j]=a[i][j]; 
		}
	}
	}
	else if(m>=2)
	{
		for(z=2;z<=m;z++)
		{
			for(i=1;i<=n;i++)//横行 
	        {
		        for(j=1;j<=n;j++)//竖行 
		        {
			        for(k=1;k<=n;k++)//第几个数 
			        {
				        c[i][j]+=a[i][k]*b[k][j];
		            }
		        }
	        }
	        
	        for(i=1;i<=n;i++)
	        {
	        	for(j=1;j<=n;j++)
	        	{
	        		b[i][j]=c[i][j];
	        		c[i][j]=0;
	        		
				}
			}
		}

	}
	
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			printf("%d ",b[i][j]);
		}
		printf("\n");
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/with_wine/article/details/115279621