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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

int keta(int n) {
  int res = 0;
  while (n > 0) {
    if (n % 2 == 1) {
      res++;
      n--;
    }
    n /= 2;
  }
  return res;
}

int solve(int n, int x) {
  if (n == 0)
    return x;
  int ppcnt = keta(n);
  n %= ppcnt;
  x++;
  return solve(n, x);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<char> a(n);
  rep(i, n) cin >> a[i];
  int ppcnt = 0;

  rep(i, n) if (a[i] == '1') ppcnt++;

  if (ppcnt == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }

  vector<int> up(n + 1, 0), down(n + 1, 0), cul(n + 1, 0);
  vector<P> two(n + 1);
  int nowup = 1, nowdown = 1;
  for (int i = n - 1; i >= 0; i--) {
    two[i] = P(nowup, nowdown);
    int num = a[i] - '0';
    up[i] = up[i + 1] + num * nowup;
    down[i] = down[i + 1] + nowdown * num;
    up[i] %= (ppcnt + 1);
    if (ppcnt != 1)
      down[i] %= (ppcnt - 1);
    nowup *= 2;
    nowup %= ppcnt + 1;
    nowdown *= 2;
    if (ppcnt != 1)
      nowdown %= ppcnt - 1;
  }

  rep(i, n) {
    int num = 0;
    if (a[i] == '1') {
      num = down[0];
      num -= two[i].second;
      if (num < 0)
        num += ppcnt - 1;
    } else {
      num = up[0];
      num += two[i].first;
      num %= (ppcnt + 1);
    }
    if (ppcnt == 1 && a[i] == '1')
      cout << 0 << endl;
    else
      cout << solve(num, 1) << endl;
  }
} /*
 ./problem.exe
 */

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

int keta(int n) {
  int res = 0;
  while (n > 0) {
    if (n % 2 == 1) {
      res++;
      n--;
    }
    n /= 2;
  }
  return res;
}

int solve(int n, int x) {
  if (n == 0)
    return x;
  int ppcnt = keta(n);
  n %= ppcnt;
  x++;
  return solve(n, x);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<char> a(n);
  rep(i, n) cin >> a[i];
  int ppcnt = 0;

  rep(i, n) if (a[i] == '1') ppcnt++;

  if (ppcnt == 0) {
    rep(i, n) cout << 0 << endl;
    return 0;
  }

  vector<int> up(n + 1, 0), down(n + 1, 0), cul(n + 1, 0);
  vector<P> two(n + 1);
  int nowup = 1, nowdown = 1;
  for (int i = n - 1; i >= 0; i--) {
    two[i] = P(nowup, nowdown);
    int num = a[i] - '0';
    up[i] = up[i + 1] + num * nowup;
    down[i] = down[i + 1] + nowdown * num;
    up[i] %= (ppcnt + 1);
    down[i] %= (ppcnt - 1);
    nowup *= 2;
    nowup %= ppcnt + 1;
    nowdown *= 2;
    nowdown %= ppcnt - 1;
  }

  rep(i, n) {
    int num = 0;
    if (a[i] == '1') {
      num = down[0];
      num -= two[i].second;
      if (num < 0)
        num += ppcnt - 1;
    } else {
      num = up[0];
      num += two[i].first;
      num %= (ppcnt + 1);
    }
    if (ppcnt == 1 && a[i] == 1)
      cout << 0 << endl;
    else
      cout << solve(num, 1) << endl;
  }
} /*
 ./problem.exe
 */