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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using pll = pair<ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using gr = vector<vll>;
using wgr = vector<vpll>;

#define pb push_back
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
struct d_ {
  template <typename T> d_ &operator,(const T &x) {
    cerr << ' ' << x;
    return *this;
  }
  template <typename T> d_ &operator,(const vector<T> &x) {
    for (auto x : x)
      cerr << ' ' << x;
    return *this;
  }
} d_t;
#define dbg(args...)                                                           \\
  { d_t, "|", __LINE__, "|", ":", args, "\\n"; }
#define EPS (1e-10)
#define INF (1LL << 61)
#define CL(A, I) (memset(A, I, sizeof(A)))
#define all(x) begin(x), end(x)
#define IN(n)                                                                  \\
  ll n;                                                                        \\
  cin >> n;
#define x first
#define y second

int main(void) {
  ios_base::sync_with_stdio(false);
  ll t;
  cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    vll a(n);
    F(n) cin >> a[i];
    string s;
    cin >> s;
    for (auto &c : s)
      c -= '0';
    if (s[n - 1]) {
      cout << 1 << endl;
      continue;
    }
    vll S;
    bool ret = 0;
    D(n - 1) {
      ll cur = a[i];
      for (auto s : S)
        cur = min(cur, cur ^ s);
      if (!s[i]) {
        // add a_i to basis
        S.pb(cur);
      } else {
        if (cur) { // a_i not in S
          ret = 1;
        }
      }
    }
    cout << ret << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using pll = pair<ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using gr = vector<vll>;
using wgr = vector<vpll>;

#define pb push_back
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
struct d_ {
  template <typename T> d_ &operator,(const T &x) {
    cerr << ' ' << x;
    return *this;
  }
  template <typename T> d_ &operator,(const vector<T> &x) {
    for (auto x : x)
      cerr << ' ' << x;
    return *this;
  }
} d_t;
#define dbg(args...)                                                           \\
  { d_t, "|", __LINE__, "|", ":", args, "\\n"; }
#define EPS (1e-10)
#define INF (1LL << 61)
#define CL(A, I) (memset(A, I, sizeof(A)))
#define all(x) begin(x), end(x)
#define IN(n)                                                                  \\
  ll n;                                                                        \\
  cin >> n;
#define x first
#define y second

int main(void) {
  ios_base::sync_with_stdio(false);
  ll t;
  cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    vll a(n);
    F(n) cin >> a[i];
    string s;
    cin >> s;
    for (auto &c : s)
      c -= '0';
    if (s[n - 1]) {
      cout << 1 << endl;
      continue;
    }
    set<ll> S = {a[n - 1], 0};
    D(n - 2) {
      //             for(auto s: S) dbg("\\t",i,"->",s);
      //             dbg(i,(ll)s[i]);
      if (!s[i]) {
        set<ll> SS = S;
        for (auto s : S)
          SS.insert(s ^ a[i]);
        S = SS;
      } else {
        set<ll> SS;
        for (auto s : S)
          if (S.count(s ^ a[i])) {
            //                     dbg("|",s);
            SS.insert(s);
            SS.insert(s ^ a[i]);
          }
        S = SS;
      }
    }
    //         for(auto s: S) dbg(s);
    cout << (S.count(0) ? 0 : 1) << endl;
  }
  return 0;
}