14609 - Matrix Operation II   

Description

In this problem, you are asked to implement a class Matrix that represents a \(N\times N\) matrix.

Given two matrix, \(A\) and \(B\) as follows:

\(\mathbf {A} ={\begin{pmatrix}a_{11}&a_{12}&\cdots &a_{1n}\\a_{21}&a_{22}&\cdots &a_{2n}\\\vdots &\vdots &\ddots &\vdots \\a_{m1}&a_{m2}&\cdots &a_{mn}\\\end{pmatrix}},\quad \mathbf {B} ={\begin{pmatrix}b_{11}&b_{12}&\cdots &b_{1p}\\b_{21}&b_{22}&\cdots &b_{2p}\\\vdots &\vdots &\ddots &\vdots \\b_{n1}&b_{n2}&\cdots &b_{np}\\\end{pmatrix}}\)

The class should support the following operations:

  1. add A = A + B\(A = A+B\)
    \(A + B = {\begin{pmatrix}a_{11}+b_{11}&a_{12}+b_{12}&\cdots &a_{1n}+b_{1n}\\a_{21}+b_{21}&a_{22}+b_{22}&\cdots &a_{2n}+b_{2n}\\\vdots &\vdots &\ddots &\vdots \\a_{m1}+b_{m1}&a_{m2}+b_{m2}&\cdots &a_{mn}+b_{mn}\\\end{pmatrix}}\)
  2. subtract A = A - B\(A = A-B\)
    \(A - B = {\begin{pmatrix}a_{11}-b_{11}&a_{12}-b_{12}&\cdots &a_{1n}-b_{1n}\\a_{21}-b_{21}&a_{22}-b_{22}&\cdots &a_{2n}-b_{2n}\\\vdots &\vdots &\ddots &\vdots \\a_{m1}-b_{m1}&a_{m2}-b_{m2}&\cdots &a_{mn}-b_{mn}\\\end{pmatrix}}\)
  3. multiply A = A * B\(A = AB\)
    For \(C = AB\), \(C = {\begin{pmatrix}c_{11}&c_{12}&\cdots &c_{1p}\\c_{21}&c_{22}&\cdots &c_{2p}\\\vdots &\vdots &\ddots &\vdots \\c_{m1}&c_{m2}&\cdots &c_{mp}\\\end{pmatrix}},\ c_{ij}=a_{i1}b_{1j}+a_{i2}b_{2j}+\cdots +a_{in}b_{nj}=\sum _{k=1}^{n}a_{ik}b_{kj}\)
  4. transpose A = !A\(A = A^{\top}\)
    \(A^{\top} = {\begin{pmatrix}a_{11}&a_{21}&\cdots &a_{m1}\\a_{12}&a_{22}&\cdots &a_{m2}\\\vdots &\vdots &\ddots &\vdots \\a_{1n}&a_{2n}&\cdots &a_{mn}\\\end{pmatrix}}\)

Input

The first line contains two integers $N$, $T$, representing the size of the matrix and the number of the operations.

Following are $N$ lines, each line contains $N$ integers, representing the element in the starting matrix.

Following are $T$ operations, each operation start with an interger $o$, representings the type of the operation.

For operation type 1, 2, 3, interger $o$ is followed by $N\times N$ numbers, representing the element in the operand matrix.


Constraints

$1 \leq N \leq 10$

$1 \leq T \leq 1000$

$o = {1,2,3,4}$

All numbers would not exceed the range of long long.

Subtask

  1. (Testcases 1, 2) Only add, subtract.
  2. (Testcases 3, 4) Only multiply.
  3. (Testcases 5, 6) Only transpose.
  4. (Testcases 7, 8) No additional constraints.

Output

Output the final result of the matrix.

Sample Input  Download

Sample Output  Download

Partial Judge Code

14609.cpp

Partial Judge Header

14609.h

Tags




Discuss