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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int popcount32(unsigned int x) {
  x = x - ((x >> 1) & 0x55555555);
  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  x = (x + (x >> 4)) & 0x0F0F0F0F;
  x = x + (x >> 8);
  x = x + (x >> 16);
  return x & 0x0000003F;
}

long long pow_mod(long long x, long long y, int mod) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

int dp[200000];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  string x;
  cin >> n >> x;

  for (int i = 1; i < 200000; i++) {
    dp[i] = dp[i % popcount32(i)] + 1;
  }

  int one = count(all(x), '1');
  if (one == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }

  int modm = 0;
  int modp = 0;
  rep(i, n) {
    if (one > 1) {
      modm = modm * 2;
      modm += x[i] - '0';
      modm %= one - 1;
    }
    modp = modp * 2;
    modp += x[i] - '0';
    modp %= one + 1;
  }

  rep(i, n) {
    if (x[i] == '0') {
      int mod = (one + 1);
      int p = pow_mod(2, n - 1 - i, mod);
      cout << dp[(modp + p) % mod] + 1 << endl;
    } else {
      int mod = (one - 1);
      if (mod == 0) {
        cout << 0 << endl;
        continue;
      }
      int p = pow_mod(2, n - 1 - i, mod);
      cout << dp[(modm - p + mod) % mod] + 1 << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int popcount32(unsigned int x) {
  x = x - ((x >> 1) & 0x55555555);
  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  x = (x + (x >> 4)) & 0x0F0F0F0F;
  x = x + (x >> 8);
  x = x + (x >> 16);
  return x & 0x0000003F;
}

long long pow_mod(long long x, long long y, int mod) {
  int res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    y >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

int dp[200000];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  string x;
  cin >> n >> x;

  for (int i = 1; i < 200000; i++) {
    dp[i] = dp[i % popcount32(i)] + 1;
  }

  int one = count(all(x), '1');
  int modm = 0;
  int modp = 0;
  rep(i, n) {
    if (one) {
      modm = modm * 2;
      modm += x[i] - '0';
      modm %= one - 1;
    }
    modp = modp * 2;
    modp += x[i] - '0';
    modp %= one + 1;
  }

  rep(i, n) {
    if (x[i] == '0') {
      int mod = (one + 1);
      int p = pow_mod(2, n - 1 - i, mod);
      cout << dp[(modp + p) % mod] + 1 << endl;
    } else {
      if (one == 1) {
        cout << 0 << endl;
        continue;
      }
      int mod = (one - 1);
      int p = pow_mod(2, n - 1 - i, mod);
      cout << dp[(modm - p + mod) % mod] + 1 << endl;
    }
  }

  return 0;
}