Basic exercises - matrix multiplication


title: basic training matrix multiplication
categories:

  • ACM
  • Matrix multiplication
    tags:
  • Under magical mark and
    date: 2020-03-14 16:27:26

1 is initialized integer multiplication, multiplication matrix is ​​initialized the same unit matrix size (only diagonal are all 1). It must be kept by the third variable results. for further control loop to control not only the number of conversion product and each multiplicand.

problem

Basic training matrix multiplication questions

By submitting this question

Resource constraints

Time limit: 1.0s Memory Limit: 512.0MB

Problem Description

Given an N-th order matrices A, A, M output power (M is a non-negative integer)
  , for example:
  A =
  . 1 2
  . 3. 4
  2 A of power
  . 7 10
  15 22 is

Input Format

The first line is a positive integer N, M (1 <= N <= 30, 0 <= M <= 5), and represents a power of the order of the matrix A requires
  the following N lines of the absolute value of the N no more than 10 non-negative integer, the value of matrix a described

Output Format

Total N output lines of N integers, M represents the power of the matrix A corresponding to the. Separated by a space between adjacent numbers

Sample input

2 2
1 2
3 4

Sample Output

7 10
15 22

algorithm

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;

int main(){	
//freopen("input.txt", "r", stdin);
	int juzhen[30][30],jieguo[2][30][30],nn=0;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>juzhen[i][j];
			jieguo[0][i][j]=i==j?1:0;
		}
	}
	for(int i=0;i<m;i++)
	{
		nn=i&1?0:1;
		for(int j=0;j<n;j++)
		{
			for(int k=0;k<n;k++)
			{
				int sum=0;
				for(int l=0;l<n;l++)
				{
						sum+=jieguo[1-nn][j][l]*juzhen[l][k];
				}
				jieguo[nn][j][k]=sum;
			}
		}	
	}
		for(int j=0;j<n;j++)
		{
			for(int k=0;k<n;k++)
				cout<<jieguo[nn][j][k]<<" ";
				cout<<endl;
			}
}
Published 43 original articles · won praise 1 · views 918

Guess you like

Origin blog.csdn.net/qq_43985303/article/details/104865563