Asa's CP Library

a01sa01to's competitive programming library.

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub a01sa01to/cp-library

:heavy_check_mark: tests/matrix/core/itp1-6d.test.cpp

Depends on

Code

#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://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/6/ITP1_6_D"

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  asalib::matrix::Matrix<int> A(n, m);
  asalib::matrix::Matrix<int> b(m, 1);
  rep(i, n) rep(j, m) {
    int a;
    cin >> a;
    A.at(i, j) = a;
  }
  rep(i, m) {
    int a;
    cin >> a;
    b.at(i, 0) = a;
  }
  auto c = A * b;
  rep(i, n) cout << c.at(i, 0) << '\n';
  return 0;
}
#line 1 "tests/matrix/core/itp1-6d.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/itp1-6d.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/6/ITP1_6_D"

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  asalib::matrix::Matrix<int> A(n, m);
  asalib::matrix::Matrix<int> b(m, 1);
  rep(i, n) rep(j, m) {
    int a;
    cin >> a;
    A.at(i, j) = a;
  }
  rep(i, m) {
    int a;
    cin >> a;
    b.at(i, 0) = a;
  }
  auto c = A * b;
  rep(i, n) cout << c.at(i, 0) << '\n';
  return 0;
}
Back to top page