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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    REP(WW, n) cerr << v[WW] << ' ';                                           \\
    cerr << endl;                                                              \\
  }
#define SHOW2d(v, WW, HH)                                                      \\
  {                                                                            \\
    REP(W_, WW) {                                                              \\
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \\
      cerr << endl;                                                            \\
    }                                                                          \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007LL

typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  vector<ll> popcount(222222);
  for (ll i = 1; i < 222222; i++) {
    ll cou = 0;
    REP(j, 30) if ((i >> j) & 1) cou++;
    popcount[i] = popcount[i % cou] + 1;
  }

  ll n;
  cin >> n;
  string str;
  cin >> str;

  ll cou = -1;
  REP(i, str.size()) if (str[i] == '1') cou++;

  vector<ll> now(3);
  vector<ll> kake(3, 1);
  REV(i, str.size()) {
    REP(j, 3) {
      if (cou + j == 0)
        continue;
      if (str[i] != '1')
        continue;
      now[j] = (now[j] + kake[j]) % (cou + j);
    }
    REP(j, 3) {
      if (cou + j == 0)
        continue;
      kake[j] = kake[j] * 2 % (cou + j);
    }
  }

  // cout << "cou: " << cou << endl;

  REP(i, 3) kake[i] = 1;
  vector<ll> ans(n);

  REV(i, str.size()) {
    ll tmp;
    ll pla;
    if (str[i] == '0') {
      // cout << i << " " << 0 << " " << now[2] << " " << kake[2] << endl;
      tmp = (now[2] + kake[2]) % (cou + 2);
      pla = 1;
    } else {
      // cout << i << " " << 1 << " " << now[0] << " " << kake[0] << endl;
      if (cou == 0) {
        tmp = 0;
        pla = 0;
      } else {
        tmp = (now[0] - kake[0] + (cou)) % cou;
        pla = 1;
      }
    }

    // cout << "i: " << i << " tmp: " << tmp << endl;
    ans[i] = popcount[tmp] + pla;

    REP(j, 3) {
      if (cou + j == 0)
        continue;
      kake[j] = kake[j] * 2 % (cou + j);
    }
  }

  REP(i, n) cout << ans[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    REP(WW, n) cerr << v[WW] << ' ';                                           \\
    cerr << endl;                                                              \\
  }
#define SHOW2d(v, WW, HH)                                                      \\
  {                                                                            \\
    REP(W_, WW) {                                                              \\
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \\
      cerr << endl;                                                            \\
    }                                                                          \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007LL

typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  vector<ll> popcount(222222);
  for (ll i = 1; i < 222222; i++) {
    ll cou = 0;
    REP(j, 30) if ((i >> j) & 1) cou++;
    popcount[i] = popcount[i % cou] + 1;
  }

  ll n;
  cin >> n;
  string str;
  cin >> str;

  ll cou = -1;
  REP(i, str.size()) if (str[i] == '1') cou++;

  vector<ll> now(3);
  vector<ll> kake(3, 1);
  REV(i, str.size()) {
    REP(j, 3) {
      if (cou + j == 0)
        continue;
      if (str[i] != '1')
        continue;
      now[j] = (now[j] + kake[j]) % (cou + j);
    }
    REP(j, 3) {
      if (cou + j == 0)
        continue;
      kake[j] = kake[j] * 2 % (cou + j);
    }
  }

  // cout << "cou: " << cou << endl;

  REP(i, 3) kake[i] = 1;
  vector<ll> ans(n);

  REV(i, str.size()) {
    ll tmp;
    if (str[i] == '0') {
      // cout << i << " " << 0 << " " << now[2] << " " << kake[2] << endl;
      tmp = (now[2] + kake[2]) % (cou + 2);
    } else {
      // cout << i << " " << 1 << " " << now[0] << " " << kake[0] << endl;
      tmp = (now[0] - kake[0] + (cou)) % cou;
    }

    // cout << "i: " << i << " tmp: " << tmp << endl;
    ans[i] = popcount[tmp] + 1;

    REP(j, 3) {
      if (cou + j == 0)
        continue;
      kake[j] = kake[j] * 2 % (cou + j);
    }
  }

  REP(i, n) cout << ans[i] << endl;

  return 0;
}