Problem p02651 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;

// @@ !! LIM(ftwo matrix debug)
// --> ftwo f:power matrix f:<< debug
// ---- inserted library file ftwo.cc

struct Ftwo {
  int8_t val;

  Ftwo(ll t = 0) { val = (t == 0) ? 0 : 1; }
  Ftwo(const Ftwo &t) : val(t.val) {}
  Ftwo &operator=(const Ftwo &t) {
    val = t.val;
    return *this;
  }
  Ftwo &operator=(ll t) {
    val = (t == 0) ? 0 : 1;
    return *this;
  }
  Ftwo &operator=(int t) {
    val = (t == 0) ? 0 : 1;
    return *this;
  }

  Ftwo &operator+=(const Ftwo &t) {
    val ^= t.val;
    return *this;
  }

  Ftwo &operator-=(const Ftwo &t) {
    val ^= t.val;
    return *this;
  }

  Ftwo &operator*=(const Ftwo &t) {
    val &= t.val;
    return *this;
  }

  Ftwo inv() const {
    if (val == 0) {
      cerr << "inv() is called for zero." << endl;
      exit(1);
    }
    return Ftwo(1);
  }

  Ftwo &operator/=(const Ftwo &t) { return (*this) *= t.inv(); }

  Ftwo operator+(const Ftwo &t) const { return Ftwo(val) += t; }
  Ftwo operator-(const Ftwo &t) const { return Ftwo(val) -= t; }
  Ftwo operator*(const Ftwo &t) const { return Ftwo(val) *= t; }
  Ftwo operator/(const Ftwo &t) const { return Ftwo(val) /= t; }
  Ftwo operator-() const { return Ftwo(-val); }

  bool operator==(const Ftwo &t) const { return val == t.val; }
  bool operator!=(const Ftwo &t) const { return val != t.val; }

  operator ll() const { return val; }
};

Ftwo operator+(int x, const Ftwo &y) { return Ftwo(x) + y; }
Ftwo operator-(int x, const Ftwo &y) { return Ftwo(x) - y; }
Ftwo operator*(int x, const Ftwo &y) { return Ftwo(x) * y; }
Ftwo operator/(int x, const Ftwo &y) { return Ftwo(x) / y; }
Ftwo operator+(ll x, const Ftwo &y) { return Ftwo(x) + y; }
Ftwo operator-(ll x, const Ftwo &y) { return Ftwo(x) - y; }
Ftwo operator*(ll x, const Ftwo &y) { return Ftwo(x) * y; }
Ftwo operator/(ll x, const Ftwo &y) { return Ftwo(x) / y; }
Ftwo operator+(const Ftwo &x, int y) { return x + Ftwo(y); }
Ftwo operator-(const Ftwo &x, int y) { return x - Ftwo(y); }
Ftwo operator*(const Ftwo &x, int y) { return x * Ftwo(y); }
Ftwo operator/(const Ftwo &x, int y) { return x / Ftwo(y); }
Ftwo operator+(const Ftwo &x, ll y) { return x + Ftwo(y); }
Ftwo operator-(const Ftwo &x, ll y) { return x - Ftwo(y); }
Ftwo operator*(const Ftwo &x, ll y) { return x * Ftwo(y); }
Ftwo operator/(const Ftwo &x, ll y) { return x / Ftwo(y); }

istream &operator>>(istream &is, Ftwo &t) {
  ll x;
  is >> x;
  t = x;
  return is;
}

ostream &operator<<(ostream &os, const Ftwo &t) {
  os << int(t.val);
  return os;
}

// ---- end ftwo.cc
// ---- inserted function power from util.cc
/* *** WARNING ***
      ll x = power(10, 12)
   does not work since it is interpreted as
      ll x = power<int>((int)10, 12)
   Use power<ll>(10, 12) or power(10LL, 12).
 */
template <typename T> T power(T a, ll b) {
  T twoPow = a;
  T rv(1);
  while (b > 0) {
    if (b & 1LL)
      rv *= twoPow;
    twoPow *= twoPow;
    b >>= 1;
  }
  return rv;
}

// ad-hoc power function
template <typename T, typename Op>
T ah_power(T a, ll b, const T &unit_t, Op op) {
  T two_pow = a;
  T ret = unit_t;
  while (b > 0) {
    if (b & 1LL)
      ret = op(ret, two_pow);
    two_pow = op(two_pow, two_pow);
    b >>= 1;
  }
  return ret;
}
// ---- end power
// ---- inserted library file matrix.cc

