cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:heavy_check_mark: 数学/行列/バイナリ行列/連立一次方程式 バイナリ行列版
(test/math/matrix/binary_matrix/linear_equation.test.cpp)

Depends on

Code

/*
 * @title 数学/行列/バイナリ行列/連立一次方程式 バイナリ行列版
 *
 * verification-helper: PROBLEM https://yukicoder.me/problems/no/1421
 */

#include <iostream>
#include <vector>

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

int main() {
  constexpr int N = 50, B = 30;
  int n, m;
  std::cin >> n >> m;
  std::vector<std::vector<int>> b(m);
  std::vector<int> y(m);
  for (int i = 0; i < m; ++i) {
    int a;
    std::cin >> a;
    b[i].resize(a);
    for (int j = 0; j < a; ++j) {
      std::cin >> b[i][j];
      --b[i][j];
    }
    std::cin >> y[i];
  }
  std::vector<int> x(n, 0);
  for (int bit = 0; bit < B; ++bit) {
    emthrm::BinaryMatrix<N> a(m, n, false);
    std::vector<bool> v(m);
    for (int i = 0; i < m; ++i) {
      for (const int b_ij : b[i]) a[i].set(b_ij);
      v[i] = y[i] >> bit & 1;
    }
    const std::vector<bool> ans = emthrm::linear_equation(a, v);
    if (ans.empty()) {
      std::cout << "-1\n";
      return 0;
    }
    for (int i = 0; i < n; ++i) {
      if (ans[i]) x[i] |= 1 << bit;
    }
  }
  for (int i = 0; i < n; ++i) {
    std::cout << x[i] << '\n';
  }
  return 0;
}
#line 1 "test/math/matrix/binary_matrix/linear_equation.test.cpp"
/*
 * @title 数学/行列/バイナリ行列/連立一次方程式 バイナリ行列版
 *
 * verification-helper: PROBLEM https://yukicoder.me/problems/no/1421
 */

#include <iostream>
#include <vector>

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



#include <bitset>
#include <string>
#line 7 "include/emthrm/math/matrix/binary_matrix/binary_matrix.hpp"

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 1 "include/emthrm/math/matrix/binary_matrix/linear_equation.hpp"



#line 5 "include/emthrm/math/matrix/binary_matrix/linear_equation.hpp"

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



#include <utility>

#line 7 "include/emthrm/math/matrix/binary_matrix/gauss_jordan.hpp"

namespace emthrm {

template <bool IS_EXTENDED = false, int N>
int gauss_jordan(BinaryMatrix<N>* a) {
  const int m = a->nrow(), n = a->ncol();
  int rank = 0;
  for (int col = 0; col < (IS_EXTENDED ? n - 1 : n); ++col) {
    int pivot = -1;
    for (int row = rank; row < m; ++row) {
      if ((*a)[row][col]) {
        pivot = row;
        break;
      }
    }
    if (pivot == -1) continue;
    std::swap((*a)[rank], (*a)[pivot]);
    for (int row = 0; row < m; ++row) {
      if (row != rank && (*a)[row][col]) (*a)[row] ^= (*a)[rank];
    }
    ++rank;
  }
  return rank;
}

}  // namespace emthrm


#line 8 "include/emthrm/math/matrix/binary_matrix/linear_equation.hpp"

namespace emthrm {

template <int N>
std::vector<bool> linear_equation(const BinaryMatrix<N>& a,
                                  const std::vector<bool>& b) {
  const int m = a.nrow(), n = a.ncol();
  BinaryMatrix<N> c(m, n + 1);
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      c[i][j] = a[i][j];
    }
    c[i][n] = b[i];
  }
  const int rank = gauss_jordan<true>(&c);
  for (int row = rank; row < m; ++row) {
    if (c[row][n]) return std::vector<bool>{};
  }
  std::vector<bool> res(n, false);
  for (int i = 0, j = -1; i < rank; ++i) {
    j = (i == 0 ? c[i]._Find_first() : c[i]._Find_next(j));
    res[j] = c[i][n];
  }
  return res;
}

}  // namespace emthrm


#line 12 "test/math/matrix/binary_matrix/linear_equation.test.cpp"

int main() {
  constexpr int N = 50, B = 30;
  int n, m;
  std::cin >> n >> m;
  std::vector<std::vector<int>> b(m);
  std::vector<int> y(m);
  for (int i = 0; i < m; ++i) {
    int a;
    std::cin >> a;
    b[i].resize(a);
    for (int j = 0; j < a; ++j) {
      std::cin >> b[i][j];
      --b[i][j];
    }
    std::cin >> y[i];
  }
  std::vector<int> x(n, 0);
  for (int bit = 0; bit < B; ++bit) {
    emthrm::BinaryMatrix<N> a(m, n, false);
    std::vector<bool> v(m);
    for (int i = 0; i < m; ++i) {
      for (const int b_ij : b[i]) a[i].set(b_ij);
      v[i] = y[i] >> bit & 1;
    }
    const std::vector<bool> ans = emthrm::linear_equation(a, v);
    if (ans.empty()) {
      std::cout << "-1\n";
      return 0;
    }
    for (int i = 0; i < n; ++i) {
      if (ans[i]) x[i] |= 1 << bit;
    }
  }
  for (int i = 0; i < n; ++i) {
    std::cout << x[i] << '\n';
  }
  return 0;
}
Back to top page