Tifa's CP Library

:heavy_check_mark: src/test_cpverifier/library-checker/matrix_det_mod_2.bmat.test.cpp

Depends on

Code

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

#include "../../code/lalg/bitmat.hpp"
#include "../../code/lalg/ge_bmat.hpp"

template <usz N>
using mat = tifa_libs::math::bitmat<N>;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  u32 n;
  std::cin >> n;

#define DO(num)                                       \
  if (n <= num) {                                     \
    mat<num> bm;                                      \
    tifa_libs::math::read_bitmat(std::cin, bm, n, n); \
    auto rk = tifa_libs::math::ge_bmat(bm, false);    \
    std::cout << (rk == n) << '\n';                   \
    return 0;                                         \
  }

  DO(1 << 6)
  DO(1 << 8)
  DO(1 << 12)

  return 0;
}
#line 1 "src/test_cpverifier/library-checker/matrix_det_mod_2.bmat.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_det_mod_2"

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



#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 5 "src/code/lalg/bitmat.hpp"

namespace tifa_libs::math {

#define FOR1_(i, l, r) for (usz 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 <usz R, usz C = R>
using bitmat = arr<std::bitset<C>, R>;

template <usz R, usz C>
constexpr matrix<bool> bitmat2mat(bitmat<R, C> const &bmat) {
  matrix<bool> ret(R, C);
  ret.apply([&bmat](u32 i, u32 j, auto &b) { b = bmat[i][j]; });
  return ret;
}

template <usz R, usz C = R>
constexpr bitmat<R, C> mat2bitmat(matrix<bool> const &mat) {
  u32 r = mat.row(), c = mat.col();
  assert(r <= R && c <= C);
  bitmat<R, C> ret;
  FOR2_ (i, 0, r, j, 0, c) ret[i][j] = mat(i, j);
  return ret;
}

template <usz R, usz C>
std::istream &read_bitmat(std::istream &is, bitmat<R, C> &bmat, u32 r, u32 c) {
  assert(r <= R && c <= C);
  char ch;
  FOR2_ (i, 0, r, j, 0, c) {
    is >> ch;
    bmat[i][j] = ch & 1;
  }
  return is;
}
template <usz R, usz C>
std::istream &read_bitmat_trans(std::istream &is, bitmat<R, C> &bmat, u32 r, u32 c) {
  assert(r <= R && c <= C);
  char ch;
  FOR2_ (i, 0, r, j, 0, c) {
    is >> ch;
    bmat[j][i] = ch & 1;
  }
  return is;
}

template <usz R, usz C>
std::ostream &print_bitmat(std::ostream &os, bitmat<R, C> &bmat, u32 r, u32 c) {
  assert(r <= R && c <= C);
  FOR2_ (i, 0, r, j, 0, c) {
    os << bmat[i][j];
    if (j == c - 1 && i != r - 1) os << '\n';
  }
  return os;
}

template <usz R, usz C>
std::istream &operator>>(std::istream &is, bitmat<R, C> &bmat) { return read_bitmat(is, bmat, R, C); }
template <usz R, usz C>
std::ostream &operator<<(std::ostream &os, bitmat<R, C> const &bmat) { return print_bitmat(os, bmat, R, C); }

#undef FOR1_
#undef FOR2_

}  // namespace tifa_libs::math


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



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

namespace tifa_libs::math {

template <usz R, usz C>
constexpr u32 ge_bmat(bitmat<R, C> &bmat, bool clear_u = true) {
  constexpr usz rk_max = std::min(R, C);
  u32 rk = 0;
  for (u32 i = 0, now_row = 0, j_ = i; i < R; ++i) {
    j_ = std::max(j_, i);
    if (!bmat[rk][i]) {
      now_row = rk;
      for (u32 j = now_row + 1; j < R; ++j)
        if (bmat[j][i]) now_row = j;
      if (now_row != rk) std::swap(bmat[now_row], bmat[rk]);
      while (j_ < C && !bmat[rk][j_]) ++j_;
      if (j_ == C) break;
    }
    for (u32 j = clear_u ? 0 : rk + 1; j < R; ++j) {
      if (j == rk || !bmat[j][j_]) continue;
      bmat[j] ^= bmat[rk];
    }
    if (++rk >= rk_max) break;
  }
  return rk;
}

}  // namespace tifa_libs::math