struct MyExc : exception {};

template <typename T> struct Matrix {
  // private:
  size_t dimI;
  size_t dimJ;
  vector<T> mem;
  static size_t defDimIJ;

  void init_from_vv(const vector<vector<T>> &vec) {
    dimI = vec.size();
    dimJ = vec.at(0).size();
    if (dimI == dimJ)
      defDimIJ = dimI;
    mem.resize(dimI * dimJ);
    for (size_t i = 0; i < dimI; i++) {
      assert(vec.at(i).size() == dimJ);
      for (size_t j = 0; j < dimJ; j++)
        at(i, j) = vec.at(i).at(j);
    }
  }

  // public:

  void precond(bool b, string msg) const {
    if (!b) {
      cerr << "**FATAL** " << msg << endl;
      exit(1);
    }
  }

  T &at(size_t i, size_t j) { return mem.at(i * dimJ + j); }
  const T &at(size_t i, size_t j) const { return mem.at(i * dimJ + j); }

  Matrix(size_t m, size_t n) : dimI(m), dimJ(n), mem(dimI * dimJ) {
    if (dimI == dimJ)
      defDimIJ = dimI;
  }

  Matrix(const vector<vector<T>> &vec) { init_from_vv(vec); }
  Matrix(initializer_list<vector<T>> il) {
    init_from_vv(vector<vector<T>>(il));
  }

  /*  They will be constructed as implicitly-defined constructors???
  Matrix(const Matrix<T>& mat)
    : dimI(mat.dimI), dimJ(mat.dimJ), mem(mat.mem) {}
  Matrix(Matrix<T>&& mat)
    : dimI(mat.dimI), dimJ(mat.dimJ), mem(move(mat.mem)) {}
  */

  Matrix(const T &t) {
    precond(defDimIJ != 0, "defDimIJ not set Matrix(T)");
    dimI = dimJ = defDimIJ;
    mem.resize(dimI * dimJ, (T)0);
    for (size_t i = 0; i < dimI; i++)
      at(i, i) = t;
  }

  /*
  Matrix<T>& operator =(const Matrix<T>& r) {
    dimI = r.dimI;
    dimJ = r.dimJ;
    mem = r.mem;
    return *this;
  }
  */

  Matrix<T> &operator+=(const Matrix<T> &r) {
    precond(dimI == r.dimI && dimJ == r.dimJ, "dimension mismatch");
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimJ; j++)
        at(i, j) += r.at(i, j);
    }
    return *this;
  }

  Matrix<T> &operator-=(const Matrix<T> &r) {
    precond(dimI == r.dimI && dimJ == r.dimJ, "dimension mismatch");
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimJ; j++)
        at(i, j) -= r.at(i, j);
    }
    return *this;
  }

  Matrix<T> operator+(const Matrix<T> &r) const {
    return Matrix<T>(*this) += r;
  }

  Matrix<T> operator-(const Matrix<T> &r) const {
    return Matrix<T>(*this) -= r;
  }

  // Unlike + and -, we anyway need a new object for multiplication.
  // Thus, we first define operator *, and then define operator *=
  // using operator *.
  Matrix<T> operator*(const Matrix<T> &r) const {
    precond(dimJ == r.dimI, "dimension mismatch");
    Matrix<T> result(dimI, r.dimJ);
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < r.dimJ; j++) {
        T s = 0;
        for (size_t k = 0; k < dimJ; k++)
          s += at(i, k) * r.at(k, j);
        result.at(i, j) = s;
      }
    }
    return result;
  }

  Matrix<T> &operator*=(const Matrix<T> &r) { return *this = *this * r; }

  bool operator==(const Matrix<T> &r) const {
    return dimI == r.dimI && dimJ == r.dimJ && mem == r.mem;
  }
  bool operator!=(const Matrix<T> &r) const { return !(*this == r); }

  ostream &ostr_out(ostream &os) const {
    vector<vector<T>> vec(dimI, vector<T>(dimJ));
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimJ; j++)
        vec.at(i).at(j) = at(i, j);
    }
    return os << vec;
  }

  Matrix<T> matpower(ll x) const {
    precond((defDimIJ = dimI) == dimJ, "only for square matrix");
    return power(*this, x);
  }

  Matrix<T> transpose() const {
    Matrix<T> res(dimJ, dimI);
    for (size_t i = 0; i < dimI; i++)
      for (size_t j = 0; j < dimJ; j++) {
        res.at(j, i) = at(i, j);
      }
    return res;
  }

  /* aux functions for sweepout */

  void basic_mult(int i, T t) {
    for (size_t j = 0; j < dimJ; j++)
      at(i, j) *= t;
  }

  void basic_xchg(int i1, int i2) {
    for (size_t j = 0; j < dimJ; j++)
      swap(at(i1, j), at(i2, j));
  }

  void basic_mult_add(int i1, T t, int i2) {
    for (size_t j = 0; j < dimJ; j++)
      at(i2, j) += at(i1, j) * t;
  }

  bool is_zero(T t) const { return t == (T)0; }

  pair<size_t, size_t> find_nz(size_t i0, size_t j0) {
    for (; j0 < dimJ; j0++) {
      size_t i = i0;
      for (; i < dimI && is_zero(at(i, j0)); i++)
        ;
      if (i < dimI)
        return {i, j0};
    }
    return {dimI, dimJ};
  }

  /*
    Sweepout for the vertical direction.
    Returns a pair (rank, det)
      rank: the rank of the matrix
      det: internally used for determinant calculation
    WARNNIG: T should be a field.
   */
  pair<size_t, T> self_sweepout() {
    T det = (T)1;
    size_t j0 = 0;
    size_t i0 = 0;
    for (; i0 < dimI; i0++) {
      auto [i1, j1] = find_nz(i0, j0);
      if (i1 == dimI)
        break;
      j0 = j1;
      if (i1 != i0) {
        det = -det;
        basic_xchg(i0, i1);
      }
      det *= at(i0, j0);
      basic_mult(i0, (T)1 / at(i0, j0));
      for (size_t i = 0; i < dimI; i++) {
        if (i == i0)
          continue;
        basic_mult_add(i0, -at(i, j0), i);
      }
    }
    return {i0, det};
  }

  pair<size_t, T> sweepout() const {
    Matrix<T> res1(*this);
    return res1.self_sweepout();
  }

  /* WARNING: T should be a field. */
  T determinant() const {
    precond((defDimIJ = dimI) == dimJ, "only for square matrix");
    auto [rank, det] = sweepout();
    return (rank == dimI) ? det : (T)0;
  }

  /* WARNING: T should be a field. */
  Matrix<T> inverse() const {
    precond((defDimIJ = dimI) == dimJ, "only for square matrix");
    Matrix<T> work(dimI, dimI * 2);
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimI; j++) {
        work.at(i, j) = at(i, j);
        work.at(i, j + dimI) = (i == j) ? (T)1 : (T)0;
      }
    }
    work.self_sweepout();
    if (!is_zero(work.at(dimI - 1, dimI - 1) - (T)1)) {
      cerr << "inverse() for non-regular matrix." << endl;
      throw MyExc();
    }
    Matrix<T> ret(dimI, dimI);
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimI; j++)
        ret.at(i, j) = work.at(i, j + dimI);
    }
    return ret;
  }
};

