Tifa's CP Library

:heavy_check_mark: src/test_cpverifier/library-checker/matrix_product_mod_2.mat.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/matrix_product_mod_2"

#include "../../code/lalg/mat.hpp"

using mat = tifa_libs::math::matrix<bool>;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  u32 n, m, k;
  std::cin >> n >> m >> k;
  mat a(n, m), b(m, k);
  char ch;
  for (u32 i = 0; i < n; ++i)
    for (u32 j = 0; j < m; ++j) {
      std::cin >> ch;
      a(i, j) = ch & 1;
    }
  for (u32 i = 0; i < m; ++i)
    for (u32 j = 0; j < k; ++j) {
      std::cin >> ch;
      b(i, j) = ch & 1;
    }
  auto c = a * b;
  for (auto& i : c.data()) {
    for (auto j : i) std::cout << j;
    std::cout << '\n';
  }
  return 0;
}
#line 1 "src/test_cpverifier/library-checker/matrix_product_mod_2.mat.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_product_mod_2"

#line 1 "src/code/lalg/mat.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/lalg/mat.hpp"

namespace tifa_libs::math {

#define FOR1_(i, l, r) for (u32 i = (l), i##ed__ = (r); i < i##ed__; ++i)
#define FOR2_(i, row_l, row_r, j, col_l, col_r) \
  FOR1_ (i, row_l, row_r)                       \
    FOR1_ (j, col_l, col_r)

template <class T>
class matrix {
  vvec<T> d;

 public:
  using value_type = T;

  constexpr matrix(u32 row, u32 col, T const &v = T{}) : d(row, vec<T>(col, v)) { assert(row > 0 && col > 0); }
  explicit constexpr matrix(vvec<T> const &data) : d(data) { assert(data.size() > 0 && data[0].size() > 0); }

  constexpr u32 row() const { return (u32)d.size(); }
  constexpr u32 col() const { return (u32)d[0].size(); }
  constexpr vvec<T> const &data() const { return d; }
  constexpr vvec<T> &data() { return d; }
  constexpr typename vec<T>::reference operator()(u32 r, u32 c) { return d[r][c]; }
  constexpr typename vec<T>::const_reference operator()(u32 r, u32 c) const { return d[r][c]; }

  template <class F>
  constexpr void apply(F &&f) { apply_range(0, row(), 0, col(), std::forward<F>(f)); }
  template <class F>
  requires requires(F f, u32 i, u32 j, T &val) {
    f(i, j, val);
  }
  constexpr void apply_range(u32 row_l, u32 row_r, u32 col_l, u32 col_r, F &&f) {
    assert(row_l < row_r && row_r <= row());
    assert(col_l < col_r && col_r <= col());
    FOR2_ (i, row_l, row_r, j, col_l, col_r) f(i, j, (*this)(i, j));
  }

  friend std::istream &operator>>(std::istream &is, matrix &mat) {
    u32 r_ = mat.row(), c_ = mat.col();
    FOR2_ (i, 0, r_, j, 0, c_) is >> mat(i, j);
    return is;
  }
  friend std::ostream &operator<<(std::ostream &os, matrix const &mat) {
    u32 r_ = mat.row(), c_ = mat.col();
    FOR2_ (i, 0, r_ - 1, j, 0, c_) os << mat(i, j) << " \n"[j + 1 == c_];
    os << mat(r_ - 1, 0);
    FOR1_ (j, 1, c_) os << ' ' << mat(r_ - 1, j);
    return os;
  }

  constexpr matrix submat(u32 row_l, u32 row_r, u32 col_l, u32 col_r) const {
    assert(row_l < row_r && row_r <= row());
    assert(col_l < col_r && col_r <= col());
    matrix ret(row_r - row_l, col_r - col_l);
    ret.apply_range(0, ret.row(), 0, ret.col(), [this, row_l, col_l](u32 i, u32 j, T &v) { v = (*this)(i + row_l, j + col_l); });
    return ret;
  }

  constexpr void swap_row(u32 r1, u32 r2) {
    assert(r1 < row() && r2 < row());
    if (r1 == r2) return;
    std::swap(d[r1], d[r2]);
  }
  constexpr void swap_col(u32 c1, u32 c2) {
    assert(c1 < col() && c2 < col());
    if (c1 == c2) return;
    FOR1_ (i, 0, row()) std::swap((*this)(i, c1), (*this)(i, c2));
  }

  constexpr matrix operator-() const {
    if constexpr (std::is_same_v<T, bool>) return *this;
    else {
      matrix ret = *this;
      ret.apply_range(0, row(), 0, col(), [](u32, u32, T &v) { v = -v; });
      return ret;
    }
  }

  friend constexpr matrix operator+(matrix l, T const &v) { return l += v; }
  friend constexpr matrix operator+(T const &v, matrix l) { return l += v; }
  constexpr matrix &operator+=(T const &v) {
    if constexpr (std::is_same_v<T, bool>) apply_range(0, row(), 0, col(), [&v](u32, u32, auto &val) { val = val ^ v; });
    else apply_range(0, row(), 0, col(), [&v](u32, u32, T &val) { val += v; });
    return *this;
  }
  friend constexpr matrix operator-(matrix l, T const &v) { return l -= v; }
  constexpr matrix &operator-=(T const &v) {
    if constexpr (std::is_same_v<T, bool>) apply_range(0, row(), 0, col(), [&v](u32, u32, auto &val) { val = val ^ v; });
    else apply_range(0, row(), 0, col(), [&v](u32, u32, T &val) { val -= v; });
    return *this;
  }
  friend constexpr matrix operator*(matrix l, T const &v) { return l *= v; }
  friend constexpr matrix operator*(T const &v, matrix l) { return l *= v; }
  constexpr matrix &operator*=(T const &v) {
    if constexpr (std::is_same_v<T, bool>) {
      if (!v)
        for (auto &i : d) i.clear(), i.resize(col());
      return *this;
    } else apply_range(0, row(), 0, col(), [&v](u32, u32, T &val) { val *= v; });
    return *this;
  }

