proconlib

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: tests/matrix_mod2/rank.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/matrix_rank_mod_2

#include <iostream>
#include "atcoder/modint.hpp"
#include "tools/matrix_mod2.hpp"

using mint = atcoder::static_modint<2>;

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  int N, M;
  std::cin >> N >> M;
  tools::matrix<mint> A(N, M);
  std::cin >> A;

  std::cout << A.rank() << '\n';
  return 0;
}
#line 1 "tests/matrix_mod2/rank.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/matrix_rank_mod_2

#include <iostream>
#line 1 "lib/ac-library/atcoder/modint.hpp"



#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

#line 1 "lib/ac-library/atcoder/internal_math.hpp"



#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m`
    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned long long y = x * _m;
        return (unsigned int)(z - y + (z < y ? _m : 0));
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#line 1 "lib/ac-library/atcoder/internal_type_traits.hpp"



#line 7 "lib/ac-library/atcoder/internal_type_traits.hpp"

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


#line 14 "lib/ac-library/atcoder/modint.hpp"

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


#line 1 "tools/matrix_mod2.hpp"



#include <algorithm>
#include <array>
#include <bitset>
#line 8 "tools/matrix_mod2.hpp"
#include <initializer_list>
#line 10 "tools/matrix_mod2.hpp"
#include <iterator>
#include <optional>
#include <string_view>
#line 15 "tools/matrix_mod2.hpp"
#include <vector>
#line 1 "tools/dynamic_bitset.hpp"



#line 5 "tools/dynamic_bitset.hpp"
#include <bit>
#line 7 "tools/dynamic_bitset.hpp"
#include <cstddef>
#include <cstdint>
#line 11 "tools/dynamic_bitset.hpp"
#include <limits>
#include <sstream>
#include <string>
#line 1 "tools/ceil.hpp"



#line 1 "tools/is_integral.hpp"



#line 5 "tools/is_integral.hpp"

namespace tools {
  template <typename T>
  struct is_integral : ::std::is_integral<T> {};

  template <typename T>
  inline constexpr bool is_integral_v = ::tools::is_integral<T>::value;
}


#line 1 "tools/is_unsigned.hpp"



#line 5 "tools/is_unsigned.hpp"

namespace tools {
  template <typename T>
  struct is_unsigned : ::std::is_unsigned<T> {};

  template <typename T>
  inline constexpr bool is_unsigned_v = ::tools::is_unsigned<T>::value;
}


#line 8 "tools/ceil.hpp"

namespace tools {
  template <typename M, typename N> requires (
    ::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
    ::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
  constexpr ::std::common_type_t<M, N> ceil(const M x, const N y) noexcept {
    assert(y != 0);
    if (y >= 0) {
      if (x > 0) {
        return (x - 1) / y + 1;
      } else {
        if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
          return 0;
        } else {
          return x / y;
        }
      }
    } else {
      if (x >= 0) {
        if constexpr (::tools::is_unsigned_v<::std::common_type_t<M, N>>) {
          return 0;
        } else {
          return x / y;
        }
      } else {
        return (x + 1) / y + 1;
      }
    }
  }
}


#line 16 "tools/dynamic_bitset.hpp"

namespace tools {
  class dynamic_bitset {
    constexpr static ::std::size_t W = ::std::numeric_limits<::std::uint64_t>::digits;
    ::std::size_t m_size;
    ::std::vector<::std::uint64_t> m_bits;

  public:
    class reference {
      friend class ::tools::dynamic_bitset;

      ::tools::dynamic_bitset *m_parent;
      ::std::size_t m_pos;

      reference(::tools::dynamic_bitset * const parent, const ::std::size_t pos) : m_parent(parent), m_pos(pos) {
      }

    public:
      reference(const reference&) = default;
      reference& operator=(const bool x) {
        this->m_parent->set(this->m_pos, x);
        return *this;
      }
      reference& operator=(const reference& other) {
        return *this = static_cast<bool>(other);
      }
      bool operator~() const {
        return !static_cast<bool>(*this);
      }
      operator bool() const {
        return this->m_parent->test(this->m_pos);
      }
      reference& flip() {
        this->m_parent->flip(this->m_pos);
        return *this;
      }
    };

    dynamic_bitset() : m_size(0) {}
    explicit dynamic_bitset(const ::std::size_t size) : m_size(size), m_bits(::tools::ceil(size, W), 0) {}
    explicit dynamic_bitset(const ::std::string& str) : m_size(str.size()), m_bits(::tools::ceil(str.size(), W), 0) {
      for (::std::size_t i = 0; i < str.size(); ++i) {
        const auto c = str[str.size() - 1 - i];
        assert(c == '0' || c == '1');
        if (c == '1') {
          this->m_bits[i / W] |= UINT64_C(1) << (i % W);
        }
      }
    }

