#K66792. Matrix Multiplication for Square Matrices

    ID: 32499 Type: Default 1000ms 256MiB

Matrix Multiplication for Square Matrices

Matrix Multiplication for Square Matrices

You are given two matrices and your task is to multiply these matrices if they are square and of the same dimension. Otherwise, if the matrices do not meet these conditions, output an empty list [].

The matrix multiplication is defined as follows: if \(A\) and \(B\) are two \(n \times n\) matrices, then the element in the \(i\)-th row and \(j\)-th column of the product \(C\) is given by \(C_{ij} = \sum_{k=1}^{n} A_{ik} \times B_{kj}\) in LaTeX format.

The matrices will be input in a specific format as described below.

inputFormat

The input consists of the following:

  • The first line contains two space-separated integers \(r1\) and \(c1\), which represent the number of rows and columns of the first matrix.
  • The next \(r1\) lines each contain \(c1\) space-separated integers representing the rows of the first matrix.
  • The following line contains two space-separated integers \(r2\) and \(c2\), which represent the number of rows and columns of the second matrix.
  • The next \(r2\) lines each contain \(c2\) space-separated integers representing the rows of the second matrix.

Note: For the matrices to be multiplied, both must be square (i.e. \(r1 = c1\) and \(r2 = c2\)) and of the same size (i.e. \(r1 = r2\)).

outputFormat

If both matrices are square and of the same dimension, output their product matrix. Each row of the resulting matrix should be printed on a new line with the elements separated by a single space.

If the matrices cannot be multiplied (i.e. they are incompatible), output a single line containing [] (without quotes).

## sample
2 2
1 2
3 4
2 2
2 0
1 2
4 4

10 8

</p>