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

/**
 *    author:  yuya1234
 *    created: 13.07.2020 09:28:05
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int count_bits(int n) {
  n = (n & 0x55555555) + (n >> 1 & 0x55555555);
  n = (n & 0x33333333) + (n >> 2 & 0x33333333);
  n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f);
  n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff);
  n = (n & 0x0000ffff) + (n >> 16 & 0x0000ffff);
  return n;
}

ll modpow(ll a, ll n, ll mod) {
  if (mod <= 0)
    return 0;
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int solve(int n) {
  if (n == 0)
    return 0;
  return solve(n % count_bits(n)) + 1;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15);

  ll n;
  cin >> n;

  string x;
  cin >> x;

  ll p = 0;
  REP(i, n) {
    if (x[i] == '1')
      p++;
  }

  ll a1 = 0, a2 = 0;
  REP(i, n) {
    if (x[i] == '1') {
      if (p == 1) {
        a1 = 0;
      } else {
        a1 = (a1 + modpow(2, n - i - 1, p - 1)) % (p - 1);
      }
      a2 = (a2 + modpow(2, n - i - 1, p + 1)) % (p + 1);
    }
  }

  REP(i, n) {
    if (x[i] == '1') {
      if (p == 1) {
        cout << 0 << endl;
      } else {
        cout << 1 + solve((a1 - modpow(2, n - i - 1, p - 1) + p - 1) % (p - 1))
             << endl;
      }
    } else {
      cout << 1 + solve((a2 + modpow(2, n - i - 1, p + 1)) % (p + 1)) << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

/**
 *    author:  yuya1234
 *    created: 13.07.2020 09:28:05
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int cnt1(int n) {
  int cnt = 0;

  while (n != 0) {
    if (n % 2 == 1)
      cnt++;
    n /= 2;
  }

  return cnt;
}

int popcount(int n) {
  int cnt, re = 0;
  while (n != 0) {
    cnt = cnt1(n);
    n %= cnt;
    re++;
  }
  return re;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15);

  int n;
  cin >> n;

  string x;
  cin >> x;

  int cnt = 0, bit = 0;
  REP(i, n) {
    if (x[i] == '1') {
      cnt++;
      bit += pow(2, SZ(x) - i - 1);
    }
  }

  string xx;
  int flg1, tmp, ans;
  REP(i, n) {
    xx = x;
    tmp = bit;
    ans = 0;
    if (xx[i] == '0') {
      xx[i] = '1';
      flg1 = 1;
      tmp += pow(2, SZ(x) - i - 1);
    } else {
      xx[i] = '0';
      flg1 = -1;
      tmp -= pow(2, SZ(x) - i - 1);
    }

    if (tmp != 0) {
      tmp = tmp % (cnt + flg1);
      ans++;
      ans += popcount(tmp);
    }

    cout << ans << endl;
  }

  return 0;
}