Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

long long power(long long a, unsigned long long b, long long m) {
  long long ret(1);
  while (b) {
    if (b & 1)
      ret = (ret * a) % m;
    a = (a * a) % m;
    b /= 2;
    if (ret < 0)
      ret += m;
    if (a < 0)
      a += m;
  }
  return ret;
}

int main() {
  int N;
  std::cin >> N;
  std::string X;
  std::cin >> X;
  int pop(0);
  for (int i(0); i < N; ++i) {
    if (X[i] == '1')
      ++pop;
  }
  int p1(0), d1(0);
  for (int i(0); i < N; ++i) {
    p1 = (p1 * 2 + (X[i] - '0')) % (pop + 1);
    if (pop > 1) {
      d1 = (d1 * 2 + (X[i] - '0')) % (pop - 1);
    }
  }
  for (int i(0); i < N; ++i) {
    long long n((X[i] == '0') ? p1 : d1), cnt(1);
    if (X[i] == '0') {
      n = (n + power(2, N - i - 1, pop + 1)) % (pop + 1);
    } else {
      n = (n - power(2, N - i - 1, pop - 1)) % (pop - 1);
      if (n < 0)
        n += pop - 1;
    }
    while (n) {
      ++cnt;
      long long m(n);
      int a(__builtin_popcount(n));
      n %= a;
    }
    std::cout << cnt << "\n";
  }
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

long long power(long long a, unsigned long long b, long long m) {
  long long ret(1);
  while (b) {
    if (b & 1)
      ret = (ret * a) % m;
    a = (a * a) % m;
    b /= 2;
    if (ret < 0)
      ret += m;
    if (a < 0)
      a += m;
  }
  return ret;
}

int main() {
  int N;
  std::cin >> N;
  std::string X;
  std::cin >> X;
  int pop(0);
  for (int i(0); i < N; ++i) {
    if (X[i] == '1')
      ++pop;
  }
  int p1(0), d1(0);
  for (int i(0); i < N; ++i) {
    p1 = (p1 * 2 + (X[i] - '0')) % (pop + 1);
    if (pop > 1) {
      d1 = (d1 * 2 + (X[i] - '0')) % (pop - 1);
    }
  }
  for (int i(0); i < N; ++i) {
    long long n((X[i] == '0') ? p1 : d1), cnt(1);
    if (X[i] == '0') {
      n = (n + power(2, N - i - 1, pop + 1)) % (pop + 1);
    } else {
      if (pop == 1) {
        cnt = 0;
        n = 0;
      } else {
        n = (n - power(2, N - i - 1, pop - 1)) % (pop - 1);
        if (n < 0)
          n += pop - 1;
      }
    }
    while (n) {
      ++cnt;
      long long m(n);
      int a(__builtin_popcount(n));
      n %= a;
    }
    std::cout << cnt << "\n";
  }
  return 0;
}