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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i)                                                             \\
  for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
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;
}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N, num = 0;
  string X;
  cin >> N >> X;
  REP(i, N) {
    if (X[i] == '1')
      num++;
  }

  ll P[N + 1], Q[N + 1], PP = 0, QQ = 0;
  P[0] = 1;
  Q[0] = 1;
  for (int i = 1; i <= N; ++i) {
    if (num > 1)
      P[i] = P[i - 1] * 2 % (num - 1);
    Q[i] = Q[i - 1] * 2 % (num + 1);
    if (X[N - i] == '1') {
      if (num > 1)
        PP += P[i - 1];
      QQ += Q[i - 1];
    }
    if (num > 1)
      PP %= num - 1;
    QQ %= num + 1;
  }

  for (int i = 0; i < N; ++i) {
    ll cnt = num;
    if (X[i] == '1')
      cnt--;
    if (X[i] == '0')
      cnt++;
    if (cnt == 0) {
      co(0);
      continue;
    }

    ll L = 0;
    if (cnt == num - 1) {
      if (X[i] == '1')
        L = PP - P[N - 1 - i];
      else
        L = PP + P[N - 1 - i];
      L %= num - 1;
      if (L < 0)
        L += num - 1;
    } else {
      if (X[i] == '1')
        L = QQ - Q[N - 1 - i];
      else
        L = QQ + Q[N - 1 - i];
      L %= num + 1;
      if (L < 0)
        L += num + 1;
    }
    if (L == 0)
      co(1);
    else {
      ll ans = 1;
      while (L > 0) {
        cnt = 0;
        for (int j = 0; j < 30; ++j) {
          if (L & (1 << j))
            cnt++;
        }
        L %= cnt;
        ans++;
      }
      co(ans);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i)                                                             \\
  for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
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;
}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N;
  string X;
  cin >> N >> X;

  for (int i = 0; i < N; ++i) {
    ll cnt = 0;
    for (int j = 0; j < N; ++j) {
      if ((X[j] == '1' && j != i) || (X[j] == '0' && j == i))
        cnt++;
    }

    ll L = 0, RUI = 1;
    for (int j = N - 1; j >= 0; --j) {
      if ((X[j] == '1' && j != i) || (X[j] == '0' && j == i))
        L += RUI;
      RUI *= 2;
      RUI %= cnt;
      L %= cnt;
    }

    if (L == 0)
      co(1);
    else {
      ll ans = 1;
      while (L > 0) {
        cnt = 0;
        for (int j = 0; j < 20; ++j) {
          if (L & (1 << j))
            cnt++;
        }
        L %= cnt;
        ans++;
      }
      co(ans);
    }
  }

  return 0;
}