:heavy_check_mark: test/math/matrix/aoj_itp1_7_d.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/7/ITP1_7_D

#include "src/math/matrix.hpp"

#include <iostream>

using namespace std;

int main() {
	int n, m, l;
	cin >> n >> m >> l;
	Matrix<long long> a(n, m), b(m, l);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j) cin >> a[i][j];
	for (int i = 0; i < m; ++i)
		for (int j = 0; j < l; ++j) cin >> b[i][j];
	auto c = a * b;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < l; ++j) {
			if (j) cout << " ";
			cout << c[i][j];
		}
		cout << endl;
	}

	return 0;
}
#line 1 "test/math/matrix/aoj_itp1_7_d.test.cpp"
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/7/ITP1_7_D

#line 1 "src/math/matrix.hpp"



#include <cassert>
#include <cstddef>
#include <initializer_list>
#include <vector>

template<class T>
class Matrix {
private:
	std::vector<std::vector<T>> mat;

public:
	Matrix(std::size_t n) : mat(n, std::vector<T>(n)) {}

	Matrix(std::size_t n, std::size_t m) : mat(n, std::vector<T>(m)) {}

	Matrix(std::initializer_list<std::vector<T>> init) : mat(init) {}

	Matrix(std::vector<std::vector<T>> &mat) : mat(mat) {}

	[[nodiscard]] std::size_t height() const { return mat.size(); }

	[[nodiscard]] std::size_t width() const { return mat[0].size(); }

	std::vector<T> &operator[](std::size_t k) { return mat[k]; }

	std::vector<T> const &operator[](std::size_t k) const { return mat[k]; }

	Matrix &operator+=(Matrix const &b) {
		std::size_t n = height(), m = width();
		assert(n == b.height() && m == b.width());
		for (std::size_t i = 0; i < n; ++i)
			for (std::size_t j = 0; j < m; ++j) (*this)[i][j] += b[i][j];
		return *this;
	}

	Matrix &operator-=(Matrix const &b) {
		std::size_t n = height(), m = width();
		assert(n == b.height() && m == b.width());
		for (std::size_t i = 0; i < n; ++i)
			for (std::size_t j = 0; j < m; ++j) (*this)[i][j] -= b[i][j];
		return *this;
	}

	Matrix &operator*=(Matrix const &b) {
		std::size_t n = height(), m = b.width(), p = width();
		assert(p == b.height());
		std::vector<std::vector<T>> c(n, std::vector<T>(m));
		for (std::size_t i = 0; i < n; ++i)
			for (std::size_t j = 0; j < m; ++j)
				for (std::size_t k = 0; k < p; ++k) c[i][j] += (*this)[i][k] * b[k][j];
		mat.swap(c);
		return *this;
	}

	Matrix operator+(Matrix const &b) const { return Matrix(*this) += b; }

	Matrix operator-(Matrix const &b) const { return Matrix(*this) -= b; }

	Matrix operator*(Matrix const &b) const { return Matrix(*this) *= b; }

	static Matrix identity(std::size_t n) {
		Matrix id(n);
		for (std::size_t i = 0; i < n; ++i) id[i][i] = 1;
		return id;
	}

	[[nodiscard]] Matrix pow(long long n) const {
		Matrix res = Matrix::identity(height()), tmp(*this);
		while (n > 0) {
			if (n & 1) res *= tmp;
			tmp *= tmp;
			n >>= 1;
		}
		return res;
	}
};


#line 4 "test/math/matrix/aoj_itp1_7_d.test.cpp"

#include <iostream>

using namespace std;

int main() {
	int n, m, l;
	cin >> n >> m >> l;
	Matrix<long long> a(n, m), b(m, l);
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j) cin >> a[i][j];
	for (int i = 0; i < m; ++i)
		for (int j = 0; j < l; ++j) cin >> b[i][j];
	auto c = a * b;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < l; ++j) {
			if (j) cout << " ";
			cout << c[i][j];
		}
		cout << endl;
	}

	return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ 00_sample_00.in :heavy_check_mark: AC 50 ms 7 MB
g++ 01_small_00.in :heavy_check_mark: AC 13 ms 7 MB
g++ 01_small_01.in :heavy_check_mark: AC 12 ms 7 MB
g++ 01_small_02.in :heavy_check_mark: AC 12 ms 7 MB
g++ 01_small_03.in :heavy_check_mark: AC 12 ms 7 MB
g++ 02_corner_00.in :heavy_check_mark: AC 12 ms 7 MB
g++ 02_corner_01.in :heavy_check_mark: AC 12 ms 7 MB
g++ 02_corner_02.in :heavy_check_mark: AC 12 ms 7 MB
g++ 02_corner_03.in :heavy_check_mark: AC 12 ms 7 MB
g++ 02_corner_04.in :heavy_check_mark: AC 12 ms 7 MB
g++ 03_rand_00.in :heavy_check_mark: AC 12 ms 7 MB
g++ 03_rand_01.in :heavy_check_mark: AC 12 ms 7 MB
g++ 03_rand_02.in :heavy_check_mark: AC 13 ms 7 MB
g++ 03_rand_03.in :heavy_check_mark: AC 13 ms 7 MB
g++ 04_large_00.in :heavy_check_mark: AC 13 ms 7 MB
g++ 04_large_01.in :heavy_check_mark: AC 13 ms 7 MB
g++ 05_maximum_00.in :heavy_check_mark: AC 29 ms 7 MB
g++ 05_maximum_01.in :heavy_check_mark: AC 29 ms 7 MB
clang++ 00_sample_00.in :heavy_check_mark: AC 12 ms 7 MB
clang++ 01_small_00.in :heavy_check_mark: AC 12 ms 13 MB
clang++ 01_small_01.in :heavy_check_mark: AC 12 ms 13 MB
clang++ 01_small_02.in :heavy_check_mark: AC 11 ms 9 MB
clang++ 01_small_03.in :heavy_check_mark: AC 12 ms 9 MB
clang++ 02_corner_00.in :heavy_check_mark: AC 12 ms 13 MB
clang++ 02_corner_01.in :heavy_check_mark: AC 11 ms 9 MB
clang++ 02_corner_02.in :heavy_check_mark: AC 12 ms 9 MB
clang++ 02_corner_03.in :heavy_check_mark: AC 11 ms 11 MB
clang++ 02_corner_04.in :heavy_check_mark: AC 12 ms 9 MB
clang++ 03_rand_00.in :heavy_check_mark: AC 12 ms 13 MB
clang++ 03_rand_01.in :heavy_check_mark: AC 11 ms 9 MB
clang++ 03_rand_02.in :heavy_check_mark: AC 12 ms 13 MB
clang++ 03_rand_03.in :heavy_check_mark: AC 13 ms 15 MB
clang++ 04_large_00.in :heavy_check_mark: AC 12 ms 11 MB
clang++ 04_large_01.in :heavy_check_mark: AC 13 ms 13 MB
clang++ 05_maximum_00.in :heavy_check_mark: AC 32 ms 14 MB
clang++ 05_maximum_01.in :heavy_check_mark: AC 33 ms 12 MB
Back to top page