    ::tools::dynamic_bitset& operator&=(const ::tools::dynamic_bitset& other) {
      assert(this->m_size == other.m_size);
      for (::std::size_t i = 0; i < this->m_bits.size(); ++i) {
        this->m_bits[i] &= other.m_bits[i];
      }
      return *this;
    }
    ::tools::dynamic_bitset& operator|=(const ::tools::dynamic_bitset& other) {
      assert(this->m_size == other.m_size);
      for (::std::size_t i = 0; i < this->m_bits.size(); ++i) {
        this->m_bits[i] |= other.m_bits[i];
      }
      return *this;
    }
    ::tools::dynamic_bitset& operator^=(const ::tools::dynamic_bitset& other) {
      assert(this->m_size == other.m_size);
      for (::std::size_t i = 0; i < this->m_bits.size(); ++i) {
        this->m_bits[i] ^= other.m_bits[i];
      }
      return *this;
    }
    ::tools::dynamic_bitset& operator<<=(const ::std::size_t pos) {
      const ::std::size_t diff = pos / W;
      if (diff < this->m_bits.size()) {
        if (pos % W > 0) {
          for (::std::size_t i = this->m_bits.size() - diff; i --> 0;) {
            this->m_bits[i] <<= pos % W;
            if (i > 0) {
              this->m_bits[i] |= this->m_bits[i - 1] >> (W - pos % W);
            }
          }
        }
        if (diff > 0) {
          for (::std::size_t i = this->m_bits.size() - diff; i --> 0;) {
            this->m_bits[i + diff] = this->m_bits[i];
          }
          ::std::fill(this->m_bits.begin(), ::std::next(this->m_bits.begin(), diff), 0);
        }
        if (this->m_size % W > 0) {
          this->m_bits.back() &= (UINT64_C(1) << (this->m_size % W)) - 1;
        }
      } else {
        ::std::fill(this->m_bits.begin(), this->m_bits.end(), 0);
      }
      return *this;
    }
    ::tools::dynamic_bitset& operator>>=(const ::std::size_t pos) {
      const ::std::size_t diff = pos / W;
      if (diff < this->m_bits.size()) {
        if (pos % W > 0) {
          for (::std::size_t i = diff; i < this->m_bits.size(); ++i) {
            this->m_bits[i] >>= pos % W;
            if (i + 1 < this->m_bits.size()) {
              this->m_bits[i] |= this->m_bits[i + 1] << (W - pos % W);
            }
          }
        }
        if (diff > 0) {
          for (::std::size_t i = diff; i < this->m_bits.size(); ++i) {
            this->m_bits[i - diff] = this->m_bits[i];
          }
          ::std::fill(::std::next(this->m_bits.begin(), this->m_bits.size() - diff), this->m_bits.end(), 0);
        }
      } else {
        ::std::fill(this->m_bits.begin(), this->m_bits.end(), 0);
      }
      return *this;
    }
    ::tools::dynamic_bitset& set() {
      ::std::fill(this->m_bits.begin(), this->m_bits.end(), ::std::numeric_limits<::std::uint64_t>::max());
      if (this->m_size % W > 0) {
        this->m_bits.back() &= (UINT64_C(1) << (this->m_size % W)) - 1;
      }
      return *this;
    }
    ::tools::dynamic_bitset& set(const ::std::size_t pos) {
      assert(pos < this->m_size);
      this->m_bits[pos / W] |= UINT64_C(1) << (pos % W);
      return *this;
    }
    ::tools::dynamic_bitset& set(const ::std::size_t pos, const bool val) {
      return val ? this->set(pos) : this->reset(pos);
    }
    ::tools::dynamic_bitset& reset() {
      ::std::fill(this->m_bits.begin(), this->m_bits.end(), 0);
      return *this;
    }
    ::tools::dynamic_bitset& reset(const ::std::size_t pos) {
      assert(pos < this->m_size);
      this->m_bits[pos / W] &= ~(UINT64_C(1) << (pos % W));
      return *this;
    }
    ::tools::dynamic_bitset operator~() const {
      return ::tools::dynamic_bitset(*this).flip();
    }
    ::tools::dynamic_bitset& flip() {
      for (::std::size_t i = 0; i < this->m_bits.size(); ++i) {
        this->m_bits[i] = ~this->m_bits[i];
      }
      if (this->m_size % W > 0) {
        this->m_bits.back() &= (UINT64_C(1) << (this->m_size % W)) - 1;
      }
      return *this;
    }
    ::tools::dynamic_bitset& flip(const ::std::size_t pos) {
      assert(pos < this->m_size);
      this->m_bits[pos / W] ^= UINT64_C(1) << (pos % W);
      return *this;
    }
    reference operator[](const ::std::size_t pos) {
      return reference(this, pos);
    }
    bool operator[](const ::std::size_t pos) const {
      return this->test(pos);
    }
    ::std::size_t count() const {
      ::std::size_t result = 0;
      for (::std::size_t i = 0; i < this->m_bits.size(); ++i) {
        result += ::std::popcount(this->m_bits[i]);
      }
      return result;
    }
    ::std::size_t size() const {
      return this->m_size;
    }
    bool test(const ::std::size_t pos) const {
      assert(pos < this->m_size);
      return (this->m_bits[pos / W] >> (pos % W)) & 1;
    }
    bool all() const {
      if (this->m_size % W > 0) {
        for (::std::size_t i = 0; i + 1 < this->m_bits.size(); ++i) {
          if (this->m_bits[i] != ::std::numeric_limits<::std::uint64_t>::max()) {
            return false;
          }
        }
        return this->m_bits.back() == (UINT64_C(1) << (this->m_size % W)) - 1;
      } else {
        for (::std::size_t i = 0; i < this->m_bits.size(); ++i) {
          if (this->m_bits[i] != ::std::numeric_limits<::std::uint64_t>::max()) {
            return false;
          }
        }
        return true;
      }
    }
    bool any() const {
      for (::std::size_t i = 0; i < this->m_bits.size(); ++i) {
        if (this->m_bits[i] != 0) {
          return true;
        }
      }
      return false;
    }
    bool none() const {
      return !this->any();
    }
    ::std::string to_string() const {
      ::std::ostringstream oss;
      oss << *this;
      return oss.str();
    }
    friend bool operator==(const ::tools::dynamic_bitset& lhs, const ::tools::dynamic_bitset& rhs) {
      return lhs.m_size == rhs.m_size && lhs.m_bits == rhs.m_bits;
    }
    friend bool operator!=(const ::tools::dynamic_bitset& lhs, const ::tools::dynamic_bitset& rhs) {
      return !(lhs == rhs);
    }
    ::tools::dynamic_bitset operator<<(const ::std::size_t pos) const {
      return ::tools::dynamic_bitset(*this) <<= pos;
    }
    ::tools::dynamic_bitset operator>>(const ::std::size_t pos) const {
      return ::tools::dynamic_bitset(*this) >>= pos;
    }
    friend ::tools::dynamic_bitset operator&(const ::tools::dynamic_bitset& lhs, const ::tools::dynamic_bitset& rhs) {
      return ::tools::dynamic_bitset(lhs) &= rhs;
    }
    friend ::tools::dynamic_bitset operator|(const ::tools::dynamic_bitset& lhs, const ::tools::dynamic_bitset& rhs) {
      return ::tools::dynamic_bitset(lhs) |= rhs;
    }
    friend ::tools::dynamic_bitset operator^(const ::tools::dynamic_bitset& lhs, const ::tools::dynamic_bitset& rhs) {
      return ::tools::dynamic_bitset(lhs) ^= rhs;
    }
    friend ::std::istream& operator>>(::std::istream& is, ::tools::dynamic_bitset& self) {
      ::std::string s;
      is >> s;
      self = ::tools::dynamic_bitset(s);
      return is;
    }
    friend ::std::ostream& operator<<(::std::ostream& os, const ::tools::dynamic_bitset& self) {
      for (::std::size_t i = self.m_bits.size(); i --> 0;) {
        for (::std::size_t j = i + 1 < self.m_bits.size() ? W : (self.m_size - 1) % W + 1; j --> 0;) {
          os << ((self.m_bits[i] >> j) & 1);
        }
      }
      return os;
    }
    bool empty() const {
      return this->m_size == 0;
    }
    void resize(const ::std::size_t size) {
      this->m_size = size;
      this->m_bits.resize(::tools::ceil(size, W));
      if (size % W > 0) {
        this->m_bits.back() &= (UINT64_C(1) << (size % W)) - 1;
      }
    }
    void shrink_to_fit() {
      this->m_bits.shrink_to_fit();
    }
  private:
    ::std::size_t Find_first(const ::std::size_t offset) const {
      for (::std::size_t i = offset; i < this->m_bits.size(); ++i) {
        if (this->m_bits[i] > 0) {
          return i * W + ::std::countr_zero(this->m_bits[i]);
        }
      }
      return this->m_size;
    }
  public:
    ::std::size_t Find_first() const {
      return this->Find_first(0);
    }
    ::std::size_t Find_next(const ::std::size_t pos) const {
      assert(pos < this->m_size);

      if (pos % W == W - 1) return this->Find_first((pos + 1) / W);
      if (const auto x = this->m_bits[pos / W] >> (pos % W + 1); x > 0) return pos + ::std::countr_zero(x) + 1;
      return this->Find_first(pos / W + 1);
    }
  };
}


#line 1 "tools/matrix.hpp"



#line 1 "tools/vector.hpp"



#line 14 "tools/vector.hpp"
#include <cmath>
#line 17 "tools/vector.hpp"
#include <functional>
#include <tuple>
#line 1 "tools/abs.hpp"



namespace tools {
  constexpr float abs(const float x) {
    return x < 0 ? -x : x;
  }
  constexpr double abs(const double x) {
    return x < 0 ? -x : x;
  }
  constexpr long double abs(const long double x) {
    return x < 0 ? -x : x;
  }
  constexpr int abs(const int x) {
    return x < 0 ? -x : x;
  }
  constexpr long abs(const long x) {
    return x < 0 ? -x : x;
  }
  constexpr long long abs(const long long x) {
    return x < 0 ? -x : x;
  }
  constexpr unsigned int abs(const unsigned int x) {
    return x;
  }
  constexpr unsigned long abs(const unsigned long x) {
    return x;
  }
  constexpr unsigned long long abs(const unsigned long long x) {
    return x;
  }
}


#line 1 "tools/tuple_hash.hpp"



#line 1 "tools/now.hpp"



#include <chrono>

namespace tools {
  inline long long now() {
    return ::std::chrono::duration_cast<::std::chrono::nanoseconds>(::std::chrono::high_resolution_clock::now().time_since_epoch()).count();
  }
}


#line 1 "tools/hash_combine.hpp"



#line 6 "tools/hash_combine.hpp"

// Source: https://github.com/google/cityhash/blob/f5dc54147fcce12cefd16548c8e760d68ac04226/src/city.h
// License: MIT
// Author: Google Inc.

// Copyright (c) 2011 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

namespace tools {
  template <typename T>
  void hash_combine(::std::size_t& seed, const T& v) {
    static const ::std::hash<T> hasher;
    static constexpr ::std::size_t k_mul = 0x9ddfea08eb382d69ULL;
    ::std::size_t a = (hasher(v) ^ seed) * k_mul;
    a ^= (a >> 47);
    ::std::size_t b = (seed ^ a) * k_mul;
    b ^= (b >> 47);
    seed = b * k_mul;
  }
}


#line 11 "tools/tuple_hash.hpp"

namespace tools {
  template <typename... Ts>
  struct tuple_hash {
    template <::std::size_t I = sizeof...(Ts) - 1>
    ::std::size_t operator()(const ::std::tuple<Ts...>& key) const {
      if constexpr (I == ::std::numeric_limits<::std::size_t>::max()) {
        static const ::std::size_t seed = ::tools::now();
        return seed;
      } else {
        ::std::size_t seed = this->operator()<I - 1>(key);
        ::tools::hash_combine(seed, ::std::get<I>(key));
        return seed;
      }
    }
  };
}


#line 21 "tools/vector.hpp"

namespace tools {
  namespace detail {
    namespace vector {
      template <typename T, ::std::size_t N>
      class members {
      protected:
        constexpr static bool variable_sized = false;
        constexpr static bool has_aliases = false;
        ::std::array<T, N> m_values;
        members() : m_values() {}
        members(const ::std::initializer_list<T> il) : m_values(il) {
          assert(il.size() == N);
        }
      };

