cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 数学/行列/バイナリ行列/バイナリ行列
(test/math/matrix/binary_matrix/binary_matrix.test.cpp)

Depends on

Code

/*
 * @title 数学/行列/バイナリ行列/バイナリ行列
 *
 * verification-helper: PROBLEM https://judge.yosupo.jp/problem/matrix_product_mod_2
 */

#include <algorithm>
#include <bitset>
#include <iostream>
#include <ranges>
#include <string>

#include "emthrm/math/matrix/binary_matrix/binary_matrix.hpp"

int main() {
  constexpr int N = 4096;
  int n, m, k;
  std::cin >> n >> m >> k;
  emthrm::BinaryMatrix<N> a(n, m), b(m, k);
  for (const int i : std::views::iota(0, n)) {
    std::string s;
    std::cin >> s;
    std::ranges::reverse(s);
    a[i] = std::bitset<N>(s);
  }
  for (const int i : std::views::iota(0, m)) {
    std::string s;
    std::cin >> s;
    std::ranges::reverse(s);
    b[i] = std::bitset<N>(s);
  }
  const emthrm::BinaryMatrix<N> c = a * b;
  for (const int i : std::views::iota(0, n)) {
    for (const int j : std::views::iota(0, k)) {
      std::cout << (c[i].test(j) ? '1' : '0');
    }
    std::cout << '\n';
  }
  return 0;
}
#line 1 "test/math/matrix/binary_matrix/binary_matrix.test.cpp"
/*
 * @title 数学/行列/バイナリ行列/バイナリ行列
 *
 * verification-helper: PROBLEM https://judge.yosupo.jp/problem/matrix_product_mod_2
 */

#include <algorithm>
#include <bitset>
#include <iostream>
#include <ranges>
#include <string>

#line 1 "include/emthrm/math/matrix/binary_matrix/binary_matrix.hpp"



#line 6 "include/emthrm/math/matrix/binary_matrix/binary_matrix.hpp"
#include <vector>

namespace emthrm {

template <int N>
struct BinaryMatrix {
  explicit BinaryMatrix(const int m, const int n = N, const bool def = false)
      : n(n), data(m, std::bitset<N>(std::string(n, def ? '1' : '0'))) {}

  int nrow() const { return data.size(); }
  int ncol() const { return n; }

  BinaryMatrix pow(long long exponent) const {
    BinaryMatrix res(n, n), tmp = *this;
    for (int i = 0; i < n; ++i) {
      res[i].set(i);
    }
    for (; exponent > 0; exponent >>= 1) {
      if (exponent & 1) res *= tmp;
      tmp *= tmp;
    }
    return res;
  }

  inline const std::bitset<N>& operator[](const int i) const { return data[i]; }
  inline std::bitset<N>& operator[](const int i) { return data[i]; }

  BinaryMatrix& operator=(const BinaryMatrix& x) = default;

  BinaryMatrix& operator+=(const BinaryMatrix& x) {
    const int m = nrow();
    for (int i = 0; i < m; ++i) {
      data[i] ^= x[i];
    }
    return *this;
  }

  BinaryMatrix& operator*=(const BinaryMatrix& x) {
    const int m = nrow(), l = x.ncol();
    BinaryMatrix t_x(l, n), res(m, l);
    for (int i = 0; i < l; ++i) {
      for (int j = 0; j < n; ++j) {
        t_x[i][j] = x[j][i];
      }
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < l; ++j) {
        if ((data[i] & t_x[j]).count() & 1) res[i].set(j);
      }
    }
    return *this = res;
  }

  BinaryMatrix operator+(const BinaryMatrix& x) const {
    return BinaryMatrix(*this) += x;
  }
  BinaryMatrix operator*(const BinaryMatrix& x) const {
    return BinaryMatrix(*this) *= x;
  }

 private:
  int n;
  std::vector<std::bitset<N>> data;
};

}  // namespace emthrm


#line 14 "test/math/matrix/binary_matrix/binary_matrix.test.cpp"

int main() {
  constexpr int N = 4096;
  int n, m, k;
  std::cin >> n >> m >> k;
  emthrm::BinaryMatrix<N> a(n, m), b(m, k);
  for (const int i : std::views::iota(0, n)) {
    std::string s;
    std::cin >> s;
    std::ranges::reverse(s);
    a[i] = std::bitset<N>(s);
  }
  for (const int i : std::views::iota(0, m)) {
    std::string s;
    std::cin >> s;
    std::ranges::reverse(s);
    b[i] = std::bitset<N>(s);
  }
  const emthrm::BinaryMatrix<N> c = a * b;
  for (const int i : std::views::iota(0, n)) {
    for (const int j : std::views::iota(0, k)) {
      std::cout << (c[i].test(j) ? '1' : '0');
    }
    std::cout << '\n';
  }
  return 0;
}
Back to top page