Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

void solve(long long N, std::string X) {
  ll originalOnes = 0;
  for (int i = N - 1; i >= 0; --i) {
    if (X[i] == '1') {
      originalOnes++;
    }
  }

  // for(int i=N-1;i>=0;--i){
  //     plusNum*=2;
  //     minusNum*=2;
  //     if(X[i] == '1'){
  //         plusNum++;
  //         minusNum++;
  //     }
  //     plusNum%=(originalOnes+1);
  //     minusNum%=(originalOnes-1);
  // }
  ll plusNum = 0;
  ll minusNum = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      plusNum += mod_pow(2ll, N - 1 - i, originalOnes + 1);
      minusNum += mod_pow(2ll, N - 1 - i, originalOnes - 1);
    }
    plusNum %= (originalOnes + 1);
    if (originalOnes == 1) {
      minusNum = 0;
    } else {
      minusNum %= (originalOnes - 1);
    }
  }
  // cerr <<"debug=" <<debug << endl;
  // cerr <<"plus=" <<plusNum <<"minusNum =" <<minusNum << endl;
  for (ll i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll ones = originalOnes + 1;
      ll num = plusNum + mod_pow(2ll, N - 1 - i, ones);
      num %= ones;
      ll ans = 1;
      while (num > 0) {
        num %= __builtin_popcount(num);
        ans++;
      }
      cout << ans << "\n";
    } else {
      if (originalOnes == 1) {
        cout << 0 << "\n";
        continue;
      }
      ll ones = originalOnes - 1;
      ll num = minusNum + ones - mod_pow(2ll, N - 1 - i, ones);
      // cerr <<"pow=" <<mod_pow(2ll,N-1-i,ones)<< endl;
      // cerr <<"i="<<i <<"num=" <<num<<"ones=" <<ones<< endl;
      num %= ones;
      ll ans = 1;
      while (num > 0) {
        num %= __builtin_popcount(num);
        ans++;
      }
      cout << ans << "\n";
    }
  }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  std::string X;
  std::cin >> X;
  solve(N, X);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

template <typename T> T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

void solve(long long N, std::string X) {
  ll originalOnes = 0;
  for (int i = N - 1; i >= 0; --i) {
    if (X[i] == '1') {
      originalOnes++;
    }
  }

  // for(int i=N-1;i>=0;--i){
  //     plusNum*=2;
  //     minusNum*=2;
  //     if(X[i] == '1'){
  //         plusNum++;
  //         minusNum++;
  //     }
  //     plusNum%=(originalOnes+1);
  //     minusNum%=(originalOnes-1);
  // }
  ll plusNum = 0;
  ll minusNum = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      plusNum += mod_pow(2ll, N - 1 - i, originalOnes + 1);
      if (originalOnes != 1) {
        minusNum += mod_pow(2ll, N - 1 - i, originalOnes - 1);
      }
    }
    plusNum %= (originalOnes + 1);
    if (originalOnes == 1) {
      minusNum = 0;
    } else {
      minusNum %= (originalOnes - 1);
    }
  }
  // cerr <<"debug=" <<debug << endl;
  // cerr <<"plus=" <<plusNum <<"minusNum =" <<minusNum << endl;
  for (ll i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll ones = originalOnes + 1;
      ll num = plusNum + mod_pow(2ll, N - 1 - i, ones);
      num %= ones;
      ll ans = 1;
      while (num > 0) {
        num %= __builtin_popcount(num);
        ans++;
      }
      cout << ans << "\n";
    } else {
      if (originalOnes == 1) {
        cout << 0 << "\n";
        continue;
      }
      ll ones = originalOnes - 1;
      ll num = minusNum + ones - mod_pow(2ll, N - 1 - i, ones);
      // cerr <<"pow=" <<mod_pow(2ll,N-1-i,ones)<< endl;
      // cerr <<"i="<<i <<"num=" <<num<<"ones=" <<ones<< endl;
      num %= ones;
      ll ans = 1;
      while (num > 0) {
        num %= __builtin_popcount(num);
        ans++;
      }
      cout << ans << "\n";
    }
  }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  long long N;
  scanf("%lld", &N);
  std::string X;
  std::cin >> X;
  solve(N, X);
  return 0;
}