      template <typename T>
      class members<T, 2> {
      protected:
        constexpr static bool variable_sized = false;
        constexpr static bool has_aliases = true;
        ::std::array<T*, 2> m_values;
        members() : m_values{&this->x, &this->y} {}
        members(const T& x, const T& y) : m_values{&this->x, &this->y}, x(x), y(y) {}
        members(const ::std::initializer_list<T> il) : m_values{&this->x, &this->y}, x(il.begin()[0]), y(il.begin()[1]) {
          assert(il.size() == 2);
        }

      public:
        T x;
        T y;
      };

      template <typename T>
      class members<T, 3> {
      protected:
        constexpr static bool variable_sized = false;
        constexpr static bool has_aliases = true;
        ::std::array<T*, 3> m_values;
        members() : m_values{&this->x, &this->y, &this->z} {}
        members(const T& x, const T& y, const T& z) : m_values{&this->x, &this->y, &this->z}, x(x), y(y), z(z) {}
        members(const ::std::initializer_list<T> il) : m_values{&this->x, &this->y, &this->z}, x(il.begin()[0]), y(il.begin()[1]), z(il.begin()[2]) {
          assert(il.size() == 3);
        }

      public:
        T x;
        T y;
        T z;
      };

      template <typename T>
      class members<T, 4> {
      protected:
        constexpr static bool variable_sized = false;
        constexpr static bool has_aliases = true;
        ::std::array<T*, 4> m_values;
        members() : m_values{&this->x, &this->y, &this->z, &this->w} {}
        members(const T& x, const T& y, const T& z, const T& w) : m_values{&this->x, &this->y, &this->z, &this->w}, x(x), y(y), z(z), w(w) {}
        members(const ::std::initializer_list<T> il) : m_values{&this->x, &this->y, &this->z, &this->w}, x(il.begin()[0]), y(il.begin()[1]), z(il.begin()[2]), w(il.begin()[3]) {
          assert(il.size() == 4);
        }

      public:
        T x;
        T y;
        T z;
        T w;
      };

      template <typename T>
      class members<T, ::std::numeric_limits<::std::size_t>::max()> {
      protected:
        constexpr static bool variable_sized = true;
        constexpr static bool has_aliases = false;
        ::std::vector<T> m_values;
        members() = default;
        members(const ::std::size_t n) : m_values(n) {}
        members(const ::std::size_t n, const T& value) : m_values(n, value) {}
        template <typename InputIter>
        members(const InputIter first, const InputIter last) : m_values(first, last) {}
        members(const ::std::initializer_list<T> il) : m_values(il) {}
      };
    }
  }

  template <typename T, ::std::size_t N = ::std::numeric_limits<::std::size_t>::max()>
  class vector : public ::tools::detail::vector::members<T, N> {
  private:
    using Base = ::tools::detail::vector::members<T, N>;
    using F = ::std::conditional_t<::std::is_floating_point_v<T>, T, double>;
    using V = ::tools::vector<T, N>;
    constexpr static bool variable_sized = Base::variable_sized;
    constexpr static bool has_aliases = Base::has_aliases;

  public:
    using reference = T&;
    using const_reference = const T&;
    using size_type = ::std::size_t;
    using difference_type = ::std::ptrdiff_t;
    using pointer = T*;
    using const_pointer = const T*;
    using value_type = T;
    class iterator {
    private:
      V* m_parent;
      size_type m_i;

    public:
      using difference_type = ::std::ptrdiff_t;
      using value_type = T;
      using reference = T&;
      using pointer = T*;
      using iterator_category = ::std::random_access_iterator_tag;

      iterator() = default;
      iterator(const iterator&) = default;
      iterator(iterator&&) = default;
      ~iterator() = default;
      iterator& operator=(const iterator&) = default;
      iterator& operator=(iterator&&) = default;

      iterator(V * const parent, const size_type i) : m_parent(parent), m_i(i) {}

      reference operator*() const {
        return (*this->m_parent)[this->m_i];
      }
      pointer operator->() const {
        return &(*(*this));
      }

      iterator& operator++() {
        ++this->m_i;
        return *this;
      }
      iterator operator++(int) {
        const auto self = *this;
        ++*this;
        return self;
      }
      iterator& operator--() {
        --this->m_i;
        return *this;
      }
      iterator operator--(int) {
        const auto self = *this;
        --*this;
        return self;
      }
      iterator& operator+=(const difference_type n) {
        this->m_i += n;
        return *this;
      }
      iterator& operator-=(const difference_type n) {
        this->m_i -= n;
        return *this;
      }
      friend iterator operator+(const iterator& self, const difference_type n) {
        return iterator(self) += n;
      }
      friend iterator operator+(const difference_type n, const iterator& self) {
        return iterator(self) += n;
      }
      friend iterator operator-(const iterator& self, const difference_type n) {
        return iterator(self) -= n;
      }
      friend difference_type operator-(const iterator& lhs, const iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return static_cast<difference_type>(lhs.m_i) - static_cast<difference_type>(rhs.m_i);
      }
      reference operator[](const difference_type n) const {
        return *(*this + n);
      }

      friend bool operator==(const iterator& lhs, const iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i == rhs.m_i;
      }
      friend bool operator!=(const iterator& lhs, const iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i != rhs.m_i;
      }
      friend bool operator<(const iterator& lhs, const iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i < rhs.m_i;
      }
      friend bool operator<=(const iterator& lhs, const iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i <= rhs.m_i;
      }
      friend bool operator>(const iterator& lhs, const iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i > rhs.m_i;
      }
      friend bool operator>=(const iterator& lhs, const iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i >= rhs.m_i;
      }
    };
    class const_iterator {
    private:
      const V *m_parent;
      size_type m_i;

    public:
      using difference_type = ::std::ptrdiff_t;
      using value_type = T;
      using reference = const T&;
      using pointer = const T*;
      using iterator_category = ::std::random_access_iterator_tag;

      const_iterator() = default;
      const_iterator(const const_iterator&) = default;
      const_iterator(const_iterator&&) = default;
      ~const_iterator() = default;
      const_iterator& operator=(const const_iterator&) = default;
      const_iterator& operator=(const_iterator&&) = default;

      const_iterator(const V * const parent, const size_type i) : m_parent(parent), m_i(i) {}

      reference operator*() const {
        return (*this->m_parent)[this->m_i];
      }
      pointer operator->() const {
        return &(*(*this));
      }

      const_iterator& operator++() {
        ++this->m_i;
        return *this;
      }
      const_iterator operator++(int) {
        const auto self = *this;
        ++*this;
        return self;
      }
      const_iterator& operator--() {
        --this->m_i;
        return *this;
      }
      const_iterator operator--(int) {
        const auto self = *this;
        --*this;
        return self;
      }
      const_iterator& operator+=(const difference_type n) {
        this->m_i += n;
        return *this;
      }
      const_iterator& operator-=(const difference_type n) {
        this->m_i -= n;
        return *this;
      }
      friend const_iterator operator+(const const_iterator& self, const difference_type n) {
        return const_iterator(self) += n;
      }
      friend const_iterator operator+(const difference_type n, const const_iterator& self) {
        return const_iterator(self) += n;
      }
      friend const_iterator operator-(const const_iterator& self, const difference_type n) {
        return const_iterator(self) -= n;
      }
      friend difference_type operator-(const const_iterator& lhs, const const_iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return static_cast<difference_type>(lhs.m_i) - static_cast<difference_type>(rhs.m_i);
      }
      reference operator[](const difference_type n) const {
        return *(*this + n);
      }

      friend bool operator==(const const_iterator& lhs, const const_iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i == rhs.m_i;
      }
      friend bool operator!=(const const_iterator& lhs, const const_iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i != rhs.m_i;
      }
      friend bool operator<(const const_iterator& lhs, const const_iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i < rhs.m_i;
      }
      friend bool operator<=(const const_iterator& lhs, const const_iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i <= rhs.m_i;
      }
      friend bool operator>(const const_iterator& lhs, const const_iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i > rhs.m_i;
      }
      friend bool operator>=(const const_iterator& lhs, const const_iterator& rhs) {
        assert(lhs.m_parent == rhs.m_parent);
        return lhs.m_i >= rhs.m_i;
      }
    };
    using reverse_iterator = ::std::reverse_iterator<iterator>;
    using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;

