#K45437. Matrix Multiplication
Matrix Multiplication
Matrix Multiplication
You are given two matrices A and B. Your task is to compute their product, if the multiplication is defined. For matrices, multiplication is defined only if the number of columns in A is equal to the number of rows in B.
If A is an m × n matrix and B is an n × p matrix, then their product C is an m × p matrix calculated by:
$$ C_{ij} = \sum_{k=1}^{n} A_{ik} \times B_{kj} $$
If the matrices cannot be multiplied, output an empty list represented as []
.
inputFormat
The input is given from standard input (stdin
) with the following format:
- The first line contains two integers m and n representing the number of rows and columns of matrix A.
- The next m lines each contain n space-separated integers, representing the rows of matrix A.
- The following line contains two integers n and p representing the number of rows and columns of matrix B.
- The next n lines each contain p space-separated integers, representing the rows of matrix B.
outputFormat
If matrix multiplication is possible, output the resulting matrix to standard output (stdout
) as m lines, each containing p space-separated integers. If multiplication is not possible, output []
(without quotes).
2 3
1 2 3
4 5 6
3 2
7 8
9 10
11 12
58 64
139 154
</p>