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

#include <bits/stdc++.h>
using namespace std;

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int modpow(int a, int b, int m) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    llint t = modpow(a, b / 2, m);
    return t * t % m;
  }
  return a * modpow(a, b - 1, m) % m;
}

int popcount(int n) {
  int ans = 0;
  while (n != 0) {
    if (n % 2 == 1)
      ans++;
    n /= 2;
  }
  return ans;
}

int main(void) {
  int n;
  cin >> n;

  string x;
  cin >> x;

  int pcx = 0;
  for (int in = 0; in < n; in++) {
    if (x[in] == '1')
      pcx++;
  }

  if (pcx == 1) {
    if (x[n - 1] != '1') {
      for (int in = 0; in < n; in++) {
        if (in == n - 1)
          cout << 2 << endl;
        else if (x[in] != '1')
          cout << 1 << endl;
        else
          cout << 0 << endl;
      }
    } else {
      for (int in = 0; in < n; in++) {
        if (in == n - 1)
          cout << 0 << endl;
        else
          cout << 2 << endl;
      }
    }
    return 0;
  }

  int ansp1 = 0, tmpp1;
  for (int in = 0; in < n; in++) {
    tmpp1 = 0;
    if (x[in] == '1') {
      tmpp1 = modpow(2, x.size() - in - 1, pcx + 1) - 1;
    }
    ansp1 += tmpp1;
    if (ansp1 < 0)
      ansp1 += pcx + 1;
    ansp1 %= pcx + 1;
  }

  int ansm1 = 0, tmpm1;
  for (int in = 0; in < n; in++) {
    tmpm1 = 0;
    if (x[in] == '1') {
      tmpm1 = modpow(2, x.size() - in - 1, pcx - 1) - 1;
    }
    ansm1 += tmpm1;
    if (ansm1 < 0)
      ansm1 += pcx - 1;
    ansm1 %= pcx - 1;
  }

  int nowx, ans;
  for (int in = 0; in < n; in++) {
    ans = 1;
    if (x[in] == '0') {
      nowx = ansp1;
      nowx += modpow(2, x.size() - in - 1, pcx + 1) - 1;
      if (nowx < 0)
        nowx += pcx + 1;
      nowx %= pcx + 1;
    } else {
      nowx = ansm1;
      nowx -= modpow(2, x.size() - in - 1, pcx - 1) - 1;
      if (nowx < 0)
        nowx += pcx - 1;
      nowx %= pcx - 1;
    }
    // cout << nowx << endl;
    int div;
    while (nowx != 0) {
      div = popcount(nowx);
      nowx %= div;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int modpow(int a, int b, int m) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    llint t = modpow(a, b / 2, m);
    return t * t % m;
  }
  return a * modpow(a, b - 1, m) % m;
}

int main(void) {
  int n;
  cin >> n;

  string x;
  cin >> x;

  int pcx = 0;
  for (int in = 0; in < n; in++) {
    if (x[in] == '1')
      pcx++;
  }

  int only1 = n + 1;
  if (pcx == 1) {
    for (int in = 0; in < n; in++) {
      if (x[in] == '1')
        only1 = in;
    }
  }

  int ansp1 = 0, ansm1 = 0;
  for (int in = 0; in < n; in++) {
    int tmpp1, tmpm1;
    // cout << x[in] << endl;
    if (x[in] == '1') {
      tmpp1 = modpow(2, x.size() - in - 1, pcx + 1);
      // cout << tmpp1 << endl;
      tmpm1 = modpow(2, x.size() - in - 1, pcx - 1);
    }
    ansp1 += tmpp1 - 1;
    ansm1 += tmpm1 - 1;
    ansp1 %= pcx + 1;
    ansm1 %= pcx - 1;
  }

  int nowx, ans;
  for (int in = 0; in < n; in++) {
    ans = 1;
    if (pcx == 1) {
      if (only1 == in) {
        cout << 0 << endl;
        continue;
      }
    }
    if (x[in] == '0') {
      nowx = ansp1;
      nowx += modpow(2, x.size() - in - 1, pcx + 1);
      nowx %= pcx + 1;
    } else {
      nowx = ansm1;
      nowx -= modpow(2, x.size() - in - 1, pcx - 1);
      nowx %= pcx - 1;
      if (nowx < 0)
        nowx += pcx - 1;
    }
    // cout << nowx << endl;
    int div;
    while (nowx != 0) {
      bitset<32> bs(nowx);
      div = bs.count();
      nowx %= div;
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}