    vector() = default;
    vector(const V& other) : Base() {
      if constexpr (has_aliases) {
        ::std::copy(other.begin(), other.end(), this->begin());
      } else {
        this->m_values = other.m_values;
      }
    }
    vector(V&& other) noexcept {
      if constexpr (has_aliases) {
        ::std::copy(other.begin(), other.end(), this->begin());
      } else {
        this->m_values = ::std::move(other.m_values);
      }
    }
    ~vector() = default;
    V& operator=(const V& other) {
      if constexpr (has_aliases) {
        ::std::copy(other.begin(), other.end(), this->begin());
      } else {
        this->m_values = other.m_values;
      }
      return *this;
    }
    V& operator=(V&& other) noexcept {
      if constexpr (has_aliases) {
        ::std::copy(other.begin(), other.end(), this->begin());
      } else {
        this->m_values = ::std::move(other.m_values);
      }
      return *this;
    }

    template <bool SFINAE = variable_sized, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    explicit vector(size_type n) : Base(n) {}
    template <bool SFINAE = variable_sized, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    vector(size_type n, const_reference value) : Base(n, value) {}
    template <typename InputIter, bool SFINAE = variable_sized, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    vector(const InputIter first, const InputIter last) : Base(first, last) {}
    template <bool SFINAE = N == 2, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    vector(const T& x, const T& y) : Base(x, y) {}
    template <bool SFINAE = N == 3, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    vector(const T& x, const T& y, const T& z) : Base(x, y, z) {}
    template <bool SFINAE = N == 4, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    vector(const T& x, const T& y, const T& z, const T& w) : Base(x, y, z, w) {}
    vector(const ::std::initializer_list<T> il) : Base(il) {}

    iterator begin() noexcept { return iterator(this, 0); }
    const_iterator begin() const noexcept { return const_iterator(this, 0); }
    const_iterator cbegin() const noexcept { return const_iterator(this, 0); }
    iterator end() noexcept { return iterator(this, this->size()); }
    const_iterator end() const noexcept { return const_iterator(this, this->size()); }
    const_iterator cend() const noexcept { return const_iterator(this, this->size()); }
    reverse_iterator rbegin() noexcept { return ::std::make_reverse_iterator(this->end()); }
    const_reverse_iterator rbegin() const noexcept { return ::std::make_reverse_iterator(this->end()); }
    const_reverse_iterator crbegin() const noexcept { return ::std::make_reverse_iterator(this->cend()); }
    reverse_iterator rend() noexcept { return ::std::make_reverse_iterator(this->begin()); }
    const_reverse_iterator rend() const noexcept { return ::std::make_reverse_iterator(this->begin()); }
    const_reverse_iterator crend() const noexcept { return ::std::make_reverse_iterator(this->cbegin()); }

    size_type size() const noexcept { return this->m_values.size(); }
    bool empty() const noexcept { return this->m_values.empty(); }

    reference operator[](const size_type n) {
      if constexpr (has_aliases) {
        return *this->m_values[n];
      } else {
        return this->m_values[n];
      }
    }
    const_reference operator[](const size_type n) const {
      if constexpr (has_aliases) {
        return *this->m_values[n];
      } else {
        return this->m_values[n];
      }
    }
    reference front() { return *this->begin(); }
    const_reference front() const { return *this->begin(); }
    reference back() { return *this->rbegin(); }
    const_reference back() const { return *this->rbegin(); }

    V operator+() const {
      return *this;
    }
    V operator-() const {
      V res = *this;
      for (auto& v : res) v = -v;
      return res;
    }
    V& operator+=(const V& other) {
      assert(this->size() == other.size());
      for (::std::size_t i = 0; i < this->size(); ++i) {
        (*this)[i] += other[i];
      }
      return *this;
    }
    friend V operator+(const V& lhs, const V& rhs) {
      return V(lhs) += rhs;
    }
    V& operator-=(const V& other) {
      assert(this->size() == other.size());
      for (::std::size_t i = 0; i < this->size(); ++i) {
        (*this)[i] -= other[i];
      }
      return *this;
    }
    friend V operator-(const V& lhs, const V& rhs) {
      return V(lhs) -= rhs;
    }
    V& operator*=(const T& c) {
      for (auto& v : *this) v *= c;
      return *this;
    }
    friend V operator*(const T& lhs, const V& rhs) {
      return V(rhs) *= lhs;
    }
    friend V operator*(const V& lhs, const T& rhs) {
      return V(lhs) *= rhs;
    }
    V& operator/=(const T& c) {
      for (auto& v : *this) v /= c;
      return *this;
    }
    friend V operator/(const V& lhs, const T& rhs) {
      return V(lhs) /= rhs;
    }

    friend bool operator==(const V& lhs, const V& rhs) {
      return ::std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
    }
    friend bool operator!=(const V& lhs, const V& rhs) {
      return !(lhs == rhs);
    }

    T inner_product(const V& other) const {
      assert(this->size() == other.size());
      T res{};
      for (::std::size_t i = 0; i < this->size(); ++i) {
        res += (*this)[i] * other[i];
      }
      return res;
    }
    T l1_norm() const {
      T res{};
      for (const auto& v : *this) {
        res += ::tools::abs(v);
      }
      return res;
    }
    T squared_l2_norm() const {
      return this->inner_product(*this);
    }
    F l2_norm() const {
      return ::std::sqrt(static_cast<F>(this->squared_l2_norm()));
    }
    template <bool SFINAE = ::std::is_floating_point_v<T>, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    V normalized() const {
      return *this / this->l2_norm();
    }

    friend ::std::ostream& operator<<(::std::ostream& os, const V& self) {
      os << '(';
      ::std::string delimiter = "";
      for (const auto& v : self) {
        os << delimiter << v;
        delimiter = ", ";
      }
      return os << ')';
    }
    friend ::std::istream& operator>>(::std::istream& is, V& self) {
      for (auto& v : self) {
        is >> v;
      }
      return is;
    }

    template <bool SFINAE = N == 2, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    T outer_product(const V& other) const {
      return this->x * other.y - this->y * other.x;
    }
    template <bool SFINAE = N == 2, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    V turned90() const {
      return V{-this->y, this->x};
    }
    template <bool SFINAE = N == 2, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    V turned270() const {
      return V{this->y, -this->x};
    }

    template <bool SFINAE = N == 2, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    static const ::std::array<V, 4>& four_directions() {
      static const ::std::array<V, 4> res = {
        V{T(1), T(0)},
        V{T(0), T(1)},
        V{T(-1), T(0)},
        V{T(0), T(-1)}
      };
      return res;
    }
    template <bool SFINAE = N == 2, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    static const ::std::array<V, 8>& eight_directions() {
      static const ::std::array<V, 8> res = {
        V{T(1), T(0)},
        V{T(1), T(1)},
        V{T(0), T(1)},
        V{T(-1), T(1)},
        V{T(-1), T(0)},
        V{T(-1), T(-1)},
        V{T(0), T(-1)},
        V{T(1), T(-1)}
      };
      return res;
    }

    template <bool SFINAE = N == 3, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    V outer_product(const V& other) const {
      return V{this->y * other.z - this->z * other.y, this->z * other.x - this->x * other.z, this->x * other.y - this->y * other.x};
    }
    template <bool SFINAE = N == 3 && ::std::is_floating_point_v<T>, ::std::enable_if_t<SFINAE, ::std::nullptr_t> = nullptr>
    ::std::array<V, 3> orthonormal_basis() const {
      assert((*this != V{0, 0, 0}));

      ::std::array<V, 3> v;
      v[0] = *this;
      v[1] = V{0, this->z, -this->y};
      if (v[1] == V{0, 0, 0}) {
        v[1] = V{-this->z, 0, this->x};
      }
      v[1] -= v[0].inner_product(v[1]) / v[0].inner_product(v[0]) * v[0];

      v[0] = v[0].normalized();
      v[1] = v[1].normalized();
      v[2] = v[0].outer_product(v[1]);

      return v;
    }
  };
}

namespace std {
  template <typename T>
  struct hash<::tools::vector<T, 2>> {
    using result_type = ::std::size_t;
    using argument_type = ::tools::vector<T, 2>;
    result_type operator()(const argument_type& key) const {
      static const ::tools::tuple_hash<T, T> hasher;
      return hasher(::std::make_tuple(key.x, key.y));
    }
  };
  template <typename T>
  struct hash<::tools::vector<T, 3>> {
    using result_type = ::std::size_t;
    using argument_type = ::tools::vector<T, 3>;
    result_type operator()(const argument_type& key) const {
      static const ::tools::tuple_hash<T, T, T> hasher;
      return hasher(::std::make_tuple(key.x, key.y, key.z));
    }
  };
  template <typename T>
  struct hash<::tools::vector<T, 4>> {
    using result_type = ::std::size_t;
    using argument_type = ::tools::vector<T, 4>;
    result_type operator()(const argument_type& key) const {
      static const ::tools::tuple_hash<T, T, T, T> hasher;
      return hasher(::std::make_tuple(key.x, key.y, key.z, key.w));
    }
  };
}


