a01sa01to's competitive programming library.
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
#include "../../../cpp/library/data-structure/matrix.hpp"
#define PROBLEM "https://atcoder.jp/contests/abc237/tasks/abc237_b"
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int h, w;
cin >> h >> w;
asalib::matrix::Matrix<int> a(h, w);
rep(i, h) rep(j, w) cin >> a.at(i, j);
auto t = a.transpose();
rep(i, w) {
rep(j, h) cout << t.at(i, j) << " \n"[j == h - 1];
}
return 0;
}
#line 1 "tests/matrix/core/abc237-b.test.cpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
#line 2 "library/data-structure/matrix.hpp"
#line 4 "library/data-structure/matrix.hpp"
using namespace std;
#line 2 "library/_internal/types.hpp"
#include <concepts>
#include <type_traits>
using namespace std;
#line 2 "library/_internal/modint-base.hpp"
#line 5 "library/_internal/modint-base.hpp"
using namespace std;
namespace asalib {
namespace _internal {
class modint_base {};
template<typename T>
concept is_modint = is_base_of_v<modint_base, T>;
} // namespace _internal
} // namespace asalib
#line 8 "library/_internal/types.hpp"
namespace asalib {
namespace _internal {
// ---------- concept definition ---------- //
template<class T>
concept integral_like = integral<T> || is_modint<T>;
template<class T>
concept floating_like = floating_point<T>;
template<class T>
concept numeric_like = integral_like<T> || floating_like<T>;
// ---------- function definition ---------- //
template<class T>
T plus(T a, T b) { return a + b; }
template<class T>
T minus(T a, T b) { return a - b; }
// ---------- constant definition ---------- //
template<class T>
T zero() { return 0; }
} // namespace _internal
} // namespace asalib
#line 7 "library/data-structure/matrix.hpp"
namespace asalib {
namespace matrix {
template<_internal::numeric_like T>
class Matrix {
public:
constexpr Matrix(): _n_row(0), _n_col(0) {};
constexpr Matrix(size_t n_row, size_t n_col): _n_row(n_row), _n_col(n_col), _data(n_row * n_col) {};
constexpr Matrix(size_t n_row, size_t n_col, T x): _n_row(n_row), _n_col(n_col), _data(n_row * n_col, x) {};
// constexpr T& operator[](size_t i, size_t j) { return _data[i * _n_col + j]; }
// constexpr const T& operator[](size_t i, size_t j) const { return _data[i * _n_col + j]; }
// 使えないっぽいので at で代用
constexpr inline T& at(size_t i, size_t j) { return _data[i * _n_col + j]; }
constexpr T at(size_t i, size_t j) const { return _data[i * _n_col + j]; }
constexpr valarray<T> row(size_t i) const { return valarray<T>(_data[slice(i * _n_col, _n_col, 1)]); }
constexpr valarray<T> col(size_t j) const { return valarray<T>(_data[slice(j, _n_row, _n_col)]); }
constexpr Matrix operator+=(const T& x) {
_data += x;
return *this;
}
constexpr Matrix operator-=(const T& x) {
_data -= x;
return *this;
}
constexpr Matrix operator*=(const T& x) {
_data *= x;
return *this;
}
constexpr Matrix operator/=(const T& x) {
_data /= x;
return *this;
}
constexpr Matrix operator%=(const T& x) {
_data %= x;
return *this;
}
constexpr Matrix operator+(const T& x) const { return Matrix(*this) += x; }
constexpr Matrix operator-(const T& x) const { return Matrix(*this) -= x; }
constexpr Matrix operator*(const T& x) const { return Matrix(*this) *= x; }
constexpr Matrix operator/(const T& x) const { return Matrix(*this) /= x; }
constexpr Matrix operator%(const T& x) const { return Matrix(*this) %= x; }
constexpr Matrix operator+=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data += x._data;
return *this;
}
constexpr Matrix operator-=(const Matrix& x) {
assert(_n_row == x._n_row);
assert(_n_col == x._n_col);
_data -= x._data;
return *this;
}
constexpr Matrix operator*=(const Matrix& x) {
assert(_n_col == x._n_row);
Matrix res(_n_row, x._n_col);
for (size_t i = 0; i < _n_row; ++i) {
for (size_t j = 0; j < x._n_col; ++j) {
res.at(i, j) = (this->_data[slice(i * _n_col, _n_col, 1)] * x._data[slice(j, x._n_row, x._n_col)]).sum();
}
}
return *this = res;
}
constexpr Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
constexpr Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
constexpr Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }
constexpr bool operator==(const Matrix& x) const { return _n_row == x._n_row && _n_col == x._n_col && _data == x._data; }
constexpr bool operator!=(const Matrix& x) const { return !(*this == x); }
constexpr bool operator<(const Matrix& x) const { return _data < x._data; }
constexpr const Matrix transpose() const {
Matrix res(_n_col, _n_row);
for (size_t i = 0; i < _n_row; ++i) res._data[slice(i, _n_col, _n_row)] = _data[slice(i * _n_col, _n_col, 1)];
return res;
}
template<integral U>
constexpr Matrix pow(U x) {
assert(_n_row == _n_col);
Matrix res = I(_n_row);
Matrix a(*this);
while (x) {
if (x & 1) res *= a;
a *= a;
x >>= 1;
}
return res;
}
constexpr static Matrix I(size_t n) {
Matrix res(n, n);
res._data[slice(0, n, n + 1)] = 1;
return res;
}
constexpr size_t n_row() const { return _n_row; }
constexpr size_t n_col() const { return _n_col; }
private:
size_t _n_row, _n_col;
valarray<T> _data;
public:
// ---------- prototype ---------- //
constexpr T determinant() const;
template<_internal::numeric_like U>
constexpr U determinant() const;
};
} // namespace matrix
} // namespace asalib
#line 8 "tests/matrix/core/abc237-b.test.cpp"
#define PROBLEM "https://atcoder.jp/contests/abc237/tasks/abc237_b"
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int h, w;
cin >> h >> w;
asalib::matrix::Matrix<int> a(h, w);
rep(i, h) rep(j, w) cin >> a.at(i, j);
auto t = a.transpose();
rep(i, w) {
rep(j, h) cout << t.at(i, j) << " \n"[j == h - 1];
}
return 0;
}