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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD;

ll powmod(ll x, ll n, ll MOD) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1) {
      ret *= x;
      ret %= MOD;
      n -= 1;
    } else {
      x *= x;
      x %= MOD;
      n >>= 1;
    }
  }
  return ret;
}

signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }
  ll num = 0;
  ll num2 = 0;
  rep(i, n) {
    if (s[i] == '1' && cnt - 1 > 0)
      num += powmod(2LL, (n - 1 - i), cnt - 1); // 0->1
    if (s[i] == '1')
      num2 += powmod(2LL, (n - 1 - i), cnt + 1); // 1->0
  }
  rep(i, n) {
    int ans = 0;
    if (s[i] == '0') {
      ll x = num2;
      x = (x + powmod(2LL, (n - 1 - i), cnt + 1)) % (cnt + 1);
      ans++;
      while (x != 0) {
        ans++;
        int md = __builtin_popcountll(x);
        x %= md;
      }
      cout << ans << endl;
    } else {
      if (cnt == 1) {
        cout << 0 << endl;
      } else {
        ll x = num;
        x = (x - powmod(2LL, (n - 1 - i), cnt - 1) + (cnt - 1)) % (cnt - 1);
        ans++;
        while (x != 0) {
          ans++;
          int md = __builtin_popcountll(x);
          x %= md;
        }
        cout << ans << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
// vector<vector<ll>> dp(n,vector<ll>(n))
// exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD)
// % MOD;
const ll MOD = 1e9 + 7;
ll powmod(ll x, ll n, ll MOD) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1) {
      ret *= x;
      ret %= MOD;
      n -= 1;
    } else {
      x *= x;
      x %= MOD;
      n >>= 1;
    }
  }
  return ret;
}
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }
  ll num = 0;
  ll num2 = 0;
  rep(i, n) {
    if (s[i] == '1')
      num += powmod(2LL, (n - 1 - i), cnt - 1); // 0->1
    if (s[i] == '1')
      num2 += powmod(2LL, (n - 1 - i), cnt + 1); // 1->0
  }
  rep(i, n) {
    int ans = 0;
    if (s[i] == '0') {
      ll x = num2;
      x = (x + powmod(2LL, (n - 1 - i), cnt + 1) + (cnt + 1)) % (cnt + 1);
      ans++;
      while (x != 0) {
        ans++;
        int md = __builtin_popcountll(x);
        x %= md;
      }
      cout << ans << endl;
    } else {
      if (cnt == 1) {
        cout << ans << endl;
      } else {
        ll x = num;
        x = (x - powmod(2LL, (n - 1 - i), cnt - 1) + (cnt - 1)) % (cnt - 1);
        ans++;
        while (x != 0) {
          ans++;
          int md = __builtin_popcountll(x);
          x %= md;
        }
      }
      cout << ans << endl;
    }
  }
}