#line 18 "tools/matrix.hpp"

namespace tools {
  namespace detail {
    namespace matrix {
      template <typename T, int N, int M>
      class members {
        static_assert(N >= 0);
        static_assert(M >= 0);

      protected:
        constexpr static bool variable_sized = false;
        ::std::array<T, N * M> m_values;
        members() : m_values() {}
      };
      template <typename T>
      class members<T, -1, -1> {
      protected:
        constexpr static bool variable_sized = true;
        ::std::vector<T> m_values;
        int m_rows;
        int m_cols;
        members() = default;
        members(const int rows, const int cols) : m_values(rows * cols), m_rows(rows), m_cols(cols) {
          assert(rows >= 0);
          assert(cols >= 0);
        }
        members(const int rows, const int cols, const T& value) : m_values(rows * cols, value), m_rows(rows), m_cols(cols) {
          assert(rows >= 0);
          assert(cols >= 0);
        }
      };
    }
  }

  template <typename T, int N = -1, int M = -1>
  class matrix : ::tools::detail::matrix::members<T, N, M> {
    template <typename, int, int>
    friend class ::tools::matrix;
    using Mat = ::tools::matrix<T, N, M>;
    using Base = ::tools::detail::matrix::members<T, N, M>;
    constexpr static bool variable_sized = Base::variable_sized;

  public:
    matrix() = default;
    template <bool SFINAE = variable_sized> requires (SFINAE)
    matrix(const int rows, const int cols) : Base(rows, cols) {}
    template <bool SFINAE = variable_sized> requires (SFINAE)
    matrix(const int rows, const int cols, const T& value) : Base(rows, cols, value) {}
    template <bool SFINAE = !variable_sized> requires (SFINAE)
    matrix(const ::std::initializer_list<::std::initializer_list<T>> il) {
      assert(il.size() == this->rows());
      assert(::std::ranges::all_of(il, [&](const auto& row) { return ::std::ssize(row) == this->cols(); }));
      for (int r = 0; r < this->rows(); ++r) {
        ::std::ranges::copy(il.begin()[r], (*this)[r]);
      }
    }
    template <bool SFINAE = variable_sized, ::std::nullptr_t = nullptr> requires (SFINAE)
    matrix(const ::std::initializer_list<::std::initializer_list<T>> il) : Base(il.size(), il.size() ? il.begin()->size() : 0) {
      assert(::std::ranges::all_of(il, [&](const auto& row) { return ::std::ssize(row) == this->cols(); }));
      for (int r = 0; r < this->rows(); ++r) {
        ::std::ranges::copy(il.begin()[r], (*this)[r]);
      }
    }
    auto operator[](const int r) {
      assert(0 <= r && r < this->rows());
      return this->m_values.begin() + r * this->cols();
    }
    auto operator[](const int r) const {
      assert(0 <= r && r < this->rows());
      return this->m_values.begin() + r * this->cols();
    }

    int rows() const {
      if constexpr (variable_sized) {
        return this->m_rows;
      } else {
        return N;
      }
    }
    int cols() const {
      if constexpr (variable_sized) {
        return this->m_cols;
      } else {
        return M;
      }
    }

    Mat operator+() const {
      return *this;
    }
    Mat operator-() const {
      return Mat(*this) *= T(-1);
    }
    friend Mat operator+(const Mat& lhs, const Mat& rhs) {
      return Mat(lhs) += rhs;
    }
    friend Mat operator-(const Mat& lhs, const Mat& rhs) {
      return Mat(lhs) -= rhs;
    }
    template <int K> requires (!Mat::variable_sized || K == -1)
    friend ::tools::matrix<T, N, K> operator*(const Mat& lhs, const ::tools::matrix<T, M, K>& rhs) {
      assert(lhs.cols() == rhs.rows());
      auto result = [&]() {
        if constexpr (Mat::variable_sized) {
          return ::tools::matrix<T>(lhs.rows(), rhs.cols());
        } else {
          return ::tools::matrix<T, N, K>{};
        }
      }();
      for (int i = 0; i < lhs.rows(); ++i) {
        for (int k = 0; k < lhs.cols(); ++k) {
          for (int j = 0; j < rhs.cols(); ++j) {
            result[i][j] += lhs[i][k] * rhs[k][j];
          }
        }
      }
      return result;
    }
    friend ::tools::vector<T, N> operator*(const Mat& lhs, const ::tools::vector<T, M>& rhs) {
      assert(lhs.cols() == rhs.size());
      auto result = [&]() {
        if constexpr (Mat::variable_sized) {
          return ::tools::vector<T>(lhs.rows());
        } else {
          return ::tools::vector<T, N>{};
        }
      }();
      for (int i = 0; i < lhs.rows(); ++i) {
        for (int j = 0; j < lhs.cols(); ++j) {
          result[i] += lhs[i][j] * rhs[j];
        }
      }
      return result;
    }
    friend Mat operator*(const Mat& lhs, const T& rhs) {
      return Mat(lhs) *= rhs;
    }
    friend Mat operator/(const Mat& lhs, const ::tools::matrix<T, M, M>& rhs) {
      const auto inv = rhs.inv();
      assert(inv);
      return lhs * *inv;
    }
    friend Mat operator/(const Mat& lhs, const T& rhs) {
      return Mat(lhs) /= rhs;
    }
    Mat& operator+=(const Mat& other) {
      assert(this->rows() == other.rows());
      assert(this->cols() == other.cols());
      for (::std::size_t i = 0; i < this->m_values.size(); ++i) {
        this->m_values[i] += other.m_values[i];
      }
      return *this;
    }
    Mat& operator-=(const Mat& other) {
      assert(this->rows() == other.rows());
      assert(this->cols() == other.cols());
      for (::std::size_t i = 0; i < this->m_values.size(); ++i) {
        this->m_values[i] -= other.m_values[i];
      }
      return *this;
    }
    Mat& operator*=(const ::tools::matrix<T, M, M>& other) {
      return *this = *this * other;
    }
    Mat& operator*=(const T& c) {
      for (auto& v : this->m_values) v *= c;
      return *this;
    }
    Mat& operator/=(const ::tools::matrix<T, M, M>& other) {
      return *this = *this / other;
    }
    Mat& operator/=(const T& c) {
      return *this *= T(1) / c;
    }
    friend bool operator==(const Mat& lhs, const Mat& rhs) {
      if constexpr (variable_sized) {
        if (lhs.rows() != rhs.rows()) return false;
        if (lhs.cols() != rhs.cols()) return false;
      }
      return lhs.m_values == rhs.m_values;
    }
    friend bool operator!=(const Mat& lhs, const Mat& rhs) {
      return !(lhs == rhs);
    }

    friend ::std::istream& operator>>(::std::istream& is, Mat& self) {
      for (auto& v : self.m_values) is >> v;
      return is;
    }
    friend ::std::ostream& operator<<(::std::ostream& os, const Mat& self) {
      for (int r = 0; r < self.rows(); ++r) {
        os << '[';
        ::std::string delimiter = "";
        for (int c = 0; c < self.cols(); ++c) {
          os << delimiter << self[r][c];
          delimiter = ", ";
        }
        os << ']' << '\n';
      }
      return os;
    }

  private:
    ::std::pair<int, T> internal_gauss_jordan() {
      int rank = 0;
      T coeff(1);

      for (int c = 0; c < this->cols(); ++c) {
        int pivot;
        for (pivot = rank; pivot < this->rows() && (*this)[pivot][c] == T(0); ++pivot);
        if (pivot == this->rows()) continue;

        if (pivot != rank) {
          for (int cc = c; cc < this->cols(); ++cc) {
            ::std::swap((*this)[rank][cc], (*this)[pivot][cc]);
          }
          coeff *= T(-1);
        }

        {
          const T scale_inv = T(1) / (*this)[rank][c];
          for (int cc = c; cc < this->cols(); ++cc) {
            (*this)[rank][cc] *= scale_inv;
          }
          coeff *= scale_inv;
        }

        for (int r = 0; r < this->rows(); ++r) {
          if (r == rank) continue;
          const T scale = (*this)[r][c];
          if (scale == T(0)) continue;
          for (int cc = c; cc < this->cols(); ++cc) {
            (*this)[r][cc] -= (*this)[rank][cc] * scale;
          }
        }

        ++rank;
      }

      return ::std::make_pair(rank, coeff);
    }

  public:
    int gauss_jordan() {
      return this->internal_gauss_jordan().first;
    }

