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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

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

int main() {
  ll i, j;
  ll n;
  string x;
  cin >> n >> x;
  ll cnt = 0;
  for (i = 0; i < n; ++i) {
    if (x.at(i) == '1')
      ++cnt;
  }
  ll plusspare = 0;
  ll minusspare = 0;
  ll tmp = cnt + 1;
  ll beki2 = 1;
  for (i = n - 1; i >= 0; --i) {
    if (x.at(i) == '1')
      plusspare += beki2;
    beki2 *= 2;
    beki2 %= tmp;
    plusspare %= tmp;
  }
  tmp -= 2;
  if (tmp == 0)
    minusspare = 0;
  else {
    beki2 = 1;
    for (i = n - 1; i >= 0; --i) {
      if (x.at(i) == '1')
        minusspare += beki2;
      beki2 *= 2;
      beki2 %= tmp;
      minusspare %= tmp;
    }
  }
  for (i = 0; i < n; ++i) {
    if (x.at(i) == '0') {
      tmp = cnt + 1;
      ll spare = plusspare + pow_mod(2ll, n - 1 - i, tmp);
      ll ans = 0;
      while (spare % tmp) {
        spare = spare % tmp;
        ++ans;
        tmp = 0;
        for (j = 0; j < 32; ++j) {
          if ((1ll << j) & spare)
            ++tmp;
        }
      }
      ++ans;
      cout << ans << endl;
    } else {
      tmp = cnt - 1;
      if (tmp == 0) {
        cout << 0 << endl;
        continue;
      }
      ll tmpspare = pow_mod(2ll, n - 1 - i, tmp);
      ll spare = minusspare + tmp - tmpspare;
      ll ans = 0;
      while (spare % tmp) {
        spare = spare % tmp;
        ++ans;
        tmp = 0;
        for (j = 0; j < 32; ++j) {
          if ((1ll << j) & spare)
            ++tmp;
        }
      }
      ++ans;
      cout << ans << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

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

int main() {
  ll i, j;
  ll n;
  string x;
  cin >> n >> x;
  ll cnt = 0;
  for (i = 0; i < n; ++i) {
    if (x.at(i) == '1')
      ++cnt;
  }
  ll plusspare = 0;
  ll minusspare = 0;
  ll tmp = cnt + 1;
  ll beki2 = 1;
  for (i = n - 1; i >= 0; --i) {
    if (x.at(i) == '1')
      plusspare += beki2;
    beki2 *= 2;
    beki2 %= tmp;
    plusspare %= tmp;
  }
  tmp -= 2;
  if (tmp == 0)
    minusspare = 0;
  {
    beki2 = 1;
    for (i = n - 1; i >= 0; --i) {
      if (x.at(i) == '1')
        minusspare += beki2;
      beki2 *= 2;
      beki2 %= tmp;
      minusspare %= tmp;
    }
  }
  for (i = 0; i < n; ++i) {
    if (x.at(i) == '0') {
      tmp = cnt + 1;
      ll spare = plusspare + pow_mod(2ll, n - 1 - i, tmp);
      ll ans = 0;
      while (spare % tmp) {
        spare = spare % tmp;
        ++ans;
        tmp = 0;
        for (j = 0; j < 32; ++j) {
          if ((1ll << j) & spare)
            ++tmp;
        }
      }
      ++ans;
      cout << ans << endl;
    } else {
      tmp = cnt - 1;
      if (tmp == 0) {
        cout << 0 << endl;
        return 0;
      }
      ll tmpspare = pow_mod(2ll, n - 1 - i, tmp);
      ll spare = minusspare + tmp - tmpspare;
      ll ans = 0;
      while (spare % tmp) {
        spare = spare % tmp;
        ++ans;
        tmp = 0;
        for (j = 0; j < 32; ++j) {
          if ((1ll << j) & spare)
            ++tmp;
        }
      }
      ++ans;
      cout << ans << endl;
    }
  }

  return 0;
}