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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int INF = 1001001001;
vector<int> f(200005, INF);

int fkeisan(int x) {
  if (f[x] != INF) {
    return f[x];
  } else {
    int b = __builtin_popcount(x);
    int x2 = x % b;
    if (x2 == 0) {
      return f[x] = 1;
    } else {
      return f[x] = fkeisan(x2) + 1;
    }
  }

  return 0;
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;
  rep(i, N) {
    if (X[i] == '1')
      cnt++;
  }

  vector<int> amarip1(N);
  int cntp1 = cnt + 1;
  int nowp1 = 1 % cntp1;
  int totalp1 = 0;
  rep(i, N) {
    int a = nowp1;
    amarip1[i] = a;
    if (X[N - 1 - i] == '1') {
      totalp1 += a;
      totalp1 %= cntp1;
    }
    nowp1 *= 2;
    nowp1 %= cntp1;
  }

  vector<int> amarim1(N);
  int cntm1 = cnt - 1;
  int totalm1 = 0;
  if (cntm1 > 0) {
    int nowm1 = 1 % cntm1;
    rep(i, N) {
      int a = nowm1;
      amarim1[i] = a;
      if (X[N - 1 - i] == '1') {
        totalm1 += a;
        totalm1 %= cntm1;
      }
      nowm1 *= 2;
      nowm1 %= cntm1;
    }
  }

  // cout << "total" << total << endl;

  f[0] = 0;
  f[1] = 1;
  f[2] = 1;
  for (int i = 0; i < N; i++) {
    if (cnt == 1 && X[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    int ans;
    if (X[i] == '0') {
      int totalpp = totalp1 + amarip1[N - 1 - i];
      totalpp %= cntp1;
      ans = fkeisan(totalpp) + 1;
    } else {
      int totalmm = totalm1 - amarim1[N - 1 - i];
      if (totalmm < 0) {
        totalmm += cntm1;
      }
      ans = fkeisan(totalmm) + 1;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int INF = 1001001001;
vector<int> f(200005, INF);

int fkeisan(int x) {
  if (f[x] != INF) {
    return f[x];
  } else {
    int b = __builtin_popcount(x);
    int x2 = x % b;
    if (x2 == 0) {
      return f[x] = 1;
    } else {
      return f[x] = fkeisan(x2) + 1;
    }
  }

  return 0;
}

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int cnt = 0;
  rep(i, N) {
    if (X[i] == '1')
      cnt++;
  }

  vector<int> amari(N);
  int now = 1 % cnt;
  int total = 0;
  rep(i, N) {
    int a = now;
    amari[i] = a;
    if (X[N - 1 - i] == '1') {
      total += a;
      total %= cnt;
    }
    now *= 2;
    now %= cnt;
  }

  vector<int> amarip1(N);
  int cntp1 = cnt + 1;
  int nowp1 = 1 % cntp1;
  int totalp1 = 0;
  rep(i, N) {
    int a = nowp1;
    amarip1[i] = a;
    if (X[N - 1 - i] == '1') {
      totalp1 += a;
      totalp1 %= cntp1;
    }
    nowp1 *= 2;
    nowp1 %= cntp1;
  }

  vector<int> amarim1(N);
  int cntm1 = cnt - 1;
  int totalm1 = 0;
  if (cntm1 != 0) {
    int nowm1 = 1 % cntm1;
    rep(i, N) {
      int a = nowm1;
      amarim1[i] = a;
      if (X[N - 1 - i] == '1') {
        totalm1 += a;
        totalm1 %= cntm1;
      }
      nowm1 *= 2;
      nowm1 %= cntm1;
    }
  }

  // cout << "total" << total << endl;

  f[0] = 0;
  f[1] = 1;
  f[2] = 1;
  for (int i = 0; i < N; i++) {
    if (cnt == 1 && X[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    int ans;
    if (X[i] == '0') {
      int totalpp = totalp1 + amarip1[N - 1 - i];
      totalpp %= cntp1;
      ans = fkeisan(totalpp) + 1;
    } else {
      int totalmm = totalm1 - amarim1[N - 1 - i];
      if (totalmm < 0) {
        totalmm += cntm1;
      }
      ans = fkeisan(totalmm) + 1;
    }
    cout << ans << endl;
  }

  return 0;
}