Problem p02651 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include<bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

#define MAX 210
int n;
ll A[MAX];
string s;

class XorBasis {
private:
  const static int len = 61;
  long long M[len];

public:
  XorBasis() { init(); }

  void init() { rep(i, len) M[i] = 0; }

  void add(long long x) {
    rep(i, len) {
      if ((x >> i) & 1LL) {
        if (!((M[i] >> i) & 1LL)) {
          M[i] = M[i] ^ x;
          return;
        }
        x = x ^ M[i];
      }
    }
  }

  bool query(long long x) {
    rep(i, len) {
      if ((x >> i) & 1LL) {
        x = x ^ M[i];
      }
    }
    return x == 0LL;
  }
};

void solve() {
  XorBasis basis;
  basis.init();
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '0') {
      basis.add(A[i]);
    } else {
      if (!basis.query(A[i])) {
        puts("1");
        return;
      }
    }
  }
  puts("0");
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> n;
    rep(i, n) cin >> A[i];
    cin >> s;
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define EPS (1e-9)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(double a, double b) { return !equals(a, b) && a < b; }
bool LTE(double a, double b) { return equals(a, b) || a < b; }

const string YES = "";
const string NO = "";

typedef long long ll;
typedef pair<int, int> ii;

#define MAX 1000
int n;
ll A[MAX];
string s;

int extgcd(int a, int b, int &x, int &y) {
  int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else
    x = 1, y = 0;
  return d;
}

int mod_inv(int a, int m) {
  int x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

int _rank(vector<vector<int>> A, int mod) {
  int H = A.size(), W = A[0].size();
  int r = 0;
  rep(x, W) {
    int pivot = INT_MAX;
    REP(y, r, H)
    if (A[y][x]) {
      pivot = y;
      break;
    }
    if (pivot == INT_MAX)
      continue;
    swap(A[r], A[pivot]);

    REP(y, r + 1, H) {
      int value = A[y][x] * mod_inv(A[r][x], mod) % mod;
      if (value)
        REP(x2, x, W)
      A[y][x2] = ((A[y][x2] - A[r][x2] * value) % mod + mod) % mod;
    }
    r++;
  }
  return r;
}

const int dig = 61;
void solve() {
  if (s.back() == '1') {
    puts("1");
    return;
  }
  vector<vector<int>> vec;
  // vector<vector<int>> M(dig,vector<int>(vec.size()));
  // vector<vector<int>> MM(dig,vector<int>((int)vec.size()+1,0));
  vector<vector<int>> M(dig);
  vector<vector<int>> MM(dig);
  rep(_, dig) MM[_].pb(0);

  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '1') {
      // if( vec.empty() ) { puts("1"); return; }
      if (MM[0].empty()) {
        puts("1");
        return;
      }
      // rep(j,dig) rep(k,(int)vec.size())	M[j][k] = MM[j][k] = vec[k][j];
      // rep(j,dig) MM[j][(int)vec.size()] = ( (A[i]>>j)&1LL );
      // rep(_,dig) MM[_].pb(0);
      rep(j, dig) MM[j][(int)MM[j].size() - 1] = ((A[i] >> j) & 1LL);
      /*
      rep(j,(int)MM.size()) {
        rep(k,(int)MM[j].size()){
          cout << MM[j][k] << " ";
        } puts("");
      }puts("");
      */
      if (_rank(M, 2) != _rank(MM, 2)) {
        puts("1");
        return;
      }
    } else {
      /*
      vector<int> tmp;
      rep(j,dig) {
        tmp.pb(((A[i]>>j)&1LL));
      }
      vec.pb(tmp);
      */
      rep(_, dig) M[_].pb(0);
      // MM.pb(vector<int>());
      rep(j, dig) {
        M[j][(int)M[j].size() - 1] = (A[i] >> j) & 1LL;
        MM[j][(int)MM[j].size() - 1] = (A[i] >> j) & 1LL;
      }
      rep(_, dig) MM[_].pb(0);
    }
  }
  puts("0");
}

char tmp[222];

int main() {
  int T;
  // cin >> T;
  scanf("%d", &T);
  while (T--) {
    // cin >> n;
    scanf("%d", &n);
    rep(i, n) scanf("%lld", &A[i]);
    // rep(i,n) cin >> A[i];
    // cin >> s;
    scanf("%s", &tmp[0]);
    s = string(tmp);
    solve();
  }
  return 0;
}