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

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
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;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

ll popcount(ll n) {
  ll count = 0;
  while (n != 0) {
    if (n % 2 == 1)
      count++;
    n /= 2;
  }
  return count;
}

ll takemod(string x, ll mod) {
  ll div = 1;
  ll ans = 0;
  rep(i, x.size()) {
    if (x[x.size() - 1 - i] == '1')
      ans += div % mod;
    div *= 2;
    div %= mod;
    ans %= mod;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll count = 0;
  vll takedmod(3);
  vll twopow1(n + 3, 0);
  vll twopow2(n + 3, 0);
  ll di = 1;
  rep(i, n) {
    if (x[i] == '1') {
      count++;
    }
  }
  rep(i, n + 1) {
    ll mod = count - 1;
    if (mod == 0)
      break;
    twopow1[i] = di;
    di *= 2;
    di %= mod;
  }
  di = 1;
  rep(i, n + 1) {
    ll mod = count + 1;
    twopow2[i] = di;
    di *= 2;
    di %= mod;
  }
  rep(i, 3) {
    if (i == 1)
      continue;
    if (count - 1 + i > 0)
      takedmod[i] = takemod(x, count - 1 + i);
  }
  rep(i, n) {
    ll ans = 0;
    ll nn = 0;
    ll co = count;
    if (x[i] == '1') {
      co--;
      if (co == 0) {
        cout << 0 << endl;
        continue;
      }
      nn = takedmod[0];
      nn -= twopow1[n - i - 1];
      nn %= co;
      if (nn < 0)
        nn += co;
    } else {
      co++;
      nn = takedmod[2];
      nn += twopow2[n - i - 1];
      nn %= co;
    }
    co = popcount(nn);
    ans++;
    while (nn != 0) {
      nn %= co;
      co = popcount(nn);
      ans++;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
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;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp

ll popcount(ll n) {
  ll count = 0;
  while (n != 0) {
    if (n % 2 == 1)
      count++;
    n /= 2;
  }
  return count;
}

ll takemod(string x, ll mod) {
  ll div = 1;
  ll ans = 0;
  rep(i, x.size()) {
    if (x[x.size() - 1 - i] == '1')
      ans += div % mod;
    div *= 2;
    div %= mod;
    ans %= mod;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  string x;
  cin >> x;
  ll count = 0;
  vll takedmod(3);
  vll twopow1(n + 3, 0);
  vll twopow2(n + 3, 0);
  ll di = 1;
  rep(i, n) {
    if (x[i] == '1') {
      count++;
    }
  }
  rep(i, n + 1) {
    ll mod = count - 1;
    twopow1[i] = di;
    di *= 2;
    di %= mod;
  }
  di = 1;
  rep(i, n + 1) {
    ll mod = count + 1;
    twopow2[i] = di;
    di *= 2;
    di %= mod;
  }
  rep(i, 3) {
    if (i == 1)
      continue;
    if (count - 1 + i > 0)
      takedmod[i] = takemod(x, count - 1 + i);
  }
  rep(i, n) {
    ll ans = 0;
    ll nn = 0;
    ll co = count;
    if (x[i] == '1') {
      co--;
      nn = takedmod[0];
      nn -= twopow1[n - i - 1];
      nn %= co;
      if (nn < 0)
        nn += co;
    } else {
      co++;
      nn = takedmod[2];
      nn += twopow2[n - i - 1];
      nn %= co;
    }
    co = popcount(nn);
    ans++;
    while (nn != 0) {
      nn %= co;
      co = popcount(nn);
      ans++;
    }
    cout << ans << endl;
  }
}