Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
// #pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
//---------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
inline unsigned int __builtin_popcount(unsigned int x) { return __popcnt(x); }
#endif // _MSC_VER
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     @hamayanhamayan0
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/

int N;
string X;
int cnt[201010];
//---------------------------------------------------------------------------------------------------
void _main() {
  cin >> N >> X;

  if (N == 1) {
    if (X == "0")
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return;
  }

  reverse(all(X));
  rep(i, 1, 201010) {
    int pp = __builtin_popcount(i);
    cnt[i] = cnt[i % pp] + 1;
  }

  int cn = 0;
  rep(i, 0, N) if (X[i] == '1') cn++;

  if (cn == 0) {
    rep(i, 0, N) printf("1\n");
  } else if (cn == N) {
    int tot = 0, p = 1;
    rep(i, 0, N) {
      if (X[i] == '1')
        tot = (tot + p) % (N - 1);
      p = (p * 2) % (N - 1);
    }
    rep(i, 0, N) printf("%d\n", cnt[tot] + 1);
  } else {
    vector<int> ans;
    int tot1 = 0, p1 = 1;
    int tot2 = 0, p2 = 1;
    rep(i, 0, N) {
      if (X[i] == '1')
        tot1 = (tot1 + p1) % (cn + 1);
      p1 = (p1 * 2) % (cn + 1);

      if (X[i] == '1')
        tot2 = (tot2 + p2) % (cn - 1);
      p2 = (p2 * 2) % (cn - 1);
    }

    p1 = 1;
    p2 = 1;
    rep(i, 0, N) {
      if (X[i] == '1') {
        int x = (tot2 - p2 + cn - 1) % (cn - 1);
        ans.push_back(cnt[x] + 1);
      } else {
        int x = (tot1 + p1 + cn + 1) % (cn + 1);
        ans.push_back(cnt[x] + 1);
      }
      p1 = (p1 * 2) % (cn + 1);
      p2 = (p2 * 2) % (cn - 1);
    }

    reverse(all(ans));
    rep(i, 0, N) printf("%d\n", ans[i]);
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
// #pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
//---------------------------------------------------------------------------------------------------
#ifdef _MSC_VER
inline unsigned int __builtin_popcount(unsigned int x) { return __popcnt(x); }
#endif // _MSC_VER
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     @hamayanhamayan0
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/

int N;
string X;
int cnt[201010];
//---------------------------------------------------------------------------------------------------
void _main() {
  cin >> N >> X;

  if (N == 1) {
    if (X == "0")
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return;
  }

  reverse(all(X));
  rep(i, 1, 201010) {
    int pp = __builtin_popcount(i);
    cnt[i] = cnt[i % pp] + 1;
  }

  int cn = 0;
  rep(i, 0, N) if (X[i] == '1') cn++;

  if (cn == 0) {
    rep(i, 0, N) printf("1\n");
  } else if (cn == N) {
    int tot = 0, p = 1;
    rep(i, 0, N) {
      if (X[i] == '1')
        tot = (tot + p) % (N - 1);
      p = (p * 2) % (N - 1);
    }
    rep(i, 0, N) printf("%d\n", cnt[tot] + 1);
  } else if (cn == 1) {
    vector<int> ans;
    int tot1 = 0, p1 = 1;
    rep(i, 0, N) {
      if (X[i] == '1')
        tot1 = (tot1 + p1) % (cn + 1);
      p1 = (p1 * 2) % (cn + 1);
    }

    p1 = 1;
    rep(i, 0, N) {
      if (X[i] == '1') {
        ans.push_back(0);
      } else {
        int x = (tot1 + p1 + cn + 1) % (cn + 1);
        ans.push_back(cnt[x] + 1);
      }
      p1 = (p1 * 2) % (cn + 1);
    }

    reverse(all(ans));
    rep(i, 0, N) printf("%d\n", ans[i]);
  } else {
    vector<int> ans;
    int tot1 = 0, p1 = 1;
    int tot2 = 0, p2 = 1;
    rep(i, 0, N) {
      if (X[i] == '1')
        tot1 = (tot1 + p1) % (cn + 1);
      p1 = (p1 * 2) % (cn + 1);

      if (X[i] == '1')
        tot2 = (tot2 + p2) % (cn - 1);
      p2 = (p2 * 2) % (cn - 1);
    }

    p1 = 1;
    p2 = 1;
    rep(i, 0, N) {
      if (X[i] == '1') {
        int x = (tot2 - p2 + cn - 1) % (cn - 1);
        ans.push_back(cnt[x] + 1);
      } else {
        int x = (tot1 + p1 + cn + 1) % (cn + 1);
        ans.push_back(cnt[x] + 1);
      }
      p1 = (p1 * 2) % (cn + 1);
      p2 = (p2 * 2) % (cn - 1);
    }

    reverse(all(ans));
    rep(i, 0, N) printf("%d\n", ans[i]);
  }
}