    int rank() const {
      return (this->rows() < this->cols() ? this->transposed() : Mat(*this)).gauss_jordan();
    }

    ::tools::matrix<T> solve(const ::tools::vector<T, N>& b) const {
      assert(this->rows() == b.size());
      assert(this->cols() >= 1);
      auto Ab = [&]() {
        if constexpr (variable_sized) {
          return Mat(this->rows(), this->cols() + 1);
        } else {
          return ::tools::matrix<T, N, M + 1>{};
        }
      }();
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          Ab[r][c] = (*this)[r][c];
        }
        Ab[r][this->cols()] = b[r];
      }

      Ab.internal_gauss_jordan();

      ::std::vector<int> ranks(Ab.cols());
      for (int r = 0, cl = 0, cr = 0; r <= Ab.rows(); ++r, cl = cr) {
        for (; cr < Ab.cols() && (r == Ab.rows() || Ab[r][cr] == T(0)); ++cr);
        for (int c = cl; c < cr; ++c) {
          ranks[c] = r;
        }
      }

      if (ranks[Ab.cols() - 2] < ranks[Ab.cols() - 1]) {
        return ::tools::matrix<T>(this->rows(), 0);
      }

      ::std::vector<::tools::vector<T>> answers(this->cols());
      int free = this->cols() - ranks.back() - 1;

      for (int l = this->cols(), r = this->cols(); r > 0; r = l) {
        for (; l > 0 && ranks[l - 1] == ranks[r - 1]; --l);
        for (int c = r - 1; c > l; --c) {
          answers[c] = ::tools::vector<T>(this->cols() - ranks.back() + 1, T(0));
          answers[c][free] = T(1);
          --free;
        }
        if (ranks[l] > 0) {
          answers[l] = ::tools::vector<T>(this->cols() - ranks.back() + 1, T(0));
          answers[l][this->cols() - ranks.back()] = Ab[ranks[l] - 1][Ab.cols() - 1];
          for (int c = l + 1; c < Ab.cols() - 1; ++c) {
            answers[l] -= Ab[ranks[l] - 1][c] * answers[c];
          }
        } else {
          answers[l] = ::tools::vector<T>(this->cols() - ranks.back() + 1, T(0));
          answers[l][free] = T(1);
          --free;
        }
      }

      ::tools::matrix<T> answer(this->cols(), this->cols() - ranks.back() + 1);
      for (int r = 0; r < this->cols(); ++r) {
        for (int c = 0; c < this->cols() - ranks.back() + 1; ++c) {
          answer[r][c] = answers[r][c];
        }
      }

      return answer;
    }

    T determinant() const {
      assert(this->rows() == this->cols());

      auto A = *this;
      const auto [rank, coeff] = A.internal_gauss_jordan();

      return rank == A.rows() ? T(1) / coeff : T(0);
    }

    template <bool SFINAE = !variable_sized && N == M> requires (SFINAE)
    static Mat e() {
      Mat result{};
      for (int i = 0; i < N; ++i) {
        result[i][i] = T(1);
      }
      return result;
    }
    template <bool SFINAE = variable_sized> requires (SFINAE)
    static Mat e(const int n) {
      assert(n >= 0);
      Mat result(n, n, T(0));
      for (int i = 0; i < n; ++i) {
        result[i][i] = T(1);
      }
      return result;
    }

    template <bool SFINAE = variable_sized || N == M> requires (SFINAE)
    ::std::optional<Mat> inv() const {
      assert(this->rows() == this->cols());

      auto AI = [&]() {
        if constexpr (variable_sized) {
          return Mat(this->rows(), this->cols() * 2);
        } else {
          return ::tools::matrix<T, N, M * 2>{};
        }
      }();
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          AI[r][c] = (*this)[r][c];
        }
        for (int c = this->cols(); c < AI.cols(); ++c) {
          AI[r][c] = T(0);
        }
        AI[r][this->cols() + r] = T(1);
      }

      AI.internal_gauss_jordan();
      for (int i = 0; i < this->rows(); ++i) {
        if (AI[i][i] != T(1)) return ::std::nullopt;
      }

      auto B = [&]() {
        if constexpr (variable_sized) {
          return Mat(this->rows(), this->cols());
        } else {
          return Mat{};
        }
      }();
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          B[r][c] = AI[r][this->cols() + c];
        }
      }
      return B;
    }

    ::tools::matrix<T, M, N> transposed() const {
      auto A_T = [&]() {
        if constexpr (variable_sized) {
          return Mat(this->cols(), this->rows());
        } else {
          return ::tools::matrix<T, M, N>{};
        }
      }();
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          A_T[c][r] = (*this)[r][c];
        }
      }
      return A_T;
    }
  };
}


#line 20 "tools/matrix_mod2.hpp"

namespace tools {
  namespace detail {
    namespace matrix {
      template <int N, int M>
      class members<::atcoder::static_modint<2>, N, M> {
      protected:
        constexpr static bool variable_sized = false;
        ::std::array<::std::bitset<M>, N> m_values;
        members() : m_values() {}
      };
      template <>
      class members<::atcoder::static_modint<2>, -1, -1> {
      protected:
        constexpr static bool variable_sized = true;
        ::std::vector<::tools::dynamic_bitset> m_values;
        int m_cols;
        members() = default;
        members(const int rows, const int cols) : m_values(rows, ::tools::dynamic_bitset(cols)), m_cols(cols) {}
        members(const int rows, const int cols, const ::atcoder::static_modint<2> value) : m_values(rows, value.val() ? ::tools::dynamic_bitset(cols).set() : ::tools::dynamic_bitset(cols)), m_cols(cols) {}
      };
    }
  }

  template <int N, int M>
  class matrix<::atcoder::static_modint<2>, N, M> : ::tools::detail::matrix::members<::atcoder::static_modint<2>, N, M> {
    template <typename, int, int>
    friend class ::tools::matrix;
    using T = ::atcoder::static_modint<2>;
    using Mat = ::tools::matrix<T, N, M>;
    using Base = ::tools::detail::matrix::members<T, N, M>;
    constexpr static bool variable_sized = Base::variable_sized;

  public:
    class row_refenrece;
    class reference;
    class const_row_reference;
    class row_reference {
      friend Mat;
      friend class const_row_reference;

      using wrapped = typename ::std::conditional_t<variable_sized, ::std::vector<::tools::dynamic_bitset>, ::std::array<::std::bitset<M>, N>>::iterator;
      wrapped m_it;

      explicit row_reference(const wrapped it) : m_it(it) {
      }

    public:
      row_reference() = default;

      reference operator[](const int c) const {
        assert(0 <= c && c < ::std::ssize(*this->m_it));
        return reference((*this->m_it)[c]);
      }
    };
    class reference {
      friend class row_reference;

      using wrapped = typename ::std::conditional_t<variable_sized, ::tools::dynamic_bitset, ::std::bitset<M>>::reference;
      wrapped m_ref;

      explicit reference(const wrapped ref) : m_ref(ref) {
      }

    public:
      reference() = default;
      reference(const reference&) = default;
      reference& operator=(const T x) {
        this->m_ref = x.val();
        return *this;
      }
      reference& operator=(const reference& other) {
        this->m_ref = other.m_ref;
        return *this;
      }
      operator T() const {
        return T::raw(static_cast<bool>(this->m_ref));
      }