  friend constexpr matrix operator+(matrix l, matrix const &r) { return l += r; }
  constexpr matrix &operator+=(matrix const &r) {
    assert(row() == r.row() && col() == r.col());
    if constexpr (std::is_same_v<T, bool>) apply_range(0, row(), 0, col(), [&r](u32 i, u32 j, auto &val) { val = val ^ r(i, j); });
    else apply_range(0, row(), 0, col(), [&r](u32 i, u32 j, T &val) { val += r(i, j); });
    return *this;
  }
  friend constexpr matrix operator-(matrix l, matrix const &r) { return l -= r; }
  constexpr matrix &operator-=(matrix const &r) {
    assert(row() == r.row() && col() == r.col());
    if constexpr (std::is_same_v<T, bool>) apply_range(0, row(), 0, col(), [&r](u32 i, u32 j, auto &val) { val = val ^ r(i, j); });
    else apply_range(0, row(), 0, col(), [&r](u32 i, u32 j, T &val) { val -= r(i, j); });
    return *this;
  }

  friend constexpr matrix operator*(matrix const &l, matrix const &r) {
    u32 i_ = l.row(), j_ = l.col(), k_ = r.col();
    assert(j_ == r.row());
    matrix ret(i_, k_);
    FOR1_ (i, 0, i_)
      FOR1_ (j, 0, j_)
        FOR1_ (k, 0, k_)
          if constexpr (std::is_same_v<T, bool>) ret(i, k) = ret(i, k) ^ (l(i, j) && r(j, k));
          else ret(i, k) += l(i, j) * r(j, k);
    return ret;
  }
  constexpr matrix &operator*=(matrix const &r) { return *this = *this * r; }

  constexpr vec<T> lproj(vec<T> const &x) const {
    u32 r_ = row(), c_ = col();
    assert(r_ == x.size());
    vec<T> ret(c_);
    for (u32 i = 0; i < c_; ++i)
      if constexpr (std::is_same_v<T, bool>) ret[i] = std::transform_reduce(d[i].begin(), d[i].end(), x.begin(), false, std::bit_xor<bool>{}, std::bit_and<bool>{});
      else ret[i] = std::transform_reduce(d[i].begin(), d[i].end(), x.begin(), T{});
    return ret;
  }

  constexpr bool operator==(matrix const &r) const {
    if (row() != r.row() || col() != r.col()) return 0;
    FOR1_ (i, 0, row())
      if (d[i] != r.d[i]) return 0;
    return 1;
  }
};

#undef FOR1_
#undef FOR2_

}  // namespace tifa_libs::math


#line 4 "src/test_cpverifier/library-checker/matrix_product_mod_2.mat.test.cpp"

using mat = tifa_libs::math::matrix<bool>;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  u32 n, m, k;
  std::cin >> n >> m >> k;
  mat a(n, m), b(m, k);
  char ch;
  for (u32 i = 0; i < n; ++i)
    for (u32 j = 0; j < m; ++j) {
      std::cin >> ch;
      a(i, j) = ch & 1;
    }
  for (u32 i = 0; i < m; ++i)
    for (u32 j = 0; j < k; ++j) {
      std::cin >> ch;
      b(i, j) = ch & 1;
    }
  auto c = a * b;
  for (auto& i : c.data()) {
    for (auto j : i) std::cout << j;
    std::cout << '\n';
  }
  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-12 example_00 :heavy_check_mark: AC 10 ms 4 MB
g++-12 example_01 :heavy_check_mark: AC 9 ms 4 MB
g++-12 example_02 :heavy_check_mark: AC 9 ms 4 MB
g++-12 max_random_00 :heavy_check_mark: AC 149989 ms 10 MB
g++-12 max_random_01 :heavy_check_mark: AC 144947 ms 10 MB
g++-12 max_random_02 :heavy_check_mark: AC 144930 ms 10 MB
g++-12 middle_00 :heavy_check_mark: AC 348 ms 4 MB
g++-12 middle_01 :heavy_check_mark: AC 38 ms 4 MB
g++-12 middle_02 :heavy_check_mark: AC 50 ms 4 MB
g++-12 middle_03 :heavy_check_mark: AC 44 ms 4 MB
g++-12 middle_04 :heavy_check_mark: AC 592 ms 4 MB
g++-12 random_00 :heavy_check_mark: AC 74420 ms 8 MB
g++-12 random_01 :heavy_check_mark: AC 47220 ms 7 MB
g++-12 random_02 :heavy_check_mark: AC 40164 ms 6 MB
g++-12 small_00 :heavy_check_mark: AC 11 ms 4 MB
g++-12 small_01 :heavy_check_mark: AC 10 ms 4 MB
g++-12 small_02 :heavy_check_mark: AC 9 ms 4 MB
g++-12 small_03 :heavy_check_mark: AC 11 ms 4 MB
g++-12 small_04 :heavy_check_mark: AC 10 ms 4 MB
g++-12 small_05 :heavy_check_mark: AC 10 ms 4 MB
g++-12 small_06 :heavy_check_mark: AC 10 ms 4 MB
g++-12 small_07 :heavy_check_mark: AC 10 ms 4 MB
g++-12 small_08 :heavy_check_mark: AC 10 ms 4 MB
g++-12 small_09 :heavy_check_mark: AC 10 ms 4 MB
Back to top page