import java.util.*;
import java.lang.*;
import java.io.*;
class Matrix {
int n, m;
int[][] mainMatrix;
public Matrix(int n, int m)
{
this.n = n;
this.m = m;
this.mainMatrix = new int[this.n][this.m];
}
public Matrix(int [][] paramMatrix)
{
this.n = paramMatrix.length;
this.m = paramMatrix[0].length;
this.mainMatrix = paramMatrix;
}
public int getElement(int n, int m)
{
return this.mainMatrix[n][m];
}
public void setElement(int n, int m, int value)
{
this.mainMatrix[n][m] = value;
}
public int getVerticalLength()
{
return this.mainMatrix.length;
}
public int getHorizontalLength()
{
return this.mainMatrix[0].length;
}
public void fillRandomValues()
{
for(int i = 0; i < this.n; i++)
{
for(int j = 0; j < this.m; j++)
{
this.mainMatrix[i][j] = rand.nextInt(100);
}
}
}
public void displayMatrix()
{
for(int i = 0; i < this.n; i++)
{
for(int j = 0; j < this.m; j++)
{
System.
out.
print(this.
mainMatrix[i
][j
] + " "); }
}
}
public static int[][] transpone(int[][] paramMatrix)
{
int[][] tmpMatrix = new int[paramMatrix[0].length][paramMatrix.length];
for(int i = 0; i < paramMatrix[0].length; i++)
{
for(int j = 0; j < paramMatrix.length; j++)
{
tmpMatrix[i][j] = paramMatrix[j][i];
}
}
return tmpMatrix;
}
public static Matrix transpone(Matrix paramMatrix)
{
Matrix tmpMatrix = new Matrix(paramMatrix.m, paramMatrix.n);
for(int i = 0; i < paramMatrix.m; i++)
{
for(int j = 0; j < paramMatrix.n; j++)
{
tmpMatrix.setElement(i, j, paramMatrix.getElement(j, i));
}
}
return tmpMatrix;
}
public static Matrix add(Matrix first, Matrix second) throws NotEqualLengthsOfMatrixException {
if(first.getVerticalLength() != second.getVerticalLength() ||
first.getHorizontalLength() != second.getHorizontalLength())
{
throw new NotEqualLengthsOfMatrixException();
}
else {
Matrix tmpMatrix = new Matrix(first.getVerticalLength(), second.getHorizontalLength());
for (int i = 0; i < tmpMatrix.getHorizontalLength(); i++) {
for(int j = 0; j < tmpMatrix.getVerticalLength(); j++){
tmpMatrix.setElement(i, j, first.getElement(i, j) + second.getElement(i, j));
}
}
return tmpMatrix;
}
}
public static Matrix subtract (Matrix first, Matrix second) throws NotEqualLengthsOfMatrixException {
if(first.getVerticalLength() != second.getVerticalLength() ||
first.getHorizontalLength() != second.getHorizontalLength())
throw new NotEqualLengthsOfMatrixException();
else {
Matrix tmpMatrix = new Matrix(first.getVerticalLength(), second.getHorizontalLength());
for (int i = 0; i < tmpMatrix.getHorizontalLength(); i++) {
for(int j = 0; j < tmpMatrix.getVerticalLength(); j++){
tmpMatrix.setElement(i, j, first.getElement(i, j) - second.getElement(i, j));
}
}
return tmpMatrix;
}
}
public static Matrix multiply (Matrix first, Matrix second) throws NotEqualLengthsOfMatrixException {
if(first.getHorizontalLength() != second.getVerticalLength())
throw new NotEqualLengthsOfMatrixException();
else {
Matrix tmpMatrix;
int n = first.getVerticalLength();
int m = second.getHorizontalLength();
int o = second.getVerticalLength();
int[][] tmpArr = new int[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
for (int k = 0; k < o; k++) {
tmpArr[i][j] += first.getElement(i, k) * second.getElement(k, j);
}
}
}
tmpMatrix = new Matrix(tmpArr);
return tmpMatrix;
}
}
}
class NotEqualLengthsOfMatrixException
extends Exception {}
class MatrixMain {
{
//int[][] A = {{-1, 1, -1}, {1, -1, 1}, {-1, 1, -1}};
//int[][] B = {{1, -1, 1}, {-1, 1, -1}, {1, -1, 1}};
int[][] A = {{33,34,12},
{33,19,10},
{12,14,17},
{84,24,51},
{43,71,21}};
int[][] B = {{10,11,34,55},
{33,45,17,81},
{45,63,12,16}};
Matrix x = new Matrix(A);
Matrix y = new Matrix(B);
x.displayMatrix();
y.displayMatrix();
Matrix mM = Matrix.multiply(x, y);
mM.displayMatrix();
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBNYXRyaXggewoKICAgIGludCBuLCBtOwogICAgaW50W11bXSBtYWluTWF0cml4OwoKICAgIHB1YmxpYyBNYXRyaXgoaW50IG4sIGludCBtKQogICAgewogICAgICAgIHRoaXMubiA9IG47CiAgICAgICAgdGhpcy5tID0gbTsKICAgICAgICB0aGlzLm1haW5NYXRyaXggPSBuZXcgaW50W3RoaXMubl1bdGhpcy5tXTsKICAgIH0KCiAgICBwdWJsaWMgTWF0cml4KGludCBbXVtdIHBhcmFtTWF0cml4KQogICAgewogICAgICAgIHRoaXMubiA9IHBhcmFtTWF0cml4Lmxlbmd0aDsKICAgICAgICB0aGlzLm0gPSBwYXJhbU1hdHJpeFswXS5sZW5ndGg7CiAgICAgICAgdGhpcy5tYWluTWF0cml4ID0gcGFyYW1NYXRyaXg7CiAgICB9CgogICAgcHVibGljIGludCBnZXRFbGVtZW50KGludCBuLCBpbnQgbSkKICAgIHsKICAgICAgICByZXR1cm4gdGhpcy5tYWluTWF0cml4W25dW21dOwogICAgfQoKICAgIHB1YmxpYyB2b2lkIHNldEVsZW1lbnQoaW50IG4sIGludCBtLCBpbnQgdmFsdWUpCiAgICB7CiAgICAgICAgdGhpcy5tYWluTWF0cml4W25dW21dID0gdmFsdWU7CiAgICB9CgogICAgcHVibGljIGludCBnZXRWZXJ0aWNhbExlbmd0aCgpCiAgICB7CiAgICAgICAgcmV0dXJuIHRoaXMubWFpbk1hdHJpeC5sZW5ndGg7CiAgICB9CgogICAgcHVibGljIGludCBnZXRIb3Jpem9udGFsTGVuZ3RoKCkKICAgIHsKICAgICAgICByZXR1cm4gdGhpcy5tYWluTWF0cml4WzBdLmxlbmd0aDsKICAgIH0KCiAgICBwdWJsaWMgdm9pZCBmaWxsUmFuZG9tVmFsdWVzKCkKICAgIHsKICAgICAgICBSYW5kb20gcmFuZCA9IG5ldyBSYW5kb20oKTsKCiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IHRoaXMubjsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IHRoaXMubTsgaisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0aGlzLm1haW5NYXRyaXhbaV1bal0gPSByYW5kLm5leHRJbnQoMTAwKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgdm9pZCBkaXNwbGF5TWF0cml4KCkKICAgIHsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgdGhpcy5uOyBpKyspCiAgICAgICAgewogICAgICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgdGhpcy5tOyBqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQodGhpcy5tYWluTWF0cml4W2ldW2pdICsgIiAiKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIHN0YXRpYyBpbnRbXVtdIHRyYW5zcG9uZShpbnRbXVtdIHBhcmFtTWF0cml4KQogICAgewogICAgICAgIGludFtdW10gdG1wTWF0cml4ID0gbmV3IGludFtwYXJhbU1hdHJpeFswXS5sZW5ndGhdW3BhcmFtTWF0cml4Lmxlbmd0aF07CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IHBhcmFtTWF0cml4WzBdLmxlbmd0aDsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IHBhcmFtTWF0cml4Lmxlbmd0aDsgaisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0bXBNYXRyaXhbaV1bal0gPSBwYXJhbU1hdHJpeFtqXVtpXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdG1wTWF0cml4OwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgTWF0cml4IHRyYW5zcG9uZShNYXRyaXggcGFyYW1NYXRyaXgpCiAgICB7CiAgICAgICAgTWF0cml4IHRtcE1hdHJpeCA9IG5ldyBNYXRyaXgocGFyYW1NYXRyaXgubSwgcGFyYW1NYXRyaXgubik7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IHBhcmFtTWF0cml4Lm07IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBwYXJhbU1hdHJpeC5uOyBqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHRtcE1hdHJpeC5zZXRFbGVtZW50KGksIGosIHBhcmFtTWF0cml4LmdldEVsZW1lbnQoaiwgaSkpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiB0bXBNYXRyaXg7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyBNYXRyaXggYWRkKE1hdHJpeCBmaXJzdCwgTWF0cml4IHNlY29uZCkgdGhyb3dzIE5vdEVxdWFsTGVuZ3Roc09mTWF0cml4RXhjZXB0aW9uIHsKICAgICAgICBpZihmaXJzdC5nZXRWZXJ0aWNhbExlbmd0aCgpICE9IHNlY29uZC5nZXRWZXJ0aWNhbExlbmd0aCgpIHx8CiAgICAgICAgICAgICAgICBmaXJzdC5nZXRIb3Jpem9udGFsTGVuZ3RoKCkgIT0gc2Vjb25kLmdldEhvcml6b250YWxMZW5ndGgoKSkKICAgICAgICB7CiAgICAgICAgICAgIHRocm93ICBuZXcgTm90RXF1YWxMZW5ndGhzT2ZNYXRyaXhFeGNlcHRpb24oKTsKICAgICAgICB9CiAgICAgICAgZWxzZSB7CiAgICAgICAgICAgIE1hdHJpeCB0bXBNYXRyaXggPSBuZXcgTWF0cml4KGZpcnN0LmdldFZlcnRpY2FsTGVuZ3RoKCksIHNlY29uZC5nZXRIb3Jpem9udGFsTGVuZ3RoKCkpOwogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHRtcE1hdHJpeC5nZXRIb3Jpem9udGFsTGVuZ3RoKCk7IGkrKykgewogICAgICAgICAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IHRtcE1hdHJpeC5nZXRWZXJ0aWNhbExlbmd0aCgpOyBqKyspew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OwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiB0bXBNYXRyaXg7CiAgICAgICAgfQogICAgfQogICAgcHVibGljIHN0YXRpYyBNYXRyaXggbXVsdGlwbHkgKE1hdHJpeCBmaXJzdCwgTWF0cml4IHNlY29uZCkgdGhyb3dzIE5vdEVxdWFsTGVuZ3Roc09mTWF0cml4RXhjZXB0aW9uIHsKICAgICAgICBpZihmaXJzdC5nZXRIb3Jpem9udGFsTGVuZ3RoKCkgIT0gc2Vjb25kLmdldFZlcnRpY2FsTGVuZ3RoKCkpCiAgICAgICAgICAgIHRocm93ICBuZXcgTm90RXF1YWxMZW5ndGhzT2ZNYXRyaXhFeGNlcHRpb24oKTsKICAgICAgICBlbHNlIHsKICAgICAgICAgICAgTWF0cml4IHRtcE1hdHJpeDsKICAgICAgICAgICAgaW50IG4gPSBmaXJzdC5nZXRWZXJ0aWNhbExlbmd0aCgpOwogICAgICAgICAgICBpbnQgbSA9IHNlY29uZC5nZXRIb3Jpem9udGFsTGVuZ3RoKCk7CiAgICAgICAgICAgIGludCBvID0gc2Vjb25kLmdldFZlcnRpY2FsTGVuZ3RoKCk7CiAgICAgICAgICAgIGludFtdW10gdG1wQXJyID0gbmV3IGludFtuXVttXTsKICAgICAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgbTsgaisrKXsKICAgICAgICAgICAgICAgICAgICBmb3IgKGludCBrID0gMDsgayA8IG87IGsrKykgewogICAgICAgICAgICAgICAgICAgICAgICB0bXBBcnJbaV1bal0gKz0gZmlyc3QuZ2V0RWxlbWVudChpLCBrKSAqIHNlY29uZC5nZXRFbGVtZW50KGssIGopOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICB0bXBNYXRyaXggPSBuZXcgTWF0cml4KHRtcEFycik7CiAgICAgICAgICAgIHJldHVybiB0bXBNYXRyaXg7CiAgICAgICAgfQogICAgfQp9CgoKY2xhc3MgTm90RXF1YWxMZW5ndGhzT2ZNYXRyaXhFeGNlcHRpb24gZXh0ZW5kcyBFeGNlcHRpb24ge30KCgpjbGFzcyBNYXRyaXhNYWluIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgogICAgewogICAgICAgIC8vaW50W11bXSBBID0ge3stMSwgMSwgLTF9LCB7MSwgLTEsIDF9LCB7LTEsIDEsIC0xfX07CiAgICAgICAgLy9pbnRbXVtdIEIgPSB7ezEsIC0xLCAxfSwgey0xLCAxLCAtMX0sIHsxLCAtMSwgMX19OwoKICAgICAgICBpbnRbXVtdIEEgPSAgIHt7MzMsMzQsMTJ9LAogICAgICAgICAgICAgICAgICAgICAgIHszMywxOSwxMH0sCiAgICAgICAgICAgICAgICAgICAgICAgezEyLDE0LDE3fSwKICAgICAgICAgICAgICAgICAgICAgICB7ODQsMjQsNTF9LAogICAgICAgICAgICAgICAgICAgICAgIHs0Myw3MSwyMX19OwoKICAgICAgICBpbnRbXVtdIEIgPSAge3sxMCwxMSwzNCw1NX0sCiAgICAgICAgICAgICAgICAgICAgICB7MzMsNDUsMTcsODF9LAogICAgICAgICAgICAgICAgICAgICAgezQ1LDYzLDEyLDE2fX07CgoKCgogICAgICAgIE1hdHJpeCB4ID0gbmV3IE1hdHJpeChBKTsKICAgICAgICBNYXRyaXggeSA9IG5ldyBNYXRyaXgoQik7CiAgICAgICAgeC5kaXNwbGF5TWF0cml4KCk7CiAgICAgICAgeS5kaXNwbGF5TWF0cml4KCk7CgogICAgICAgIE1hdHJpeCBtTSA9IE1hdHJpeC5tdWx0aXBseSh4LCB5KTsKCiAgICAgICAgbU0uZGlzcGxheU1hdHJpeCgpOwogICAgICAgIAogICAgfQp9