#line 5 "src/test_cpverifier/library-checker/matrix_det_mod_2.bmat.test.cpp"

template <usz N>
using mat = tifa_libs::math::bitmat<N>;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  u32 n;
  std::cin >> n;

#define DO(num)                                       \
  if (n <= num) {                                     \
    mat<num> bm;                                      \
    tifa_libs::math::read_bitmat(std::cin, bm, n, n); \
    auto rk = tifa_libs::math::ge_bmat(bm, false);    \
    std::cout << (rk == n) << '\n';                   \
    return 0;                                         \
  }

  DO(1 << 6)
  DO(1 << 8)
  DO(1 << 12)

  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-12 example_00 :heavy_check_mark: AC 12 ms 6 MB
g++-12 example_01 :heavy_check_mark: AC 12 ms 6 MB
g++-12 lowrank_max_random_00 :heavy_check_mark: AC 315 ms 6 MB
g++-12 lowrank_max_random_01 :heavy_check_mark: AC 351 ms 6 MB
g++-12 lowrank_max_random_02 :heavy_check_mark: AC 303 ms 6 MB
g++-12 lowrank_max_random_03 :heavy_check_mark: AC 315 ms 6 MB
g++-12 lowrank_max_random_04 :heavy_check_mark: AC 278 ms 6 MB
g++-12 max_random_00 :heavy_check_mark: AC 393 ms 6 MB
g++-12 max_random_01 :heavy_check_mark: AC 395 ms 6 MB
g++-12 max_random_02 :heavy_check_mark: AC 393 ms 6 MB
g++-12 max_random_03 :heavy_check_mark: AC 393 ms 6 MB
g++-12 max_random_04 :heavy_check_mark: AC 397 ms 6 MB
g++-12 max_random_05 :heavy_check_mark: AC 396 ms 6 MB
g++-12 middle_00 :heavy_check_mark: AC 32 ms 6 MB
g++-12 middle_01 :heavy_check_mark: AC 12 ms 6 MB
g++-12 middle_02 :heavy_check_mark: AC 12 ms 6 MB
g++-12 middle_03 :heavy_check_mark: AC 12 ms 6 MB
g++-12 middle_04 :heavy_check_mark: AC 25 ms 6 MB
g++-12 middle_05 :heavy_check_mark: AC 39 ms 6 MB
g++-12 perm_max_random_00 :heavy_check_mark: AC 139 ms 6 MB
g++-12 perm_max_random_01 :heavy_check_mark: AC 140 ms 6 MB
g++-12 perm_max_random_02 :heavy_check_mark: AC 235 ms 6 MB
g++-12 perm_max_random_03 :heavy_check_mark: AC 140 ms 6 MB
g++-12 perm_max_random_04 :heavy_check_mark: AC 128 ms 6 MB
g++-12 random_00 :heavy_check_mark: AC 32 ms 6 MB
g++-12 random_01 :heavy_check_mark: AC 12 ms 6 MB
g++-12 random_02 :heavy_check_mark: AC 11 ms 6 MB
g++-12 random_03 :heavy_check_mark: AC 268 ms 6 MB
g++-12 random_04 :heavy_check_mark: AC 181 ms 6 MB
g++-12 random_05 :heavy_check_mark: AC 414 ms 6 MB
g++-12 small_00 :heavy_check_mark: AC 11 ms 6 MB
g++-12 small_01 :heavy_check_mark: AC 11 ms 6 MB
g++-12 small_02 :heavy_check_mark: AC 11 ms 6 MB
g++-12 small_03 :heavy_check_mark: AC 11 ms 6 MB
g++-12 small_04 :heavy_check_mark: AC 10 ms 6 MB
g++-12 small_05 :heavy_check_mark: AC 10 ms 6 MB
Back to top page