Skip to the content.

:heavy_check_mark: Matrix Mod 2
(matrix/matrix-mod2.hpp)

$\mathbb{F}_2$ 上の行列.

各行を DynamicBitset で保持する.和と差は xor,行基本変形も xor で行う.

set_row(i, s)row_string(i) は,0/1 文字列形式の入力出力に使う.

Depends on

Verified with

Code

#pragma once

#include "data-structure/dynamic-bitset.hpp"

struct MatrixMod2 {
  using BS = DynamicBitset;
  int h, w;
  vector<BS> a;
  MatrixMod2() : h(0), w(0) {}
  MatrixMod2(int n) : h(n), w(n), a(n, BS(n)) {}
  MatrixMod2(int h_, int w_) : h(h_), w(w_), a(h, BS(w)) {}

  bool get(int i, int j) const { return a[i][j]; }
  void set(int i, int j, bool v = true) { a[i].set(j, v); }
  void add(int i, int j, bool v = true) {
    if (v) a[i].flip(j);
  }
  void sub(int i, int j, bool v = true) { add(i, j, v); }
  static MatrixMod2 id(int n) {
    MatrixMod2 mat(n);
    for (int i = 0; i < n; i++) mat.set(i, i);
    return mat;
  }
  MatrixMod2& operator+=(const MatrixMod2& r) {
    assert(h == r.h && w == r.w);
    for (int i = 0; i < h; i++) a[i] ^= r.a[i];
    return *this;
  }
  MatrixMod2& operator-=(const MatrixMod2& r) { return *this += r; }
  MatrixMod2 operator+(const MatrixMod2& r) const { return MatrixMod2(*this) += r; }
  MatrixMod2 operator-(const MatrixMod2& r) const { return MatrixMod2(*this) -= r; }
  MatrixMod2 operator*(const MatrixMod2& r) const {
    if (w <= 256) return multiply_sparse(r);
    return multiply_four_russians(r);
  }
  MatrixMod2 multiply_sparse(const MatrixMod2& r) const {
    assert(w == r.h);
    MatrixMod2 ret(h, r.w);
    for (int i = 0; i < h; i++)
      for (int k = a[i].find_first(); k < w; k = a[i].find_next(k))
        ret.a[i] ^= r.a[k];
    return ret;
  }
  MatrixMod2 multiply_four_russians(const MatrixMod2& r, int block = 0) const {
    assert(w == r.h);
    if (block == 0) block = four_russians_block_size(w);
    assert(block > 0);
    MatrixMod2 ret(h, r.w);
    for (int l = 0; l < w; l += block) {
      int len = min(block, w - l);
      vector<BS> table(1 << len, BS(r.w));
      for (int mask = 1; mask < (1 << len); mask++) {
        int b = __builtin_ctz(mask);
        table[mask] = table[mask ^ (1 << b)];
        table[mask] ^= r.a[l + b];
      }
      for (int i = 0; i < h; i++) {
        int mask = 0;
        for (int j = 0; j < len; j++)
          if (get(i, l + j)) mask |= 1 << j;
        ret.a[i] ^= table[mask];
      }
    }
    return ret;
  }
  static int four_russians_block_size(int n) {
    if (n <= 512) return 4;
    if (n <= 1024) return 5;
    if (n <= 2048) return 6;
    return 7;
  }
  MatrixMod2& operator*=(const MatrixMod2& r) { return *this = *this * r; }

  MatrixMod2 pow(long long n) const {
    assert(h == w);
    MatrixMod2 ret = id(h);
    MatrixMod2 mat(*this);
    while (n > 0) {
      if (n & 1) ret *= mat;
      mat *= mat;
      n >>= 1;
    }
    return ret;
  }
  int rank() const {
    MatrixMod2 mat(*this);
    int r = 0;
    for (int c = 0; c < w && r < h; c++) {
      int p = r;
      while (p < h && !mat.get(p, c)) p++;
      if (p == h) continue;
      mat.swap_row(r, p);
      for (int i = 0; i < h; i++)
        if (i != r && mat.get(i, c)) mat.add_row(i, r);
      r++;
    }
    return r;
  }
  int det() const {
    assert(h == w);
    MatrixMod2 mat(*this);
    for (int c = 0; c < w; c++) {
      int p = c;
      while (p < h && !mat.get(p, c)) p++;
      if (p == h) return 0;
      mat.swap_row(c, p);
      for (int i = c + 1; i < h; i++) {
        if (mat.get(i, c)) mat.add_row(i, c);
      }
    }
    return 1;
  }
  optional<MatrixMod2> inv() const {
    assert(h == w);
    MatrixMod2 mat(*this);
    MatrixMod2 imat = id(h);
    for (int c = 0; c < w; c++) {
      int p = c;
      while (p < h && !mat.get(p, c)) p++;
      if (p == h) return nullopt;
      mat.swap_row(c, p);
      imat.swap_row(c, p);
      for (int i = 0; i < h; i++) {
        if (i == c || !mat.get(i, c)) continue;
        mat.add_row(i, c);
        imat.add_row(i, c);
      }
    }
    return imat;
  }

