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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\\n";
  else
    cout << a << "\\n";
}
// end of template

const int MAX_N = 200011;
vector<int> C;

int divide(string &X, int m) {
  int ans = 0;
  rep(i, X.size()) {
    ans *= 2;
    ans += X[i] - '0';
    ans = ans % m;
  }
  return ans;
}

ll pow_mod(ll a, ll b, ll m) {
  ll ret = 1;
  while (b > 0) {
    if (b & 1) {
      ret = ret * a % m;
    }
    a = a * a % m;
    b >>= 1;
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N;
  string X;
  cin >> N >> X;

  C.assign(MAX_N, -1);
  C[0] = 0;
  rep(i, 1, MAX_N) {
    int pcnt = (int)bitset<32>(i).count();
    C[i] = 1 + C[i % pcnt];
  }

  auto x = bitset<MAX_N>(X);
  int xpcnt = (int)x.count();

  int mod_neg = 0;
  if (xpcnt > 1)
    mod_neg = divide(X, xpcnt - 1);
  int mod_pos = divide(X, xpcnt + 1);

  for (int i = N - 1; i >= 0; i--) {
    int m;
    if (x[i] == 0) {
      m = (mod_pos + pow_mod(2, i, xpcnt + 1)) % (xpcnt + 1);
      m += xpcnt + 1;
      m %= xpcnt + 1;
      output(1 + C[m]);
    } else {
      if (xpcnt <= 1) {
        output(0);
      } else {
        m = (mod_neg - pow_mod(2, i, xpcnt - 1) + xpcnt - 1) % (xpcnt - 1);
        m += xpcnt - 1;
        m %= xpcnt - 1;
        output(1 + C[m]);
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\\n";
  else
    cout << a << "\\n";
}
// end of template

const int MAX_N = 200011;
vector<int> C;

int divide(string &X, int m) {
  int ans = 0;
  rep(i, X.size()) {
    ans *= 2;
    ans += X[i] - '0';
    ans %= m;
  }
  return ans;
}

ll pow_mod(ll a, ll b, ll m) {
  ll ret = 1;
  while (b > 0) {
    if (b & 1) {
      ret = ret * a % m;
    }
    a = a * a % m;
    b >>= 1;
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N;
  string X;
  cin >> N >> X;

  C.assign(MAX_N, -1);
  C[0] = 0;
  rep(i, 1, MAX_N) {
    int pcnt = (int)bitset<32>(i).count();
    C[i] = 1 + C[i % pcnt];
  }

  auto x = bitset<MAX_N>(X);
  int xpcnt = (int)x.count();

  int mod_neg = 0;
  if (xpcnt > 0)
    mod_neg = divide(X, xpcnt - 1);
  int mod_pos = divide(X, xpcnt + 1);

  for (int i = N - 1; i >= 0; i--) {
    int m;
    if (x[i] == 0) {
      m = (mod_pos + pow_mod(2, i, xpcnt + 1)) % (xpcnt + 1);
      m += xpcnt + 1;
      m %= xpcnt + 1;
    } else {
      m = (mod_neg - pow_mod(2, i, xpcnt - 1) + xpcnt - 1) % (xpcnt - 1);
      m += xpcnt - 1;
      m %= xpcnt - 1;
    }
    output(1 + C[m]);
  }

  return 0;
}