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

/*{{{*/
#include <bits/stdc++.h>

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define FORR(i, a, b) for (ll i = b - 1; i >= a; i--)

#define vl vector<ll>
#define pll pair<ll, ll>

#define DEBUG(x) cout << #x << ": " << x << endl

using namespace std;
/*}}}*/

int main() {
  ll n;
  string s;
  cin >> n;
  cin >> s;
  vector<bool> digit(n);
  ll oneCount = 0;
  REP(i, n) {
    digit[i] = s[n - 1 - i] == '1';
    if (digit[i])
      oneCount++;
  }
  vl revProcessed(n);
  vl revAns(n, -1);
  ll lessOneCount = oneCount - 1;
  if (lessOneCount < 0)
    lessOneCount = oneCount + 1;
  ll moreOneCount = oneCount + 1;
  if (moreOneCount > n)
    moreOneCount = oneCount - 1;

  ll lt = 1;
  ll mt = 1;
  ll lessBaseSum = 0;
  ll moreBaseSum = 0;

  REP(i, n) {
    if (digit[i]) {
      if (lessOneCount > 0) {
        lessBaseSum += lt % lessOneCount;
        lessBaseSum %= lessOneCount;
      }
      if (moreOneCount > 0) {
        moreBaseSum += mt % moreOneCount;
        moreBaseSum %= moreOneCount;
      }
    }
    if (lessOneCount > 0) {
      lt *= 2;
      lt %= lessOneCount;
    }
    if (moreOneCount > 0) {
      mt *= 2;
      mt %= moreOneCount;
    }
  }

  lt = 1;
  mt = 1;
  REP(i, n) {
    if (digit[i]) {
      if (lessOneCount == 0) {
        revAns[i] = 0;
      } else
        revProcessed[i] = (lessBaseSum + lessOneCount - lt) % lessOneCount;
    } else {
      revProcessed[i] = (moreBaseSum + mt) % moreOneCount;
    }
    if (lessOneCount > 0) {
      lt *= 2;
      lt %= lessOneCount;
    }
    if (moreOneCount > 0) {
      mt *= 2;
      mt %= moreOneCount;
    }
  }
  REP(i, n) {
    if (revAns[i] < 0 && revProcessed[i] == 0)
      revAns[i] = 1;
  }

  // go to second procedure

  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    ll t = 1;
    ll oc = 0;
    REP(j, 20) {
      if ((revProcessed[i] & t) > 0)
        oc++;
      t *= 2;
    }
    revProcessed[i] = revProcessed[i] % oc;
    if (revProcessed[i] == 0)
      revAns[i] = 2;
  }
  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    ll t = 1;
    ll oc = 0;
    REP(j, 20) {
      if ((revProcessed[i] & t) > 0)
        oc++;
      t *= 2;
    }
    revProcessed[i] = revProcessed[i] % oc;
    if (revProcessed[i] == 0)
      revAns[i] = 3;
  }
  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    ll t = 1;
    ll oc = 0;
    REP(j, 20) {
      if ((revProcessed[i] & t) > 0)
        oc++;
      t *= 2;
    }
    revProcessed[i] = revProcessed[i] % oc;
    if (revProcessed[i] == 0)
      revAns[i] = 4;
  }
  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    ll t = 1;
    ll oc = 0;
    REP(j, 20) {
      if ((revProcessed[i] & t) > 0)
        oc++;
      t *= 2;
    }
    revProcessed[i] = revProcessed[i] % oc;
    if (revProcessed[i] == 0)
      revAns[i] = 5;
  }
  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    ll t = 1;
    ll oc = 0;
    REP(j, 20) {
      if ((revProcessed[i] & t) > 0)
        oc++;
      t *= 2;
    }
    revProcessed[i] = revProcessed[i] % oc;
    if (revProcessed[i] == 0)
      revAns[i] = 6;
  }
  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    ll t = 1;
    ll oc = 0;
    REP(j, 20) {
      if ((revProcessed[i] & t) > 0)
        oc++;
      t *= 2;
    }
    revProcessed[i] = revProcessed[i] % oc;
    if (revProcessed[i] == 0)
      revAns[i] = 7;
  }
  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    ll t = 1;
    ll oc = 0;
    REP(j, 20) {
      if ((revProcessed[i] & t) > 0)
        oc++;
      t *= 2;
    }
    revProcessed[i] = revProcessed[i] % oc;
    if (revProcessed[i] == 0)
      revAns[i] = 8;
  }
  REP(i, n) { cout << revAns[n - 1 - i] << endl; }

  return 0;
}