  void swap_row(int i, int j) {
    if (i == j) return;
    swap(a[i], a[j]);
  }
  void add_row(int i, int j) { a[i] ^= a[j]; }
  void set_row(int i, const string& s) {
    assert((int)s.size() == w);
    a[i].reset();
    for (int j = 0; j < w; j++) {
      assert(s[j] == '0' || s[j] == '1');
      if (s[j] == '1') a[i].set(j);
    }
  }
  string row_string(int i) const {
    string s(w, '0');
    for (int j = 0; j < w; j++)
      if (get(i, j)) s[j] = '1';
    return s;
  }
  friend ostream& operator<<(ostream& os, const MatrixMod2& mat) {
    for (int i = 0; i < mat.h; i++) {
      os << mat.row_string(i);
      if (i + 1 < mat.h) os << "\n";
    }
    return os;
  }
};

/**
 * @brief Matrix Mod 2
 * @docs docs/matrix/matrix-mod2.md
 */
#line 2 "matrix/matrix-mod2.hpp"

#line 2 "data-structure/dynamic-bitset.hpp"

struct DynamicBitset {
  using u64 = uint64_t;
  struct reference {
    DynamicBitset* b;
    int pos;
    reference& operator=(bool v) {
      b->set(pos, v);
      return *this;
    }
    reference& operator=(const reference& r) { return *this = bool(r); }
    reference& flip() {
      b->flip(pos);
      return *this;
    }
    operator bool() const { return b->test(pos); }
  };

  DynamicBitset() : _n(0) {}
  explicit DynamicBitset(int n, bool value = false) : _n(n), a(blocks(n), value ? ~0ull : 0ull) {
    trim();
  }

  int size() const { return _n; }
  bool empty() const { return _n == 0; }
  bool test(int pos) const {
    assert(0 <= pos && pos < _n);
    return (a[pos / W] >> (pos % W)) & 1ull;
  }
  bool operator[](int pos) const { return test(pos); }
  reference operator[](int pos) {
    assert(0 <= pos && pos < _n);
    return reference{this, pos};
  }
  DynamicBitset& set() {
    fill(a.begin(), a.end(), ~0ull);
    trim();
    return *this;
  }
  DynamicBitset& set(int pos, bool value = true) {
    assert(0 <= pos && pos < _n);
    if (value)
      a[pos / W] |= 1ull << (pos % W);
    else
      a[pos / W] &= ~(1ull << (pos % W));
    return *this;
  }
  DynamicBitset& reset() {
    fill(a.begin(), a.end(), 0);
    return *this;
  }
  DynamicBitset& reset(int pos) { return set(pos, false); }
  DynamicBitset& flip() {
    for (auto& x : a) x = ~x;
    trim();
    return *this;
  }
  DynamicBitset& flip(int pos) {
    assert(0 <= pos && pos < _n);
    a[pos / W] ^= 1ull << (pos % W);
    return *this;
  }
  int count() const {
    int ret = 0;
    for (u64 x : a) ret += __builtin_popcountll(x);
    return ret;
  }
  bool any() const {
    for (u64 x : a)
      if (x) return true;
    return false;
  }
  bool none() const { return !any(); }
  bool all() const { return count() == _n; }