      unsigned int val() const {
        return static_cast<bool>(this->m_ref);
      }
      reference& operator++() {
        this->m_ref = !static_cast<bool>(this->m_ref);
        return *this;
      }
      reference& operator--() {
        return ++*this;
      }
      T operator++(int) {
        const T result = *this;
        ++*this;
        return result;
      }
      T operator--(int) {
        return (*this)++;
      }
      reference& operator+=(const T rhs) {
        if (rhs.val()) ++*this;
        return *this;
      }
      reference& operator+=(const reference& rhs) {
        if (rhs.m_ref) ++*this;
        return *this;
      }
      reference& operator-=(const T rhs) {
        return *this += rhs;
      }
      reference& operator-=(const reference& rhs) {
        return *this += rhs;
      }
      reference& operator*=(const T rhs) {
        if (!rhs.val()) this->m_ref = false;
        return *this;
      }
      reference& operator*=(const reference& rhs) {
        if (!rhs.m_ref) this->m_ref = false;
        return *this;
      }
      reference& operator/=(const T rhs) {
        assert(rhs.val());
        return *this;
      }
      reference& operator/=(const reference& rhs) {
        assert(rhs.m_ref);
        return *this;
      }
      T operator+() const {
        return *this;
      }
      T operator-() const {
        return *this;
      }
      T pow(const long long n) const {
        assert(0 <= n);
        return *this;
      }
      T inv() const {
        assert(this->m_ref);
        return *this;
      }
      friend T operator+(const reference& lhs, const reference& rhs) {
        return static_cast<T>(lhs) += rhs;
      }
      friend T operator+(const T lhs, const reference& rhs) {
        return T(lhs) += rhs;
      }
      friend T operator+(const reference& lhs, const T rhs) {
        return static_cast<T>(lhs) += rhs;
      }
      friend T operator-(const reference& lhs, const reference& rhs) {
        return static_cast<T>(lhs) -= rhs;
      }
      friend T operator-(const T lhs, const reference& rhs) {
        return T(lhs) -= rhs;
      }
      friend T operator-(const reference& lhs, const T rhs) {
        return static_cast<T>(lhs) -= rhs;
      }
      friend T operator*(const reference& lhs, const reference& rhs) {
        return static_cast<T>(lhs) *= rhs;
      }
      friend T operator*(const T lhs, const reference& rhs) {
        return T(lhs) *= rhs;
      }
      friend T operator*(const reference& lhs, const T rhs) {
        return static_cast<T>(lhs) *= rhs;
      }
      friend T operator/(const reference& lhs, const reference& rhs) {
        return static_cast<T>(lhs) /= rhs;
      }
      friend T operator/(const T lhs, const reference& rhs) {
        return T(lhs) /= rhs;
      }
      friend T operator/(const reference& lhs, const T rhs) {
        return static_cast<T>(lhs) /= rhs;
      }
      friend bool operator==(const reference& lhs, const reference& rhs) {
        return static_cast<bool>(lhs.m_ref) == static_cast<bool>(rhs.m_ref);
      }
      friend bool operator==(const T lhs, const reference& rhs) {
        return lhs.val() == static_cast<bool>(rhs.m_ref);
      }
      friend bool operator==(const reference& lhs, const T rhs) {
        return static_cast<bool>(lhs.m_ref) == rhs.val();
      }
      friend bool operator!=(const reference& lhs, const reference& rhs) {
        return static_cast<bool>(lhs.m_ref) != static_cast<bool>(rhs.m_ref);
      }
      friend bool operator!=(const T lhs, const reference& rhs) {
        return lhs.val() != static_cast<bool>(rhs.m_ref);
      }
      friend bool operator!=(const reference& lhs, const T rhs) {
        return static_cast<bool>(lhs.m_ref) != rhs.val();
      }
    };
    class const_row_reference {
      friend Mat;

      using wrapped = typename ::std::conditional_t<variable_sized, ::std::vector<::tools::dynamic_bitset>, ::std::array<::std::bitset<M>, N>>::const_iterator;
      wrapped m_it;

      explicit const_row_reference(const wrapped it) : m_it(it) {
      }

    public:
      const_row_reference() = default;
      const_row_reference(const row_reference ref) : m_it(ref.m_it) {
      }

      T operator[](const int c) const {
        assert(0 <= c && c < ::std::ssize(*this->m_it));
        return T::raw(this->m_it->test(c));
      }
    };

