Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

// べき乗高速化
ll mpow(ll x, ll n, ll M) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  reverse(x.begin(), x.end());
  ll popcountx = 0;
  rep(i, n) {
    if (x[i] == '1')
      popcountx++;
  }
  ll X = 0; // mod(popcountx + 1)
  ll Y = 0; // mod(popcountx - 1)
  rep(i, n) {
    if (x[i] == '1') {
      X += mpow(2, i, popcountx + 1);
      X %= popcountx + 1;
      if (popcountx > 0) {
        Y += mpow(2, i, popcountx - 1);
        Y %= popcountx - 1;
      }
    }
  }
  vector<ll> ans;
  rep(i, n) {
    if (x[i] == '0') {
      ll temp = X;
      temp += mpow(2, i, popcountx + 1);
      temp %= popcountx + 1;
      ll count = 1;
      while (temp > 0) {
        temp = temp % __builtin_popcount(temp);
        count++;
      }
      ans.push_back(count);
    } else {
      ll temp = Y;
      if (popcountx == 1) {
        ans.push_back(0);
        continue;
      }
      temp -= mpow(2, i, popcountx - 1);
      ll count = 1;
      temp %= popcountx - 1;
      if (temp < 0) {
        temp += popcountx - 1;
      }
      while (temp > 0) {
        temp = temp % __builtin_popcount(temp);
        count++;
      }
      ans.push_back(count);
    }
  }
  reverse(ans.begin(), ans.end());
  for (auto v : ans) {
    cout << v << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
typedef long long ll;

// べき乗高速化
ll mpow(ll x, ll n, ll M) {
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  reverse(x.begin(), x.end());
  ll popcountx = 0;
  rep(i, n) {
    if (x[i] == '1')
      popcountx++;
  }
  ll X = 0; // mod(popcountx + 1)
  ll Y = 0; // mod(popcountx - 1)
  rep(i, n) {
    if (x[i] == '1') {
      X += mpow(2, i, popcountx + 1);
      X %= popcountx + 1;
      if (popcountx > 1) {
        Y += mpow(2, i, popcountx - 1);
        Y %= popcountx - 1;
      }
    }
  }
  vector<ll> ans;
  rep(i, n) {
    if (x[i] == '0') {
      ll temp = X;
      temp += mpow(2, i, popcountx + 1);
      temp %= popcountx + 1;
      ll count = 1;
      while (temp > 0) {
        temp = temp % __builtin_popcount(temp);
        count++;
      }
      ans.push_back(count);
    } else {
      ll temp = Y;
      if (popcountx == 1) {
        ans.push_back(0);
        continue;
      }
      temp -= mpow(2, i, popcountx - 1);
      ll count = 1;
      temp %= popcountx - 1;
      if (temp < 0) {
        temp += popcountx - 1;
      }
      while (temp > 0) {
        temp = temp % __builtin_popcount(temp);
        count++;
      }
      ans.push_back(count);
    }
  }
  reverse(ans.begin(), ans.end());
  for (auto v : ans) {
    cout << v << endl;
  }
  return 0;
}