  DynamicBitset& operator&=(const DynamicBitset& r) {
    assert(_n == r._n);
    for (int i = 0; i < (int)a.size(); i++) a[i] &= r.a[i];
    return *this;
  }
  DynamicBitset& operator|=(const DynamicBitset& r) {
    assert(_n == r._n);
    for (int i = 0; i < (int)a.size(); i++) a[i] |= r.a[i];
    return *this;
  }
  DynamicBitset& operator^=(const DynamicBitset& r) {
    assert(_n == r._n);
    for (int i = 0; i < (int)a.size(); i++) a[i] ^= r.a[i];
    return *this;
  }
  DynamicBitset operator~() const {
    DynamicBitset ret(*this);
    ret.flip();
    return ret;
  }
  DynamicBitset operator&(const DynamicBitset& r) const { return DynamicBitset(*this) &= r; }
  DynamicBitset operator|(const DynamicBitset& r) const { return DynamicBitset(*this) |= r; }
  DynamicBitset operator^(const DynamicBitset& r) const { return DynamicBitset(*this) ^= r; }
  DynamicBitset& operator<<=(int k) {
    assert(k >= 0);
    if (k >= _n) return reset();
    int q = k / W, r = k % W;
    if (q) {
      for (int i = (int)a.size() - 1; i >= 0; i--) a[i] = i >= q ? a[i - q] : 0;
    }
    if (r) {
      for (int i = (int)a.size() - 1; i > 0; i--) a[i] = (a[i] << r) | (a[i - 1] >> (W - r));
      a[0] <<= r;
    }
    trim();
    return *this;
  }
  DynamicBitset& operator>>=(int k) {
    assert(k >= 0);
    if (k >= _n) return reset();
    int q = k / W, r = k % W;
    if (q) {
      for (int i = 0; i < (int)a.size(); i++) a[i] = i + q < (int)a.size() ? a[i + q] : 0;
    }
    if (r) {
      for (int i = 0; i + 1 < (int)a.size(); i++) a[i] = (a[i] >> r) | (a[i + 1] << (W - r));
      a.back() >>= r;
    }
    return *this;
  }
  DynamicBitset operator<<(int k) const { return DynamicBitset(*this) <<= k; }
  DynamicBitset operator>>(int k) const { return DynamicBitset(*this) >>= k; }
  bool operator==(const DynamicBitset& r) const { return _n == r._n && a == r.a; }
  bool operator!=(const DynamicBitset& r) const { return !(*this == r); }
  int find_first() const { return find_next(-1); }
  int find_next(int pos) const {
    assert(-1 <= pos && pos < _n);
    int i = pos + 1;
    if (i >= _n) return _n;
    int b = i / W;
    u64 x = a[b] & (~0ull << (i % W));
    while (true) {
      if (x) {
        int ret = b * W + __builtin_ctzll(x);
        return ret < _n ? ret : _n;
      }
      if (++b == (int)a.size()) return _n;
      x = a[b];
    }
  }
  string to_string() const {
    string s(_n, '0');
    for (int i = 0; i < _n; i++)
      if (test(i)) s[_n - 1 - i] = '1';
    return s;
  }

 private:
  static constexpr int W = 64;
  int _n;
  vector<u64> a;
  static int blocks(int n) { return (n + W - 1) / W; }
  void trim() {
    if (_n == 0) return;
    int r = _n % W;
    if (r) a.back() &= (1ull << r) - 1;
  }
};

/**
 * @brief Dynamic Bitset
 * @docs docs/data-structure/dynamic-bitset.md
 */
#line 4 "matrix/matrix-mod2.hpp"

struct MatrixMod2 {
  using BS = DynamicBitset;
  int h, w;
  vector<BS> a;
  MatrixMod2() : h(0), w(0) {}
  MatrixMod2(int n) : h(n), w(n), a(n, BS(n)) {}
  MatrixMod2(int h_, int w_) : h(h_), w(w_), a(h, BS(w)) {}

