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

#include <bits/stdc++.h>
#define ll long long
#define db double
#define ldb long double
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define ts puts("test  check")
#define xh1(a, b) for (int i = a; i < b; i++)
#define xh2(a, b) for (int j = a; j < b; j++)
#define stlbl(a) for (it = a.begin(); it != a.end(); it++)
#define dzkt                                                                   \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define mst(a, i) memset(a, i, sizeof(a))
#define iosks                                                                  \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int N = 2e5 + 10, M = 3e4 + 10, inf = 1e9 + 10, mod = 1e9 + 7;
const db pi = atan(1) * 4;
ll pow2(ll a, ll b) {
  ll s = 1;
  while (b) {
    if (b & 1)
      s = (s * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return s;
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
ll prime(ll x) {
  for (ll i = 2; i <= sqrt(x + 0.5); i++)
    if (x % i == 0)
      return 0;
  return 1;
}
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' && ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * f;
}
set<ll> st;
vector<ll> vec;

ll pop(ll x) {
  ll s = 0;
  while (x) {
    if (x & 1)
      s++;
    x /= 2;
  }
  return s;
}

ll n, c, x = 1, f[N];

string s;

ll v1[N], v2[N], x1 = 1, x2 = 1, a1 = 0, a2 = 0;

int main() {
  iosks;

  xh1(1, N) f[i] = f[i % pop(i)] + 1;

  cin >> n >> s;
  xh1(0, n) if (s[i] == '1') c++;

  if (c > 1) {
    xh1(1, n + 1) v1[i] = x1 % (c - 1), v2[i] = x2 % (c + 1),
                  x1 = (x1 * 2) % (c - 1), x2 = (x2 * 2) % (c + 1);

    xh1(0, n) if (s[i] == '1') a1 = (a1 + v1[n - i]) % (c - 1),
                               a2 = (a2 + v2[n - i]) % (c + 1);

    xh1(0, n) {
      if (s[i] == '0')
        cout << f[(a2 + v2[n - i]) % (c + 1)] + 1 << endl;
      else
        cout << f[(a1 - v1[n - i] + c - 1) % (c - 1)] + 1 << endl;
    }
  } else if (c == 0)
    xh1(0, n) cout << 1 << endl;
  else {
    xh1(1, n + 1) v2[i] = x2 % (c + 1), x2 = (x2 * 2) % (c + 1);
    xh1(0, n) if (s[i] == '1') a2 = (a2 + v2[n - i]) % (c + 1);
    xh1(0, n) {
      if (s[i] == '0')
        cout << f[(a2 + v2[n - i]) % (c + 1)] + 1 << endl;
      else
        cout << 0 << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define db double
#define ldb long double
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define ts puts("test  check")
#define xh1(a, b) for (int i = a; i < b; i++)
#define xh2(a, b) for (int j = a; j < b; j++)
#define stlbl(a) for (it = a.begin(); it != a.end(); it++)
#define dzkt                                                                   \\
  int T;                                                                       \\
  cin >> T;                                                                    \\
  while (T--)
#define mst(a, i) memset(a, i, sizeof(a))
#define iosks                                                                  \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int N = 2e5 + 10, M = 3e4 + 10, inf = 1e9 + 10, mod = 1e9 + 7;
const db pi = atan(1) * 4;
ll pow2(ll a, ll b) {
  ll s = 1;
  while (b) {
    if (b & 1)
      s = (s * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return s;
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
ll prime(ll x) {
  for (ll i = 2; i <= sqrt(x + 0.5); i++)
    if (x % i == 0)
      return 0;
  return 1;
}
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' && ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * f;
}
set<ll> st;
vector<ll> vec;

ll pop(ll x) {
  ll s = 0;
  while (x) {
    if (x & 1)
      s++;
    x /= 2;
  }
  return s;
}

ll n, c, x = 1, f[N];

string s;

ll v1[N], v2[N], x1 = 1, x2 = 1, a1 = 0, a2 = 0;

int main() {
  iosks;

  xh1(1, N) f[i] = f[i % pop(i)] + 1;

  cin >> n >> s;
  xh1(0, n) if (s[i] == '1') c++;

  if (c >= 1) {
    xh1(1, n + 1) v1[i] = x1 % (c - 1), v2[i] = x2 % (c + 1),
                  x1 = (x1 * 2) % (c - 1), x2 = (x2 * 2) % (c + 1);

    xh1(0, n) if (s[i] == '1') a1 = (a1 + v1[n - i]) % (c - 1),
                               a2 = (a2 + v2[n - i]) % (c + 1);

    xh1(0, n) {
      if (s[i] == '0')
        cout << f[(a2 + v2[n - i]) % (c + 1)] + 1 << endl;
      else
        cout << f[(a1 - v1[n - i] + c - 1) % (c - 1)] + 1 << endl;
    }
  } else if (c == 0)
    xh1(0, n) cout << 1 << endl;
  return 0;
}