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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;
using vi = vector<int>;

int getpop(int x) {
  int cnt = 0;
  while (x > 0) {
    if (x % 2 == 1) {
      cnt++;
    }
    x = x / 2;
  }
  return cnt;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  int pop_cnt = 0;
  rep(i, n) {
    if (x[i] == '1')
      pop_cnt++;
  }

  vi plus(n), minus(n);
  int base = 1;
  rep(i, n) {
    plus[i] = base % (pop_cnt + 1);
    base *= 2;
    base %= (pop_cnt + 1);
  }

  base = 1;
  if (pop_cnt > 1) {
    rep(i, n) {
      minus[i] = base % (pop_cnt - 1);
      base *= 2;
      base %= (pop_cnt - 1);
    }
  }

  int plus_n = 0, minus_n = 0;
  rep(i, n) {
    if (x[i] == '1') {
      plus_n += plus[n - i - 1];
      plus_n %= (pop_cnt + 1);
      if (pop_cnt > 1) {
        minus_n += minus[n - i - 1];
        minus_n %= (pop_cnt - 1);
      }
    }
  }

  int tmp = 0;
  int cnt = 0;
  rep(i, n) {
    // cout << i << endl;
    tmp = 0;
    cnt = 0;
    if (x[i] == '1') {
      if (pop_cnt > 1) {
        tmp = minus_n - minus[n - i - 1] + (pop_cnt - 1);
        tmp %= (pop_cnt - 1);
      } else {
        cout << 0 << endl;
        continue;
      }
    } else {
      tmp = plus_n + plus[n - i - 1];
      tmp %= (pop_cnt + 1);
    }
    cnt++;
    while (tmp > 0) {
      int popcnt = getpop(tmp);
      tmp %= popcnt;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;
using vi = vector<int>;

int getpop(int x) {
  int cnt = 0;
  while (x > 0) {
    if (x % 2 == 1) {
      cnt++;
    }
    x = x / 2;
  }
  return cnt;
}

int main() {
  int n;
  string x;
  cin >> n >> x;
  int pop_cnt = 0;
  rep(i, n) {
    if (x[i] == '1')
      pop_cnt++;
  }

  vi plus(n), minus(n);
  int base = 1;
  rep(i, n) {
    plus[i] = base % (pop_cnt + 1);
    base *= 2;
    base %= (pop_cnt + 1);
  }

  base = 1;
  if (pop_cnt > 0) {
    rep(i, n) {
      minus[i] = base % (pop_cnt - 1);
      base *= 2;
      base %= (pop_cnt - 1);
    }
  }

  int plus_n = 0, minus_n = 0;
  rep(i, n) {
    if (x[i] == '1') {
      plus_n += plus[n - i - 1];
      plus_n %= (pop_cnt + 1);
      if (pop_cnt > 0) {
        minus_n += minus[n - i - 1];
        minus_n %= (pop_cnt - 1);
      }
    }
  }

  int tmp = 0;
  int cnt = 0;
  rep(i, n) {
    tmp = 0;
    cnt = 0;
    if (x[i] == '1') {
      tmp = minus_n - minus[n - i - 1] + (pop_cnt - 1);
      tmp %= (pop_cnt - 1);
    } else {
      tmp = plus_n + plus[n - i - 1];
      tmp %= (pop_cnt + 1);
    }
    cnt++;
    while (tmp > 0) {
      int popcnt = getpop(tmp);
      tmp %= popcnt;
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}