#C302. Matrix Multiplication

    ID: 46401 Type: Default 1000ms 256MiB

Matrix Multiplication

Matrix Multiplication

You are given two matrices A and B. Matrix A has dimensions \(m \times n\) and matrix B has dimensions \(n \times p\). Your task is to compute the product matrix \(C = A \times B\), which will have dimensions \(m \times p\).

If the dimensions of the matrices are not compatible for multiplication (i.e. the number of columns in A is not equal to the number of rows in B), then output the error message "Incompatible dimensions for matrix multiplication".

The input is given through standard input and the result should be printed to standard output. Each row of the result matrix should be printed on a separate line with its elements separated by a single space.

inputFormat

The input format is as follows:

  • The first line contains two integers \(m\) and \(n\), representing the number of rows and columns in matrix A respectively.
  • The next \(m\) lines each contain \(n\) integers, representing the rows of matrix A.
  • The following line contains two integers \(n'\) and \(p\), where \(n'\) represents the number of rows in matrix B and \(p\) represents the number of columns in matrix B.
  • The next \(n'\) lines each contain \(p\) integers, representing the rows of matrix B.

Note: For a valid multiplication, \(n\) must be equal to \(n'\).

outputFormat

If the matrices are compatible, output the resulting matrix C in \(m\) lines, each containing \(p\) space-separated integers. If the matrices are not compatible, output the exact error message "Incompatible dimensions for matrix multiplication".

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

10 8

</p>