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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;
using vvb = vector<vector<bool>>;
using vb = vector<bool>;
ll f(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll t = f(a, b / 2, p);
    return t * t % p;
  }
  return a * (f(a, b - 1, p)) % p;
}

//__builtin_popcount()
signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  if (n == 1) {
    if (s == "0")
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }
  string t = s;
  reverse(all(t));
  vl memo1(n), memo2(n);
  ll sum1 = 0, sum2 = 0;
  if (t[0] == '1')
    sum1 = 1, sum2 = 1;
  ll cnt = 0;
  rep(i, 0, n) cnt += (s[i] == '1');
  if (cnt == 1) {
  }
  memo1[0] = 1;
  memo2[0] = 1;
  rep(i, 1, n) {
    memo1[i] = memo1[i - 1] * 2 % (cnt + 1);
    if (cnt - 1 > 0)
      memo2[i] = memo2[i - 1] * 2 % (cnt - 1);
    if (t[i] == '1') {
      sum1 += memo1[i];
      sum1 %= (cnt + 1);
      sum2 += memo2[i];
      if (cnt - 1 > 0)
        sum2 %= (cnt - 1);
    }
  }
  vl ans(n);
  rep(i, 0, n) {
    ll j;
    if (t[i] == '0') {
      j = sum1;
      j += memo1[i];
      j %= (cnt + 1);
    } else {
      j = sum2;
      j -= memo2[i];
      if (j < 0)
        j += (cnt - 1);
      // if(cnt-1>0)j%=(cnt-1);
      if (cnt - 1 == 0) {
        ans[i] = 0;
        continue;
      }
    }

    ll add = 1;
    while (j > 0) {
      if (__builtin_popcount(j) == 0) {
        add++;
        break;
      }
      j %= __builtin_popcount(j);
      add++;
    }
    ans[i] = add;
  }
  reverse(all(ans));
  rep(i, 0, n) cout << ans[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;
using vvb = vector<vector<bool>>;
using vb = vector<bool>;
ll f(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll t = f(a, b / 2, p);
    return t * t % p;
  }
  return a * (f(a, b - 1, p)) % p;
}

//__builtin_popcount()
signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  if (n == 1) {
    if (s == "0")
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }
  string t = s;
  reverse(all(t));
  vl memo1(n), memo2(n);
  ll sum1 = 0, sum2 = 0;
  if (t[0] == '1')
    sum1 = 1, sum2 = 1;
  ll cnt = 0;
  rep(i, 0, n) cnt += (s[i] == '1');
  memo1[0] = 1;
  memo2[0] = 1;
  rep(i, 1, n) {
    memo1[i] = memo1[i - 1] * 2 % (cnt + 1);
    memo2[i] = memo2[i - 1] * 2 % (cnt - 1);
    if (t[i] == '1') {
      sum1 += memo1[i];
      sum1 %= (cnt + 1);
      sum2 += memo2[i];
      sum2 %= (cnt - 1);
    }
  }
  vl ans(n);
  rep(i, 0, n) {
    ll j;
    if (t[i] == '0') {
      j = sum1;
      j += memo1[i];
      j %= (cnt + 1);
    } else {
      j = sum2;
      j -= memo2[i];
      if (j < 0)
        j += (cnt - 1);
      j %= (cnt - 1);
    }

    ll add = 1;
    while (j > 0) {
      if (__builtin_popcount(j) == 0) {
        add++;
        break;
      }
      j %= __builtin_popcount(j);
      add++;
    }
    ans[i] = add;
  }
  reverse(all(ans));
  rep(i, 0, n) cout << ans[i] << endl;

  return 0;
}