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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 200000
#define mod1 1000000007
#define mod2 1000000009
#define mod3 998244353
#define endl '\\n'
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
void Google(ll tt) { cout << "Case #" << tt << ": "; }

ll po(ll x, ll y, ll z) {
  if (y == 0)
    return 1 % z;
  ll pp = po(x, y / 2, z);
  if (y % 2 == 0)
    return (pp * pp) % z;
  else
    return (((pp * pp) % z) * (x % z)) % z;
}
int main() {
  IO clock_t begin = clock();
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      ans++;
  }
  // cout<<ans<<endl;
  ll m1 = ans - 1, m2 = ans + 1, ss = 0, sss = 0;
  for (ll i = 0; i < n; i++) {
    if (m1 > 0 && s[i] == '1')
      ss = (ss + po(2, n - i - 1, m1));
    if (m2 > 0 && s[i] == '1')
      sss = (sss + po(2, n - i - 1, m2));
  }
  // cout<<ss<<' '<<sss<<endl;
  for (ll i = 0; i < n; i++) {
    ll cnt = 0, sum;
    if (s[i] == '0') {
      sum = sss;
      sum += po(2, n - i - 1, m2);
      sum %= m2;
      sum += m2;
      sum %= m2;
      cnt++;
      while (sum > 0) {
        ll bits = __builtin_popcount(sum);
        cnt++;
        if (sum % bits == 0)
          break;
        sum %= bits;
      }
      // cout<<cnt<<endl;
    } else {
      if (m1 <= 0) {
        cout << "0 ";
        continue;
      }
      sum = ss;
      sum -= po(2, n - i - 1, m1);
      sum %= m1;
      sum += m1;
      sum %= m1;
      // cout<<sum<<endl;
      cnt++;
      while (sum > 0) {
        ll bits = __builtin_popcount(sum);
        cnt++;
        if (sum % bits == 0)
          break;
        sum %= bits;
      }
    }
    cout << cnt << ' ';
  }
  // cout<<double(clock() - begin)/CLOCKS_PER_SEC<<endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 200000
#define mod1 1000000007
#define mod2 1000000009
#define mod3 998244353
#define endl '\\n'
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
void Google(ll tt) { cout << "Case #" << tt << ": "; }

ll po(ll x, ll y, ll z) {
  if (y == 0)
    return 1 % z;
  ll pp = po(x, y / 2, z);
  if (y % 2 == 0)
    return (pp * pp) % z;
  else
    return (((pp * pp) % z) * (x % z)) % z;
}
int main() {
  IO clock_t begin = clock();
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      ans++;
  }
  // cout<<ans<<endl;
  ll m1 = ans - 1, m2 = ans + 1, ss = 0, sss = 0;
  for (ll i = 0; i < n; i++) {
    if (m1 > 0 && s[i] == '1')
      ss = (ss + po(2, n - i - 1, m1));
    if (m2 > 0 && s[i] == '1')
      sss = (sss + po(2, n - i - 1, m2));
  }
  // cout<<ss<<' '<<sss<<endl;
  for (ll i = 0; i < n; i++) {
    ll cnt = 0, sum;
    if (s[i] == '0') {
      sum = sss;
      sum += po(2, n - i - 1, m2);
      sum += m2;
      sum %= m2;
      cnt++;
      while (sum > 0) {
        ll bits = __builtin_popcount(sum);
        cnt++;
        if (sum % bits == 0)
          break;
        sum %= bits;
      }
      // cout<<cnt<<endl;
    } else {
      sum = ss;
      sum -= po(2, n - i - 1, m1);
      sum %= m1;
      sum += m1;
      sum %= m1;
      // cout<<sum<<endl;
      cnt++;
      while (sum > 0) {
        ll bits = __builtin_popcount(sum);
        cnt++;
        if (sum % bits == 0)
          break;
        sum %= bits;
      }
    }
    cout << cnt << ' ';
  }
  // cout<<double(clock() - begin)/CLOCKS_PER_SEC<<endl;
  return 0;
}