14609 - Matrix Operation II
|
Time |
Memory |
Case 1 |
1 sec |
32 MB |
Case 2 |
1 sec |
32 MB |
Case 3 |
1 sec |
32 MB |
Case 4 |
1 sec |
32 MB |
Case 5 |
1 sec |
32 MB |
Case 6 |
1 sec |
32 MB |
Case 7 |
1 sec |
32 MB |
Case 8 |
1 sec |
32 MB |
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:
- 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}}\)
- 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}}\)
- 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}\)
- 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
- (Testcases 1, 2) Only add, subtract.
- (Testcases 3, 4) Only multiply.
- (Testcases 5, 6) Only transpose.
- (Testcases 7, 8) No additional constraints.
Output
Output the final result of the matrix.
Partial Judge Code
14609.cpp
Partial Judge Header
14609.h
Tags