    matrix() = default;
    template <bool SFINAE = variable_sized> requires (SFINAE)
    matrix(const int rows, const int cols) : Base(rows, cols) {}
    template <bool SFINAE = variable_sized> requires (SFINAE)
    matrix(const int rows, const int cols, const T value) : Base(rows, cols, value) {}
    template <bool SFINAE = !variable_sized> requires (SFINAE)
    matrix(const ::std::initializer_list<::std::initializer_list<T>> il) {
      assert(il.size() == this->rows());
      assert(::std::ranges::all_of(il, [&](const auto& row) { return ::std::ssize(row) == this->cols(); }));
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          if (il.begin()[r].begin()[c].val()) {
            this->m_values[r].set(c);
          }
        }
      }
    }
    template <bool SFINAE = variable_sized, ::std::nullptr_t = nullptr> requires (SFINAE)
    matrix(const ::std::initializer_list<::std::initializer_list<T>> il) : Base(il.size(), il.size() ? il.begin()->size() : 0) {
      assert(::std::ranges::all_of(il, [&](const auto& row) { return ::std::ssize(row) == this->cols(); }));
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          if (il.begin()[r].begin()[c].val()) {
            this->m_values[r].set(c);
          }
        }
      }
    }
    row_reference operator[](const int r) {
      assert(0 <= r && r < this->rows());
      return row_reference(this->m_values.begin() + r);
    }
    const_row_reference operator[](const int r) const {
      assert(0 <= r && r < this->rows());
      return const_row_reference(this->m_values.begin() + r);
    }

    int rows() const {
      if constexpr (variable_sized) {
        return this->m_values.size();
      } else {
        return N;
      }
    }
    int cols() const {
      if constexpr (variable_sized) {
        return this->m_cols;
      } else {
        return M;
      }
    }

    Mat operator+() const {
      return *this;
    }
    Mat operator-() const {
      return *this;
    }
    friend Mat operator+(const Mat& lhs, const Mat& rhs) {
      return Mat(lhs) += rhs;
    }
    friend Mat operator-(const Mat& lhs, const Mat& rhs) {
      return Mat(lhs) -= rhs;
    }
    template <int K> requires (!Mat::variable_sized || K == -1)
    friend ::tools::matrix<T, N, K> operator*(const Mat& lhs, const ::tools::matrix<T, M, K>& rhs) {
      assert(lhs.cols() == rhs.rows());

      const auto transposed = rhs.transposed();

      auto result = [&]() {
        if constexpr (Mat::variable_sized) {
          return ::tools::matrix<T>(lhs.rows(), rhs.cols());
        } else {
          return ::tools::matrix<T, N, K>{};
        }
      }();
      for (int i = 0; i < lhs.rows(); ++i) {
        for (int j = 0; j < rhs.cols(); ++j) {
          if ((lhs.m_values[i] & transposed.m_values[j]).count() % 2) {
            result.m_values[i].set(j);
          }
        }
      }
      return result;
    }
    friend ::tools::vector<T, N> operator*(const Mat& lhs, const ::tools::vector<T, M>& rhs) {
      assert(lhs.cols() == rhs.size());

      auto bitset = [&]() {
        if constexpr (Mat::variable_sized) {
          return ::tools::dynamic_bitset(lhs.cols());
        } else {
          return ::std::bitset<M>{};
        }
      }();
      for (int i = 0; i < ::std::ssize(rhs); ++i) {
        if (rhs[i].val()) {
          bitset.set(i);
        }
      }

      auto result = [&]() {
        if constexpr (Mat::variable_sized) {
          return ::tools::vector<T>(lhs.rows());
        } else {
          return ::tools::vector<T, N>{};
        }
      }();
      for (int i = 0; i < lhs.rows(); ++i) {
        result[i] = T::raw((lhs[i] & bitset).count() % 2);
      }
      return result;
    }
    friend Mat operator*(const Mat& lhs, const T& rhs) {
      return Mat(lhs) *= rhs;
    }
    friend Mat operator/(const Mat& lhs, const ::tools::matrix<T, M, M>& rhs) {
      const auto inv = rhs.inv();
      assert(inv);
      return lhs * *inv;
    }
    friend Mat operator/(const Mat& lhs, const T& rhs) {
      return Mat(lhs) /= rhs;
    }
    Mat& operator+=(const Mat& other) {
      assert(this->rows() == other.rows());
      assert(this->cols() == other.cols());
      for (int r = 0; r < this->rows(); ++r) {
        this->m_values[r] ^= other.m_values[r];
      }
      return *this;
    }
    Mat& operator-=(const Mat& other) {
      return *this += other;
    }
    Mat& operator*=(const ::tools::matrix<T, M, M>& other) {
      return *this = *this * other;
    }
    Mat& operator*=(const T c) {
      if (c.val() == 0) {
        for (auto& row : this->m_values) {
          row.reset();
        }
      }
      return *this;
    }
    Mat& operator/=(const ::tools::matrix<T, M, M>& other) {
      return *this = *this / other;
    }
    Mat& operator/=(const T c) {
      assert(c.val());
      return *this;
    }
    friend bool operator==(const Mat& lhs, const Mat& rhs) {
      if constexpr (variable_sized) {
        if (lhs.rows() != rhs.rows()) return false;
        if (lhs.cols() != rhs.cols()) return false;
      }
      return lhs.m_values == rhs.m_values;
    }
    friend bool operator!=(const Mat& lhs, const Mat& rhs) {
      return !(lhs == rhs);
    }

    friend ::std::istream& operator>>(::std::istream& is, Mat& self) {
      for (int r = 0; r < self.rows(); ++r) {
        for (int c = 0; c < self.cols(); ++c) {
          char v;
          do {
            is >> v;
            assert(is);
          } while (!(v == '0' || v == '1'));
          self.m_values[r].set(c, v - '0');
        }
      }
      return is;
    }
    friend ::std::ostream& operator<<(::std::ostream& os, const Mat& self) {
      for (int r = 0; r < self.rows(); ++r) {
        os << '[';
        for (int c = 0; c < self.cols(); ++c) {
          os << ('0' + self.m_values[r].test(c)) << ::std::array<::std::string_view, 2>{", ", ""}[c == self.cols() - 1];
        }
        os << ']' << '\n';
      }
      return os;
    }

    int gauss_jordan() {
      int rank = 0;

      for (int c = 0; c < this->cols(); ++c) {
        int pivot;
        for (pivot = rank; pivot < this->rows() && !this->m_values[pivot].test(c); ++pivot);
        if (pivot == this->rows()) continue;

        if (pivot != rank) {
          ::std::swap(this->m_values[rank], this->m_values[pivot]);
        }

        for (int r = 0; r < this->rows(); ++r) {
          if (r == rank) continue;
          if (!this->m_values[r].test(c)) continue;
          this->m_values[r] ^= this->m_values[rank];
        }

        ++rank;
      }

      return rank;
    }

    int rank() const {
      return (this->rows() < this->cols() ? this->transposed() : Mat(*this)).gauss_jordan();
    }

    ::tools::matrix<T> solve(const ::tools::vector<T, N>& b) const {
      assert(this->rows() == ::std::ssize(b));
      assert(this->cols() >= 1);
      auto Ab = [&]() {
        if constexpr (variable_sized) {
          return Mat(this->rows(), this->cols() + 1);
        } else {
          return ::tools::matrix<T, N, M + 1>{};
        }
      }();
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          if (this->m_values[r].test(c)) {
            Ab.m_values[r].set(c);
          }
        }
        if (b[r].val()) {
          Ab.m_values[r].set(this->cols());
        }
      }

      Ab.gauss_jordan();

      ::std::vector<int> ranks(Ab.cols());
      for (int r = 0, cl = 0, cr = 0; r <= Ab.rows(); ++r, cl = cr) {
        for (; cr < Ab.cols() && (r == Ab.rows() || !Ab.m_values[r].test(cr)); ++cr);
        for (int c = cl; c < cr; ++c) {
          ranks[c] = r;
        }
      }

      if (ranks[Ab.cols() - 2] < ranks[Ab.cols() - 1]) {
        return ::tools::matrix<T>(this->rows(), 0);
      }

      ::tools::matrix<T> answer(this->cols(), this->cols() - ranks.back() + 1);
      int free = this->cols() - ranks.back() - 1;

      for (int l = this->cols(), r = this->cols(); r > 0; r = l) {
        for (; l > 0 && ranks[l - 1] == ranks[r - 1]; --l);
        for (int c = r - 1; c > l; --c) {
          answer.m_values[c].set(free);
          --free;
        }
        if (ranks[l] > 0) {
          answer.m_values[l].set(this->cols() - ranks.back(), Ab.m_values[ranks[l] - 1].test(Ab.cols() - 1));
          for (int c = l + 1; c < Ab.cols() - 1; ++c) {
            if (Ab.m_values[ranks[l] - 1].test(c)) {
              answer.m_values[l] ^= answer.m_values[c];
            }
          }
        } else {
          answer.m_values[l].set(free);
          --free;
        }
      }

      return answer;
    }

    T determinant() const {
      assert(this->rows() == this->cols());

      auto A = *this;
      const auto rank = A.gauss_jordan();

      return T::raw(rank == A.rows());
    }

    template <bool SFINAE = !variable_sized && N == M> requires (SFINAE)
    static Mat e() {
      Mat result{};
      for (int i = 0; i < N; ++i) {
        result[i][i] = T(1);
      }
      return result;
    }
    template <bool SFINAE = variable_sized> requires (SFINAE)
    static Mat e(const int n) {
      assert(n >= 0);
      Mat result(n, n);
      for (int i = 0; i < n; ++i) {
        result[i][i] = T::raw(1);
      }
      return result;
    }

    template <bool SFINAE = variable_sized || N == M> requires (SFINAE)
    ::std::optional<Mat> inv() const {
      assert(this->rows() == this->cols());

      auto AI = [&]() {
        if constexpr (variable_sized) {
          return Mat(this->rows(), this->cols() * 2);
        } else {
          return ::tools::matrix<T, N, M * 2>{};
        }
      }();
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          if (this->m_values[r].test(c)) {
            AI.m_values[r].set(c);
          }
        }
        AI.m_values[r].set(this->cols() + r);
      }

      AI.gauss_jordan();
      for (int i = 0; i < this->rows(); ++i) {
        if (!AI.m_values[i].test(i)) return ::std::nullopt;
      }

      auto B = [&]() {
        if constexpr (variable_sized) {
          return Mat(this->rows(), this->cols());
        } else {
          return Mat{};
        }
      }();
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          if (AI.m_values[r].test(this->cols() + c)) {
            B.m_values[r].set(c);
          }
        }
      }
      return B;
    }

    ::tools::matrix<T, M, N> transposed() const {
      auto A_T = [&]() {
        if constexpr (variable_sized) {
          return Mat(this->cols(), this->rows());
        } else {
          return ::tools::matrix<T, M, N>{};
        }
      }();
      for (int r = 0; r < this->rows(); ++r) {
        for (int c = 0; c < this->cols(); ++c) {
          if (this->m_values[r].test(c)) {
            A_T.m_values[c].set(r);
          }
        }
      }
      return A_T;
    }
  };
}


#line 6 "tests/matrix_mod2/rank.test.cpp"

using mint = atcoder::static_modint<2>;

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);

  int N, M;
  std::cin >> N >> M;
  tools::matrix<mint> A(N, M);
  std::cin >> A;

  std::cout << A.rank() << '\n';
  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 5 ms 3 MB
g++ example_01 :heavy_check_mark: AC 4 ms 4 MB
g++ example_02 :heavy_check_mark: AC 4 ms 3 MB
g++ lowrank_00 :heavy_check_mark: AC 430 ms 8 MB
g++ lowrank_01 :heavy_check_mark: AC 318 ms 16 MB
g++ lowrank_02 :heavy_check_mark: AC 395 ms 17 MB
g++ lowrank_03 :heavy_check_mark: AC 395 ms 8 MB
g++ lowrank_04 :heavy_check_mark: AC 446 ms 8 MB
g++ lowrank_05 :heavy_check_mark: AC 400 ms 8 MB
g++ lowrank_06 :heavy_check_mark: AC 423 ms 8 MB
g++ lowrank_07 :heavy_check_mark: AC 417 ms 8 MB
g++ lowrank_08 :heavy_check_mark: AC 415 ms 8 MB
g++ lowrank_09 :heavy_check_mark: AC 328 ms 25 MB
g++ max_00 :heavy_check_mark: AC 1151 ms 1054 MB
g++ max_01 :heavy_check_mark: AC 803 ms 530 MB
g++ max_02 :heavy_check_mark: AC 685 ms 355 MB
g++ max_03 :heavy_check_mark: AC 703 ms 8 MB
g++ max_04 :heavy_check_mark: AC 625 ms 8 MB
g++ max_05 :heavy_check_mark: AC 623 ms 8 MB
g++ max_06 :heavy_check_mark: AC 840 ms 702 MB
g++ max_07 :heavy_check_mark: AC 1090 ms 1052 MB
g++ max_08 :heavy_check_mark: AC 1807 ms 2100 MB
g++ random_00 :heavy_check_mark: AC 364 ms 7 MB
g++ random_01 :heavy_check_mark: AC 93 ms 6 MB
g++ random_02 :heavy_check_mark: AC 292 ms 12 MB
g++ random_03 :heavy_check_mark: AC 129 ms 4 MB
g++ random_04 :heavy_check_mark: AC 55 ms 4 MB
g++ random_05 :heavy_check_mark: AC 153 ms 5 MB
g++ random_06 :heavy_check_mark: AC 201 ms 5 MB
g++ random_07 :heavy_check_mark: AC 42 ms 4 MB
g++ zero_00 :heavy_check_mark: AC 5 ms 4 MB
g++ zero_01 :heavy_check_mark: AC 4 ms 4 MB
g++ zero_02 :heavy_check_mark: AC 92 ms 528 MB
g++ zero_03 :heavy_check_mark: AC 5 ms 4 MB
g++ zero_04 :heavy_check_mark: AC 211 ms 1052 MB
Back to top page