Error: 0

Fixed submission:

/*{{{*/
#include <bits/stdc++.h>

typedef long long ll;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define FORR(i, a, b) for (ll i = b - 1; i >= a; i--)

#define vl vector<ll>
#define pll pair<ll, ll>

#define DEBUG(x) cout << #x << ": " << x << endl

using namespace std;
/*}}}*/

int main() {
  ll n;
  string s;
  cin >> n;
  cin >> s;
  vector<bool> digit(n);
  ll oneCount = 0;
  REP(i, n) {
    digit[i] = s[n - 1 - i] == '1';
    if (digit[i])
      oneCount++;
  }
  vl revProcessed(n);
  vl revAns(n, -1);
  ll lessOneCount = oneCount - 1;
  if (lessOneCount < 0)
    lessOneCount = oneCount + 1;
  ll moreOneCount = oneCount + 1;
  if (moreOneCount > n)
    moreOneCount = oneCount - 1;

  ll lt = 1;
  ll mt = 1;
  ll lessBaseSum = 0;
  ll moreBaseSum = 0;
  // what if lessOneCount = 0?

  REP(i, n) {
    if (digit[i]) {
      if (lessOneCount > 0) {
        lessBaseSum += lt % lessOneCount;
        lessBaseSum %= lessOneCount;
      }
      moreBaseSum += mt % moreOneCount;
      moreBaseSum %= moreOneCount;
    }
    if (lessOneCount > 0) {
      lt *= 2;
      lt %= lessOneCount;
    }
    mt *= 2;
    mt %= moreOneCount;
  }

  lt = 1;
  mt = 1;
  REP(i, n) {
    if (digit[i]) {
      if (lessOneCount == 0) {
        revAns[i] = 0;
      } else
        revProcessed[i] = (lessBaseSum + lessOneCount - lt) % lessOneCount;
    } else {
      revProcessed[i] = (moreBaseSum + mt) % moreOneCount;
    }
    if (lessOneCount > 0) {
      lt *= 2;
      lt %= lessOneCount;
    }
    mt *= 2;
    mt %= moreOneCount;
  }
  // REP(i,n){
  //   if(revAns[i]>-1) DEBUG(revAns[i]);
  //   else DEBUG(revProcessed[i]);
  // }
  REP(i, n) {
    if (revAns[i] < 0 && revProcessed[i] == 0)
      revAns[i] = 1;
  }

  // cout << "go to second procedure" << endl;
  //  second procedure

  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    // cout << "Po" << endl;
    ll t = 1;
    ll oc = 0;
    REP(j, 20) {
      if ((revProcessed[i] & t) > 0)
        oc++;
      t *= 2;
    }
    // DEBUG(revProcessed[i]);
    // DEBUG(oc);
    revProcessed[i] = revProcessed[i] % oc;
    if (revProcessed[i] == 0)
      revAns[i] = 2;
  }
  REP(i, n) {
    if (revAns[i] > -1)
      continue;
    if (revProcessed[i] == 1)
      revAns[i] = 2 + 1;
    if (revProcessed[i] == 2)
      revAns[i] = 2 + 1;
    if (revProcessed[i] == 3)
      revAns[i] = 2 + 2;
    if (revProcessed[i] == 4)
      revAns[i] = 2 + 1;
    if (revProcessed[i] == 5)
      revAns[i] = 2 + 2;
    if (revProcessed[i] == 6)
      revAns[i] = 2 + 1;
    if (revProcessed[i] == 7)
      revAns[i] = 2 + 2;
    if (revProcessed[i] == 8)
      revAns[i] = 2 + 1;
  }
  REP(i, n) { cout << revAns[n - 1 - i] << endl; }

  return 0;
}