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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int cnt = 0;
  string s;
  cin >> s;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }

  if (cnt == 0 or cnt == 1) {
    if (cnt == 0) {
      rep(i, n) { cout << 1 << endl; }
      return 0;
    } else {
      if (s[n - 1] == '1') {
        rep(i, n - 1) { cout << 2 << endl; }
        cout << 0 << endl;
      } else {
        rep(i, n - 1) {
          if (s[i] == '1')
            cout << 0 << endl;
          else
            cout << 1 << endl;
        }
        cout << 2 << endl;
      }
      return 0;
    }
  }

  vi sum(2);
  rep(i, n) {
    sum[0] *= 2;
    sum[1] *= 2;
    sum[0] %= (cnt + 1);
    sum[1] %= (cnt - 1);
    if (s[i] == '1') {
      sum[0] += 1;
      sum[1] += 1;
    }
  }

  rep(i, n) {
    int x = 0;
    if (s[i] == '1') {
      x = 1;
    }
    ll ss = sum[x];
    function<int(int)> pow = [&](int t) {
      if (!t)
        return 1;
      int a = pow(t >> 1);
      a = ll(a) * a % (cnt + 1 - x * 2);
      if (t & 1)
        a *= 2;
      a %= (cnt + 1 - x * 2);
      return a;
    };
    if (x) {
      ss -= pow(n - i - 1);
      ss = (ss + (cnt + 1 - x * 2)) % (cnt + 1 - x * 2);
      int res = 1;
      while (ss) {
        ss %= __builtin_popcountll(ss);
        res++;
      }
      cout << res << '\\n';
    } else {
      ss += pow(n - i - 1);
      ss = (ss + (cnt + 1 - x * 2)) % (cnt + 1 - x * 2);
      int res = 1;
      while (ss) {
        ss %= __builtin_popcountll(ss);
        res++;
      }
      cout << res << '\\n';
    }
  }
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int cnt = 0;
  string s;
  cin >> s;
  rep(i, n) {
    if (s[i] == '1')
      cnt++;
  }

  if (cnt == 0 or cnt == 1) {
    if (cnt == 0) {
      rep(i, n) { cout << 1 << endl; }
      return 0;
    } else {
      if (s[n - 1] == '1') {
        rep(i, n - 1) { cout << 2 << endl; }
        cout << 0 << endl;
      } else {
        rep(i, n) {
          if (s[i] == '1')
            cout << 0 << endl;
          else
            cout << 1 << endl;
        }
      }
    }
  }

  vi sum(2);
  rep(i, n) {
    sum[0] *= 2;
    sum[1] *= 2;
    sum[0] %= (cnt + 1);
    sum[1] %= (cnt - 1);
    if (s[i] == '1') {
      sum[0] += 1;
      sum[1] += 1;
    }
  }

  rep(i, n) {
    int x = 0;
    if (s[i] == '1') {
      x = 1;
    }
    ll ss = sum[x];
    function<int(int)> pow = [&](int t) {
      if (!t)
        return 1;
      int a = pow(t >> 1);
      a = ll(a) * a % (cnt + 1 - x * 2);
      if (t & 1)
        a *= 2;
      a %= (cnt + 1 - x * 2);
      return a;
    };
    if (x) {
      ss -= pow(n - i - 1);
      ss = (ss + (cnt + 1 - x * 2)) % (cnt + 1 - x * 2);
      int res = 1;
      while (ss) {
        ss %= __builtin_popcountll(ss);
        res++;
      }
      cout << res << '\\n';
    } else {
      ss += pow(n - i - 1);
      ss = (ss + (cnt + 1 - x * 2)) % (cnt + 1 - x * 2);
      int res = 1;
      while (ss) {
        ss %= __builtin_popcountll(ss);
        res++;
      }
      cout << res << '\\n';
    }
  }
}