#K62317. Matrix Multiplication

    ID: 31505 Type: Default 1000ms 256MiB

Matrix Multiplication

Matrix Multiplication

Given two matrices A and B, perform the matrix multiplication if possible. The multiplication is defined as $$C_{ij} = \sum_{k=1}^{n} A_{ik} \times B_{kj}$$ where n is the number of columns in A (and the number of rows in B). If A and B cannot be multiplied (i.e. if the number of columns in A is not equal to the number of rows in B), output an empty matrix represented as []

The input provides the dimensions and elements for matrix A followed by those for matrix B. Your task is to compute and output the product matrix if multiplication is valid, or output [] otherwise.

inputFormat

Input is read from standard input (stdin). The input format is as follows:

1. The first line contains two integers rA and cA, representing the number of rows and columns of matrix A.
2. The next rA lines each contain cA space-separated integers, the elements of matrix A.
3. The following line contains two integers rB and cB, representing the number of rows and columns of matrix B.
4. The next rB lines each contain cB space-separated integers, the elements of matrix B.

outputFormat

Output is written to standard output (stdout). If the matrices can be multiplied (i.e. if cA equals rB), output the resulting matrix with each row on a new line and elements separated by a single space. If multiplication is not feasible, output a single line containing [] (without quotes).## sample

2 3
1 2 3
4 5 6
3 2
7 8
9 10
11 12
58 64

139 154

</p>