template <typename T> size_t Matrix<T>::defDimIJ = 0;

template <typename T> ostream &operator<<(ostream &os, const Matrix<T> &mat) {
  return mat.ostr_out(os);
}

// ---- end matrix.cc
// ---- inserted function << from util.cc
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")";
  return os;
}

template <typename T1, typename T2, typename T3, typename T4>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", "
     << get<3>(t) << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << ']';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const set<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const unordered_set<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const multiset<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T1, typename T2, typename C>
ostream &operator<<(ostream &os, const map<T1, T2, C> &mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

template <typename T1, typename T2, typename C>
ostream &operator<<(ostream &os, const unordered_map<T1, T2, C> &mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

template <typename T, typename T2>
ostream &operator<<(ostream &os, const queue<T, T2> &orig) {
  queue<T, T2> que(orig);
  bool first = true;
  os << '[';
  while (!que.empty()) {
    T x = que.front();
    que.pop();
    if (!first)
      os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}

template <typename T, typename T2, typename T3>
ostream &operator<<(ostream &os, const priority_queue<T, T2, T3> &orig) {
  priority_queue<T, T2, T3> pq(orig);
  bool first = true;
  os << '[';
  while (!pq.empty()) {
    T x = pq.top();
    pq.pop();
    if (!first)
      os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}

template <typename T> ostream &operator<<(ostream &os, const stack<T> &st) {
  stack<T> tmp(st);
  os << '[';
  bool first = true;
  while (!tmp.empty()) {
    T &t = tmp.top();
    if (first)
      first = false;
    else
      os << ", ";
    os << t;
    tmp.pop();
  }
  os << ']';
  return os;
}

#if __cplusplus >= 201703L
template <typename T> ostream &operator<<(ostream &os, const optional<T> &t) {
  if (t.has_value())
    os << "v(" << t.value() << ")";
  else
    os << "nullopt";
  return os;
}
#endif

// ---- end <<
// ---- inserted library file debug.cc
template <class... Args> string dbgFormat(const char *fmt, Args... args) {
  size_t len = snprintf(nullptr, 0, fmt, args...);
  char buf[len + 1];
  snprintf(buf, len + 1, fmt, args...);
  return string(buf);
}

template <class Head> void dbgLog(Head &&head) { cerr << head << endl; }

template <class Head, class... Tail> void dbgLog(Head &&head, Tail &&...tail) {
  cerr << head << " ";
  dbgLog(forward<Tail>(tail)...);
}

#if DEBUG
#define DLOG(...) dbgLog(__VA_ARGS__)
#define DFMT(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL(func, ...) func(__VA_ARGS__)
#else
#define DLOG(...)
#define DFMT(...)
#define DCALL(func, ...)
#endif

#if DEBUG_LIB
#define DLOG_LIB(...) dbgLog(__VA_ARGS__)
#define DFMT_LIB(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL_LIB(func, ...) func(__VA_ARGS__)
#else
#define DLOG_LIB(...)
#define DFMT_LIB(...)
#define DCALL_LIB(func, ...)
#endif

// ---- end debug.cc
// @@ !! LIM  -- end mark --

int main(/* int argc, char *argv[] */) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

#if DEBUG
  const ll msize = 8;
#else
  const ll msize = 60;
#endif

  auto i2vec = [](ll x) -> vector<Ftwo> {
    vector<Ftwo> vec(msize);
    for (ll i = 0; i < msize; i++) {
      vec[i] = Ftwo(x % 2);
      x /= 2;
    }
    return vec;
  };

  auto solve = [&]() -> ll {
    ll N;
    cin >> N;
    vector<ll> A(N);
    for (ll i = 0; i < N; i++)
      cin >> A[i];
    string S;
    cin >> S;
    reverse(A.begin(), A.end());
    reverse(S.begin(), S.end());
    if (S[0] == '1')
      return 1;
    ll idx = 0;
    Matrix<Ftwo> prev_mat(0, 0);
    ll prev_rank = 0;
    while (true) {
      ll prev_idx = idx;
      while (idx < N && S[idx] == '0')
        idx++;
      ll cnt0 = idx - prev_idx;
      while (idx < N && S[idx] == '1')
        idx++;
      ll cnt1 = idx - prev_idx - cnt0;
      if (cnt1 == 0)
        break;
      Matrix<Ftwo> mat0(prev_rank + cnt0, msize);
      for (ll i = 0; i < prev_rank; i++) {
        for (ll j = 0; j < msize; j++)
          mat0.at(i, j) = prev_mat.at(i, j);
      }
      for (ll i = 0; i < cnt0; i++) {
        auto vec = i2vec(A[prev_idx + i]);
        for (ll j = 0; j < msize; j++)
          mat0.at(prev_rank + i, j) = vec[j];
      }
      DLOG("mat0=", mat0);
      auto [rank0, dummy0] = mat0.self_sweepout();
      DLOG("swept: mat0=", mat0);
      DLOG("rank0=", rank0);
      Matrix<Ftwo> mat1(rank0 + cnt1, msize);
      for (ll i = 0; i < (ll)rank0; i++) {
        for (ll j = 0; j < msize; j++)
          mat1.at(i, j) = mat0.at(i, j);
      }
      for (ll i = 0; i < cnt1; i++) {
        auto vec = i2vec(A[prev_idx + cnt0 + i]);
        for (ll j = 0; j < msize; j++)
          mat1.at(rank0 + i, j) = vec[j];
      }
      DLOG("mat1=", mat1);
      auto [rank1, dummy1] = mat1.self_sweepout();
      DLOG("swept: mat1=", mat1);
      DLOG("rank1=", rank1);
      if (rank0 < rank1)
        return 1;
      prev_mat = move(mat0);
      prev_rank = rank0;
    }
    return 0;
  };

  ll T;
  cin >> T;
  for (ll _t = 0; _t < T; _t++)
    cout << solve() << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;

// @@ !! LIM(ftwo matrix debug)
// --> ftwo f:power matrix f:<< debug
// ---- inserted library file ftwo.cc

struct Ftwo {
  int8_t val;

  Ftwo(ll t = 0) { val = (t == 0) ? 0 : 1; }
  Ftwo(const Ftwo &t) : val(t.val) {}
  Ftwo &operator=(const Ftwo &t) {
    val = t.val;
    return *this;
  }
  Ftwo &operator=(ll t) {
    val = (t == 0) ? 0 : 1;
    return *this;
  }
  Ftwo &operator=(int t) {
    val = (t == 0) ? 0 : 1;
    return *this;
  }

  Ftwo &operator+=(const Ftwo &t) {
    val ^= t.val;
    return *this;
  }

  Ftwo &operator-=(const Ftwo &t) {
    val ^= t.val;
    return *this;
  }

  Ftwo &operator*=(const Ftwo &t) {
    val &= t.val;
    return *this;
  }

  Ftwo inv() const {
    if (val == 0) {
      cerr << "inv() is called for zero." << endl;
      exit(1);
    }
    return Ftwo(1);
  }

  Ftwo &operator/=(const Ftwo &t) { return (*this) *= t.inv(); }

  Ftwo operator+(const Ftwo &t) const { return Ftwo(val) += t; }
  Ftwo operator-(const Ftwo &t) const { return Ftwo(val) -= t; }
  Ftwo operator*(const Ftwo &t) const { return Ftwo(val) *= t; }
  Ftwo operator/(const Ftwo &t) const { return Ftwo(val) /= t; }
  Ftwo operator-() const { return Ftwo(-val); }

  bool operator==(const Ftwo &t) const { return val == t.val; }
  bool operator!=(const Ftwo &t) const { return val != t.val; }

  operator ll() const { return val; }
};

Ftwo operator+(int x, const Ftwo &y) { return Ftwo(x) + y; }
Ftwo operator-(int x, const Ftwo &y) { return Ftwo(x) - y; }
Ftwo operator*(int x, const Ftwo &y) { return Ftwo(x) * y; }
Ftwo operator/(int x, const Ftwo &y) { return Ftwo(x) / y; }
Ftwo operator+(ll x, const Ftwo &y) { return Ftwo(x) + y; }
Ftwo operator-(ll x, const Ftwo &y) { return Ftwo(x) - y; }
Ftwo operator*(ll x, const Ftwo &y) { return Ftwo(x) * y; }
Ftwo operator/(ll x, const Ftwo &y) { return Ftwo(x) / y; }
Ftwo operator+(const Ftwo &x, int y) { return x + Ftwo(y); }
Ftwo operator-(const Ftwo &x, int y) { return x - Ftwo(y); }
Ftwo operator*(const Ftwo &x, int y) { return x * Ftwo(y); }
Ftwo operator/(const Ftwo &x, int y) { return x / Ftwo(y); }
Ftwo operator+(const Ftwo &x, ll y) { return x + Ftwo(y); }
Ftwo operator-(const Ftwo &x, ll y) { return x - Ftwo(y); }
Ftwo operator*(const Ftwo &x, ll y) { return x * Ftwo(y); }
Ftwo operator/(const Ftwo &x, ll y) { return x / Ftwo(y); }

istream &operator>>(istream &is, Ftwo &t) {
  ll x;
  is >> x;
  t = x;
  return is;
}

ostream &operator<<(ostream &os, const Ftwo &t) {
  os << int(t.val);
  return os;
}

// ---- end ftwo.cc
// ---- inserted function power from util.cc
/* *** WARNING ***
      ll x = power(10, 12)
   does not work since it is interpreted as
      ll x = power<int>((int)10, 12)
   Use power<ll>(10, 12) or power(10LL, 12).
 */
template <typename T> T power(T a, ll b) {
  T twoPow = a;
  T rv(1);
  while (b > 0) {
    if (b & 1LL)
      rv *= twoPow;
    twoPow *= twoPow;
    b >>= 1;
  }
  return rv;
}

// ad-hoc power function
template <typename T, typename Op>
T ah_power(T a, ll b, const T &unit_t, Op op) {
  T two_pow = a;
  T ret = unit_t;
  while (b > 0) {
    if (b & 1LL)
      ret = op(ret, two_pow);
    two_pow = op(two_pow, two_pow);
    b >>= 1;
  }
  return ret;
}
// ---- end power
// ---- inserted library file matrix.cc

struct MyExc : exception {};

template <typename T> struct Matrix {
  // private:
  size_t dimI;
  size_t dimJ;
  vector<T> mem;
  static size_t defDimIJ;

  void init_from_vv(const vector<vector<T>> &vec) {
    dimI = vec.size();
    dimJ = vec.at(0).size();
    if (dimI == dimJ)
      defDimIJ = dimI;
    mem.resize(dimI * dimJ);
    for (size_t i = 0; i < dimI; i++) {
      assert(vec.at(i).size() == dimJ);
      for (size_t j = 0; j < dimJ; j++)
        at(i, j) = vec.at(i).at(j);
    }
  }

  // public:

  void precond(bool b, string msg) const {
    if (!b) {
      cerr << "**FATAL** " << msg << endl;
      exit(1);
    }
  }

  T &at(size_t i, size_t j) { return mem.at(i * dimJ + j); }
  const T &at(size_t i, size_t j) const { return mem.at(i * dimJ + j); }

  Matrix(size_t m, size_t n) : dimI(m), dimJ(n), mem(dimI * dimJ) {
    if (dimI == dimJ)
      defDimIJ = dimI;
  }

  Matrix(const vector<vector<T>> &vec) { init_from_vv(vec); }
  Matrix(initializer_list<vector<T>> il) {
    init_from_vv(vector<vector<T>>(il));
  }

  /*  They will be constructed as implicitly-defined constructors???
  Matrix(const Matrix<T>& mat)
    : dimI(mat.dimI), dimJ(mat.dimJ), mem(mat.mem) {}
  Matrix(Matrix<T>&& mat)
    : dimI(mat.dimI), dimJ(mat.dimJ), mem(move(mat.mem)) {}
  */

  Matrix(const T &t) {
    precond(defDimIJ != 0, "defDimIJ not set Matrix(T)");
    dimI = dimJ = defDimIJ;
    mem.resize(dimI * dimJ, (T)0);
    for (size_t i = 0; i < dimI; i++)
      at(i, i) = t;
  }

  /*
  Matrix<T>& operator =(const Matrix<T>& r) {
    dimI = r.dimI;
    dimJ = r.dimJ;
    mem = r.mem;
    return *this;
  }
  */

  Matrix<T> &operator+=(const Matrix<T> &r) {
    precond(dimI == r.dimI && dimJ == r.dimJ, "dimension mismatch");
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimJ; j++)
        at(i, j) += r.at(i, j);
    }
    return *this;
  }

  Matrix<T> &operator-=(const Matrix<T> &r) {
    precond(dimI == r.dimI && dimJ == r.dimJ, "dimension mismatch");
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimJ; j++)
        at(i, j) -= r.at(i, j);
    }
    return *this;
  }

  Matrix<T> operator+(const Matrix<T> &r) const {
    return Matrix<T>(*this) += r;
  }

  Matrix<T> operator-(const Matrix<T> &r) const {
    return Matrix<T>(*this) -= r;
  }

  // Unlike + and -, we anyway need a new object for multiplication.
  // Thus, we first define operator *, and then define operator *=
  // using operator *.
  Matrix<T> operator*(const Matrix<T> &r) const {
    precond(dimJ == r.dimI, "dimension mismatch");
    Matrix<T> result(dimI, r.dimJ);
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < r.dimJ; j++) {
        T s = 0;
        for (size_t k = 0; k < dimJ; k++)
          s += at(i, k) * r.at(k, j);
        result.at(i, j) = s;
      }
    }
    return result;
  }

  Matrix<T> &operator*=(const Matrix<T> &r) { return *this = *this * r; }

  bool operator==(const Matrix<T> &r) const {
    return dimI == r.dimI && dimJ == r.dimJ && mem == r.mem;
  }
  bool operator!=(const Matrix<T> &r) const { return !(*this == r); }

  ostream &ostr_out(ostream &os) const {
    vector<vector<T>> vec(dimI, vector<T>(dimJ));
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimJ; j++)
        vec.at(i).at(j) = at(i, j);
    }
    return os << vec;
  }

  Matrix<T> matpower(ll x) const {
    precond((defDimIJ = dimI) == dimJ, "only for square matrix");
    return power(*this, x);
  }

  Matrix<T> transpose() const {
    Matrix<T> res(dimJ, dimI);
    for (size_t i = 0; i < dimI; i++)
      for (size_t j = 0; j < dimJ; j++) {
        res.at(j, i) = at(i, j);
      }
    return res;
  }

  /* aux functions for sweepout */

  void basic_mult(int i, T t) {
    for (size_t j = 0; j < dimJ; j++)
      at(i, j) *= t;
  }

  void basic_xchg(int i1, int i2) {
    for (size_t j = 0; j < dimJ; j++)
      swap(at(i1, j), at(i2, j));
  }

  void basic_mult_add(int i1, T t, int i2) {
    for (size_t j = 0; j < dimJ; j++)
      at(i2, j) += at(i1, j) * t;
  }

  bool is_zero(T t) const { return t == (T)0; }

  pair<size_t, size_t> find_nz(size_t i0, size_t j0) {
    for (; j0 < dimJ; j0++) {
      size_t i = i0;
      for (; i < dimI && is_zero(at(i, j0)); i++)
        ;
      if (i < dimI)
        return {i, j0};
    }
    return {dimI, dimJ};
  }

  /*
    Sweepout for the vertical direction.
    Returns a pair (rank, det)
      rank: the rank of the matrix
      det: internally used for determinant calculation
    WARNNIG: T should be a field.
   */
  pair<size_t, T> self_sweepout() {
    T det = (T)1;
    size_t j0 = 0;
    size_t i0 = 0;
    for (; i0 < dimI; i0++) {
      auto [i1, j1] = find_nz(i0, j0);
      if (i1 == dimI)
        break;
      j0 = j1;
      if (i1 != i0) {
        det = -det;
        basic_xchg(i0, i1);
      }
      det *= at(i0, j0);
      basic_mult(i0, (T)1 / at(i0, j0));
      for (size_t i = 0; i < dimI; i++) {
        if (i == i0)
          continue;
        basic_mult_add(i0, -at(i, j0), i);
      }
    }
    return {i0, det};
  }

  pair<size_t, T> sweepout() const {
    Matrix<T> res1(*this);
    return res1.self_sweepout();
  }

  /* WARNING: T should be a field. */
  T determinant() const {
    precond((defDimIJ = dimI) == dimJ, "only for square matrix");
    auto [rank, det] = sweepout();
    return (rank == dimI) ? det : (T)0;
  }

  /* WARNING: T should be a field. */
  Matrix<T> inverse() const {
    precond((defDimIJ = dimI) == dimJ, "only for square matrix");
    Matrix<T> work(dimI, dimI * 2);
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimI; j++) {
        work.at(i, j) = at(i, j);
        work.at(i, j + dimI) = (i == j) ? (T)1 : (T)0;
      }
    }
    work.self_sweepout();
    if (!is_zero(work.at(dimI - 1, dimI - 1) - (T)1)) {
      cerr << "inverse() for non-regular matrix." << endl;
      throw MyExc();
    }
    Matrix<T> ret(dimI, dimI);
    for (size_t i = 0; i < dimI; i++) {
      for (size_t j = 0; j < dimI; j++)
        ret.at(i, j) = work.at(i, j + dimI);
    }
    return ret;
  }
};