  bool get(int i, int j) const { return a[i][j]; }
  void set(int i, int j, bool v = true) { a[i].set(j, v); }
  void add(int i, int j, bool v = true) {
    if (v) a[i].flip(j);
  }
  void sub(int i, int j, bool v = true) { add(i, j, v); }
  static MatrixMod2 id(int n) {
    MatrixMod2 mat(n);
    for (int i = 0; i < n; i++) mat.set(i, i);
    return mat;
  }
  MatrixMod2& operator+=(const MatrixMod2& r) {
    assert(h == r.h && w == r.w);
    for (int i = 0; i < h; i++) a[i] ^= r.a[i];
    return *this;
  }
  MatrixMod2& operator-=(const MatrixMod2& r) { return *this += r; }
  MatrixMod2 operator+(const MatrixMod2& r) const { return MatrixMod2(*this) += r; }
  MatrixMod2 operator-(const MatrixMod2& r) const { return MatrixMod2(*this) -= r; }
  MatrixMod2 operator*(const MatrixMod2& r) const {
    if (w <= 256) return multiply_sparse(r);
    return multiply_four_russians(r);
  }
  MatrixMod2 multiply_sparse(const MatrixMod2& r) const {
    assert(w == r.h);
    MatrixMod2 ret(h, r.w);
    for (int i = 0; i < h; i++)
      for (int k = a[i].find_first(); k < w; k = a[i].find_next(k))
        ret.a[i] ^= r.a[k];
    return ret;
  }
  MatrixMod2 multiply_four_russians(const MatrixMod2& r, int block = 0) const {
    assert(w == r.h);
    if (block == 0) block = four_russians_block_size(w);
    assert(block > 0);
    MatrixMod2 ret(h, r.w);
    for (int l = 0; l < w; l += block) {
      int len = min(block, w - l);
      vector<BS> table(1 << len, BS(r.w));
      for (int mask = 1; mask < (1 << len); mask++) {
        int b = __builtin_ctz(mask);
        table[mask] = table[mask ^ (1 << b)];
        table[mask] ^= r.a[l + b];
      }
      for (int i = 0; i < h; i++) {
        int mask = 0;
        for (int j = 0; j < len; j++)
          if (get(i, l + j)) mask |= 1 << j;
        ret.a[i] ^= table[mask];
      }
    }
    return ret;
  }
  static int four_russians_block_size(int n) {
    if (n <= 512) return 4;
    if (n <= 1024) return 5;
    if (n <= 2048) return 6;
    return 7;
  }
  MatrixMod2& operator*=(const MatrixMod2& r) { return *this = *this * r; }

  MatrixMod2 pow(long long n) const {
    assert(h == w);
    MatrixMod2 ret = id(h);
    MatrixMod2 mat(*this);
    while (n > 0) {
      if (n & 1) ret *= mat;
      mat *= mat;
      n >>= 1;
    }
    return ret;
  }
  int rank() const {
    MatrixMod2 mat(*this);
    int r = 0;
    for (int c = 0; c < w && r < h; c++) {
      int p = r;
      while (p < h && !mat.get(p, c)) p++;
      if (p == h) continue;
      mat.swap_row(r, p);
      for (int i = 0; i < h; i++)
        if (i != r && mat.get(i, c)) mat.add_row(i, r);
      r++;
    }
    return r;
  }
  int det() const {
    assert(h == w);
    MatrixMod2 mat(*this);
    for (int c = 0; c < w; c++) {
      int p = c;
      while (p < h && !mat.get(p, c)) p++;
      if (p == h) return 0;
      mat.swap_row(c, p);
      for (int i = c + 1; i < h; i++) {
        if (mat.get(i, c)) mat.add_row(i, c);
      }
    }
    return 1;
  }
  optional<MatrixMod2> inv() const {
    assert(h == w);
    MatrixMod2 mat(*this);
    MatrixMod2 imat = id(h);
    for (int c = 0; c < w; c++) {
      int p = c;
      while (p < h && !mat.get(p, c)) p++;
      if (p == h) return nullopt;
      mat.swap_row(c, p);
      imat.swap_row(c, p);
      for (int i = 0; i < h; i++) {
        if (i == c || !mat.get(i, c)) continue;
        mat.add_row(i, c);
        imat.add_row(i, c);
      }
    }
    return imat;
  }

  void swap_row(int i, int j) {
    if (i == j) return;
    swap(a[i], a[j]);
  }
  void add_row(int i, int j) { a[i] ^= a[j]; }
  void set_row(int i, const string& s) {
    assert((int)s.size() == w);
    a[i].reset();
    for (int j = 0; j < w; j++) {
      assert(s[j] == '0' || s[j] == '1');
      if (s[j] == '1') a[i].set(j);
    }
  }
  string row_string(int i) const {
    string s(w, '0');
    for (int j = 0; j < w; j++)
      if (get(i, j)) s[j] = '1';
    return s;
  }
  friend ostream& operator<<(ostream& os, const MatrixMod2& mat) {
    for (int i = 0; i < mat.h; i++) {
      os << mat.row_string(i);
      if (i + 1 < mat.h) os << "\n";
    }
    return os;
  }
};

/**
 * @brief Matrix Mod 2
 * @docs docs/matrix/matrix-mod2.md
 */
Back to top page