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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \\
  {                                                                            \\
    ++recur_depth;                                                             \\
    auto x_ = x;                                                               \\
    --recur_depth;                                                             \\
    cerr << string(recur_depth, '\\t') << "\\e[91m" << __func__ << ":"           \\
         << __LINE__ << "\\t" << #x << " = " << x_ << "\\e[39m" << endl;         \\
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt1 = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt1++;
  }
  // this is the initial cnt1s

  auto binpow = [](ll a, ll b, ll m) {
    ll res = 1;
    while (b > 0) {
      if (b & 1)
        res = res * a % m;
      a = a * a % m;
      b >>= 1;
    }
    return res;
  };
  ll rem[2];
  rem[0] = rem[1] = 0;
  vector<ll> remans(n);
  for (ll i = 0; i < n; i++) {
    if (cnt1 - 1 != 0)
      rem[0] =
          (rem[0] + (s[i] - '0') * binpow(2, n - i - 1, cnt1 - 1)) % (cnt1 - 1);
    else
      rem[0] = -1;
    rem[1] =
        (rem[1] + (s[i] - '0') * binpow(2, n - i - 1, cnt1 + 1)) % (cnt1 + 1);
  }

  for (ll i = 0; i < n; i++) {
    if (s[i] == '0') {
      remans[i] = (rem[1] + binpow(2, n - i - 1, cnt1 + 1)) % (cnt1 + 1);
    } else {
      if (rem[0] != -1)
        remans[i] =
            ((cnt1 - 1) + rem[0] - binpow(2, n - i - 1, cnt1 - 1)) % (cnt1 - 1);
      else
        remans[i] = -1;
    }
  }
  // dbg(remans);
  for (ll i = 0; i < n; i++) {
    if (remans[i] == -1) {
      cout << 0 << endl;
      continue;
    }
    ll ans = 1;
    while (remans[i] > 0) {
      ll div = __builtin_popcount(remans[i]);
      remans[i] %= div;
      ans++;
    }
    cout << ans << endl;
  }
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t;cin >> t;while(t--)
  solve();
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \\
  {                                                                            \\
    ++recur_depth;                                                             \\
    auto x_ = x;                                                               \\
    --recur_depth;                                                             \\
    cerr << string(recur_depth, '\\t') << "\\e[91m" << __func__ << ":"           \\
         << __LINE__ << "\\t" << #x << " = " << x_ << "\\e[39m" << endl;         \\
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt1 = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt1++;
  }
  // this is the initial cnt1s

  auto binpow = [](ll a, ll b, ll m) {
    ll res = 1;
    while (b > 0) {
      if (b & 1)
        res = res * a % m;
      a = a * a % m;
      b >>= 1;
    }
    return res;
  };
  ll rem[2];
  rem[0] = rem[1] = 0;
  for (ll i = 0; i < n; i++) {
    rem[0] =
        (rem[0] + (s[i] - '0') * binpow(2, n - i - 1, cnt1 - 1)) % (cnt1 - 1);
    rem[1] =
        (rem[1] + (s[i] - '0') * binpow(2, n - i - 1, cnt1 + 1)) % (cnt1 + 1);
  }

  vector<ll> remans(n);
  for (ll i = 0; i < n; i++) {
    if (s[i] == '0') {
      remans[i] = (rem[1] + binpow(2, n - i - 1, cnt1 + 1)) % (cnt1 + 1);
    } else {
      remans[i] =
          ((cnt1 - 1) + rem[0] - binpow(2, n - i - 1, cnt1 - 1)) % (cnt1 - 1);
    }
  }
  // dbg(remans);
  for (ll i = 0; i < n; i++) {
    ll ans = 1;
    while (remans[i] > 0) {
      ll div = __builtin_popcount(remans[i]);
      remans[i] %= div;
      ans++;
    }
    cout << ans << endl;
  }
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t;cin >> t;while(t--)
  solve();
}