template <typename T> size_t Matrix<T>::defDimIJ = 0;

template <typename T> ostream &operator<<(ostream &os, const Matrix<T> &mat) {
  return mat.ostr_out(os);
}

// ---- end matrix.cc
// ---- inserted function << from util.cc
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}

template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")";
  return os;
}

template <typename T1, typename T2, typename T3, typename T4>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", "
     << get<3>(t) << ")";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << ']';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const set<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const unordered_set<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T, typename C>
ostream &operator<<(ostream &os, const multiset<T, C> &v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  os << '}';

  return os;
}

template <typename T1, typename T2, typename C>
ostream &operator<<(ostream &os, const map<T1, T2, C> &mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

template <typename T1, typename T2, typename C>
ostream &operator<<(ostream &os, const unordered_map<T1, T2, C> &mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin())
      os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';

  return os;
}

template <typename T, typename T2>
ostream &operator<<(ostream &os, const queue<T, T2> &orig) {
  queue<T, T2> que(orig);
  bool first = true;
  os << '[';
  while (!que.empty()) {
    T x = que.front();
    que.pop();
    if (!first)
      os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}

template <typename T, typename T2, typename T3>
ostream &operator<<(ostream &os, const priority_queue<T, T2, T3> &orig) {
  priority_queue<T, T2, T3> pq(orig);
  bool first = true;
  os << '[';
  while (!pq.empty()) {
    T x = pq.top();
    pq.pop();
    if (!first)
      os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}

template <typename T> ostream &operator<<(ostream &os, const stack<T> &st) {
  stack<T> tmp(st);
  os << '[';
  bool first = true;
  while (!tmp.empty()) {
    T &t = tmp.top();
    if (first)
      first = false;
    else
      os << ", ";
    os << t;
    tmp.pop();
  }
  os << ']';
  return os;
}

#if __cplusplus >= 201703L
template <typename T> ostream &operator<<(ostream &os, const optional<T> &t) {
  if (t.has_value())
    os << "v(" << t.value() << ")";
  else
    os << "nullopt";
  return os;
}
#endif

// ---- end <<
// ---- inserted library file debug.cc
template <class... Args> string dbgFormat(const char *fmt, Args... args) {
  size_t len = snprintf(nullptr, 0, fmt, args...);
  char buf[len + 1];
  snprintf(buf, len + 1, fmt, args...);
  return string(buf);
}

template <class Head> void dbgLog(Head &&head) { cerr << head << endl; }

template <class Head, class... Tail> void dbgLog(Head &&head, Tail &&...tail) {
  cerr << head << " ";
  dbgLog(forward<Tail>(tail)...);
}

#if DEBUG
#define DLOG(...) dbgLog(__VA_ARGS__)
#define DFMT(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL(func, ...) func(__VA_ARGS__)
#else
#define DLOG(...)
#define DFMT(...)
#define DCALL(func, ...)
#endif

#if DEBUG_LIB
#define DLOG_LIB(...) dbgLog(__VA_ARGS__)
#define DFMT_LIB(...) cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL_LIB(func, ...) func(__VA_ARGS__)
#else
#define DLOG_LIB(...)
#define DFMT_LIB(...)
#define DCALL_LIB(func, ...)
#endif

// ---- end debug.cc
// @@ !! LIM  -- end mark --

int main(/* int argc, char *argv[] */) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

#if DEBUG
  const ll msize = 8;
#else
  const ll msize = 60;
#endif

  auto i2vec = [](ll x) -> vector<Ftwo> {
    vector<Ftwo> vec(msize);
    for (ll i = 0; i < msize; i++) {
      vec[i] = Ftwo(x % 2);
      x /= 2;
    }
    return vec;
  };

  auto solve = [&]() -> ll {
    ll N;
    cin >> N;
    vector<ll> A(N);
    for (ll i = 0; i < N; i++)
      cin >> A[i];
    string S;
    cin >> S;
    reverse(A.begin(), A.end());
    reverse(S.begin(), S.end());
    if (S[0] == '1')
      return 1;
    ll idx = 0;
    Matrix<Ftwo> prev_mat(0, 0);
    ll prev_rank = 0;
    while (true) {
      ll prev_idx = idx;
      while (idx < N && S[idx] == '0')
        idx++;
      ll cnt0 = idx - prev_idx;
      while (idx < N && S[idx] == '1')
        idx++;
      ll cnt1 = idx - prev_idx - cnt0;
      if (cnt1 == 0)
        break;
      Matrix<Ftwo> mat0(prev_rank + cnt0, msize);
      for (ll i = 0; i < prev_rank; i++) {
        for (ll j = 0; j < msize; j++)
          mat0.at(i, j) = prev_mat.at(i, j);
      }
      for (ll i = 0; i < cnt0; i++) {
        auto vec = i2vec(A[prev_idx + i]);
        for (ll j = 0; j < msize; j++)
          mat0.at(prev_rank + i, j) = vec[j];
      }
      DLOG("mat0=", mat0);
      auto [rank0, dummy0] = mat0.self_sweepout();
      DLOG("swept: mat0=", mat0);
      DLOG("rank0=", rank0);
      Matrix<Ftwo> mat1(rank0 + cnt1, msize);
      for (ll i = 0; i < (ll)rank0; i++) {
        for (ll j = 0; j < msize; j++)
          mat1.at(i, j) = mat0.at(i, j);
      }
      for (ll i = 0; i < cnt1; i++) {
        auto vec = i2vec(A[prev_idx + cnt0 + i]);
        for (ll j = 0; j < msize; j++)
          mat1.at(prev_rank + rank0 + i, j) = vec[j];
      }
      DLOG("mat1=", mat1);
      auto [rank1, dummy1] = mat1.self_sweepout();
      DLOG("swept: mat1=", mat1);
      DLOG("rank1=", rank1);
      if (rank0 < rank1)
        return 1;
      prev_mat = move(mat0);
      prev_rank = rank0;
    }
    return 0;
  };

  ll T;
  cin >> T;
  for (ll _t = 0; _t < T; _t++)
    cout << solve() << "\\n";

  return 0;
}