Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int one = 0;
  REP(i, n) {
    if (s[i] == '1')
      one++;
  }
  int p1 = 1, p2 = 1, sum1 = 0, sum2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      sum1 = (sum1 + p1) % (one + 1);
      sum2 = (sum2 + p2) % (one - 1);
    }
    p1 = (p1 * 2) % (one + 1);
    p2 = (p2 * 2) % (one - 1);
  }
  VI anss(n);
  p1 = 1, p2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    int ans = 1;
    if (s[i] == '1') {
      int sum = sum2;
      sum += (one - 1 - p2);
      sum %= one - 1;
      while (sum != 0) {
        int md = 0;
        REP(i, 30) {
          if (sum >> i & 1)
            md++;
        }
        sum %= md;
        ans++;
      }
    } else {
      int sum = sum1;
      sum += p1;
      sum %= one + 1;
      while (sum != 0) {
        int md = 0;
        REP(i, 30) {
          if (sum >> i & 1)
            md++;
        }
        sum %= md;
        ans++;
      }
    }
    anss[i] = ans;
    p1 = (p1 * 2) % (one + 1);
    p2 = (p2 * 2) % (one - 1);
  }
  REP(i, n)
  cout << anss[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int one = 0;
  REP(i, n) {
    if (s[i] == '1')
      one++;
  }
  if (one == 0) {
    REP(i, n)
    cout << 1 << endl;
    return 0;
  }
  if (one == 1) {
    if (s[n - 1] == '1') {
      REP(i, n) {
        if (s[i] == '1')
          cout << 0 << endl;
        else
          cout << 2 << endl;
      }
    } else {
      REP(i, n) {
        if (i == n - 1)
          cout << 2 << endl;
        else if (s[i] == '1')
          cout << 0 << endl;
        else
          cout << 1 << endl;
      }
    }
    return 0;
  }
  int p1 = 1, p2 = 1, sum1 = 0, sum2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      sum1 = (sum1 + p1) % (one + 1);
      sum2 = (sum2 + p2) % (one - 1);
    }
    p1 = (p1 * 2) % (one + 1);
    p2 = (p2 * 2) % (one - 1);
  }
  VI anss(n);
  p1 = 1, p2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    int ans = 1;
    if (s[i] == '1') {
      int sum = sum2;
      sum += (one - 1 - p2);
      sum %= one - 1;
      while (sum != 0) {
        int md = 0;
        REP(i, 30) {
          if (sum >> i & 1)
            md++;
        }
        sum %= md;
        ans++;
      }
    } else {
      int sum = sum1;
      sum += p1;
      sum %= one + 1;
      while (sum != 0) {
        int md = 0;
        REP(i, 30) {
          if (sum >> i & 1)
            md++;
        }
        sum %= md;
        ans++;
      }
    }
    anss[i] = ans;
    p1 = (p1 * 2) % (one + 1);
    p2 = (p2 * 2) % (one - 1);
  }
  REP(i, n)
  cout << anss[i] << endl;
  return 0;
}