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

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

// string s[1010], b[1010];
bool a[40];
ll c[40], cc[40], d[1010];
//???????´??????? x???e???
ll binpow(ll x, ll e) {
  ll a = 1, p = x;

  while (e > 0) {
    if (e % 2 == 0) {
      p = (p * p);
      e /= 2;
    } else {
      a = (a * p);
      e--;
    }
  }

  return a;
}

signed main(void) {
  while (true) {
    int n, m, q;
    cin >> n >> m >> q;
    if (!n && !m && !q)
      break;
    // REP(i, q) cin >> s[i] >> b[i];

    REP(i, n) {
      a[i] = false;
      c[i] = cc[i] = 0;
    }
    REP(i, m) d[i] = binpow(2, n) - 1;
    // cout << "a" << endl;
    ll k = 0;
    REP(i, q) {
      string s, b;
      cin >> s >> b;
      ll t = 0;
      REP(j, n) { t = ((t << 1) | (s[j] - '0')); }
      k ^= t;
      // cout << "i:" << i << endl;
      REP(j, m) {
        // cout << "j:" << j << " " << d[j] << " " << b[j]<< endl;
        if (b[j] == '1')
          d[j] &= k;
        else
          d[j] &= ~k;
      }
    }
    REP(i, m) {
      int cnt = 0;
      VI e;
      REP(j, n) {
        if (d[i] >> j & 1) {
          cnt++;
          e.PB(j);
        }
      }
      if (cnt == 1) {
        e[0] = n - 1 - e[0];
        // cout << "e" << e[0] << endl;
        if (e[0] < 10)
          cout << e[0];
        else
          cout << (char)(e[0] - 10 + 'A');
      } else {
        cout << "?";
      }
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

// string s[1010], b[1010];
bool a[40];
ll c[40], cc[40], d[1010];
//???????´??????? x???e???
ll binpow(ll x, ll e) {
  ll a = 1, p = x;

  while (e > 0) {
    if (e % 2 == 0) {
      p = (p * p);
      e /= 2;
    } else {
      a = (a * p);
      e--;
    }
  }

  return a;
}

signed main(void) {
  while (true) {
    int n, m, q;
    cin >> n >> m >> q;
    if (!n && !m && !q)
      break;
    // REP(i, q) cin >> s[i] >> b[i];

    REP(i, n) {
      a[i] = false;
      c[i] = cc[i] = 0;
    }
    REP(i, m) d[i] = binpow(2, n) - 1;
    // cout << "a" << endl;
    REP(i, q) {
      string s, b;
      cin >> s >> b;
      REP(j, n) {
        if (s[j] == '1')
          a[j] = !a[j];
      }
      for (int j = n - 1; j >= 0; --j) {
        if (a[j])
          c[i] += (binpow(2, n - 1 - j));
      }
      REP(j, m) {
        // cout << "j:" << j << " " << d[j] << " " << b[j]<< endl;
        if (b[j] == '1')
          d[j] &= c[i];
        else
          d[j] &= ~c[i];
      }
    }
    // cout << "b" << endl;
    // REP(i, q) cout << c[i] << " "; cout << endl;

    // cout << "c" << endl;
    // REP(i, m) cout << d[i] << " "; cout << endl;

    REP(i, m) {
      int cnt = 0;
      VI e;
      REP(j, n) {
        if (d[i] >> j & 1) {
          cnt++;
          e.PB(j);
        }
      }
      if (cnt == 1) {
        e[0] = n - 1 - e[0];
        // cout << "e" << e[0] << endl;
        if (e[0] < 10)
          cout << e[0];
        else
          cout << (char)(e[0] - 10 + 'A');
      } else {
        cout << "?";
      }
    }
    cout << endl;
  }

  return 0;
}