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

#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < b + 1; i++)
#define all(v) v.begin(), v.end()
#define SENTINEL 2000000000
#define NIL -1
using namespace std;
typedef long long ll;

const ll MAX = 510000;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int func(int a) {
  unsigned int sum = (a & 0x55555555) + ((a >> 1) & 0x55555555);
  sum = (sum & 0x33333333) + ((sum >> 2) & 0x33333333);
  sum = (sum & 0x0f0f0f0f) + ((sum >> 4) & 0x0f0f0f0f);
  sum = (sum & 0x00ff00ff) + ((sum >> 8) & 0x00ff00ff);
  sum = (sum & 0x0000ffff) + (sum >> 16);
  return sum;
}
int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int tmp = 0;
  REP(i, n) {
    if (x[i] == '1') {
      tmp++;
    }
  }
  vector<int> amari_1(n); //-1
  vector<int> amari_2(n); //+1
  int a = 1;
  IREP(i, n) {
    if (tmp - 1 == 0) {
      continue;
    }
    a %= (tmp - 1);
    amari_1[i] = a;
    a *= 2;
  }
  a = 1;
  IREP(i, n) {
    a %= (tmp + 1);
    amari_2[i] = a;
    a *= 2;
  }
  int now_1 = 0;
  int now_2 = 0;
  REP(i, n) {
    if (x[i] == '1') {
      now_2 += amari_2[i];
      now_2 %= (tmp + 1);
      if (tmp - 1 <= 0) {
        continue;
      }
      now_1 += amari_1[i];
      now_1 %= (tmp - 1);
    }
  }
  REP(i, n) {
    int ans = 1;
    int am;
    if (x[i] == '1') {
      if (tmp - 1 <= 0) {
        cout << 0 << endl;
        continue;
      }
      am = now_1 - amari_1[i];
      if (am < 0)
        am += (tmp - 1);
    } else {
      am = now_2 + amari_2[i];
      am %= (tmp + 1);
    }
    while (am > 0) {
      int b = func(am);
      am %= b;
      ans++;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < b + 1; i++)
#define all(v) v.begin(), v.end()
#define SENTINEL 2000000000
#define NIL -1
using namespace std;
typedef long long ll;

const ll MAX = 510000;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int func(int a) {
  unsigned int sum = (a & 0x55555555) + ((a >> 1) & 0x55555555);
  sum = (sum & 0x33333333) + ((sum >> 2) & 0x33333333);
  sum = (sum & 0x0f0f0f0f) + ((sum >> 4) & 0x0f0f0f0f);
  sum = (sum & 0x00ff00ff) + ((sum >> 8) & 0x00ff00ff);
  sum = (sum & 0x0000ffff) + (sum >> 16);
  return sum;
}
int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int tmp = 0;
  REP(i, n) {
    if (x[i] == '1') {
      tmp++;
    }
  }
  vector<int> amari_1(n); //-1
  vector<int> amari(n);   // tmp
  vector<int> amari_2(n); //+1
  int a = 1;
  IREP(i, n) {
    a %= tmp;
    amari[i] = a;
    a *= 2;
  }
  a = 1;
  IREP(i, n) {
    a %= (tmp - 1);
    amari_1[i] = a;
    a *= 2;
  }
  a = 1;
  IREP(i, n) {
    a %= (tmp + 1);
    amari_2[i] = a;
    a *= 2;
  }
  int now_1 = 0;
  int now = 0;
  int now_2 = 0;
  REP(i, n) {
    if (x[i] == '1') {
      now_1 += amari_1[i];
      now_1 %= (tmp - 1);
      now += amari[i];
      now %= tmp;
      now_2 += amari_2[i];
      now_2 %= (tmp + 1);
    }
  }
  REP(i, n) {
    int ans = 1;
    int am;
    if (x[i] == '1') {
      am = now_1 - amari_1[i];
      if (am < 0)
        am += (tmp - 1);
    } else {
      am = now_2 + amari_2[i];
      am %= (tmp + 1);
    }
    while (am > 0) {
      int b = func(am);
      am %= b;
      ans++;
    }
    cout << ans << endl;
  }
}