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

#include <bits/stdc++.h>

#define FLASH                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long int
#define MAX 1e9
#define MIN -1e9
#define mod 1000000007
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << '=' << (x) << endl;
#define N 2002002
using namespace std;

int n;
string s;
int power1[N], power2[N];

void precompute(int cnt) {
  int k = cnt - 1;
  if (k != 0) {
    power1[n - 1] = 1 % k;
    for (int i = n - 2; i >= 0; i--) {
      power1[i] = (power1[i + 1] % k) * (2 % k);
      power1[i] %= k;
    }
  }
  k = cnt + 1;
  power2[n - 1] = 1 % k;
  for (int i = n - 2; i >= 0; i--) {
    power2[i] = power2[i + 1] * (2 % k);
    power2[i] %= k;
  }
}

int getmod1(string str, int n, int k) {
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n && j >= 0) {
    if (str[j] == '1') {
      res += (power1[j]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
int getmod2(string str, int n, int k) {
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n && j >= 0) {
    if (str[j] == '1') {
      res += (power2[j]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
int count(int n) {
  int cnt = 0;
  while (n > 0) {
    cnt++;
    n = n % (__builtin_popcountll(n));
  }
  return cnt;
}

int32_t main() {
  cin >> n;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (s[i] == '1');
  precompute(cnt);
  int k1, k2;
  if (cnt - 1 != 0)
    k1 = getmod1(s, n, cnt - 1);
  k2 = getmod2(s, n, cnt + 1);
  if (cnt - 1 == 0)
    k1 = 0;
  int temp1 = k1;
  int temp2 = k2;
  for (int i = 0; i < n; i++) {
    if (cnt - 1 == 0 && s[i] == '1') {
      cout << "0\\n";
      continue;
    }
    int ans = 0;
    k1 = temp1, k2 = temp2;
    if (s[i] == '1') {
      k1 -= power1[i];
      if (cnt - 1 > 0)
        while (k1 < 0)
          k1 += (cnt - 1);
      ans = count(k1);
    } else {
      k2 += power2[i];
      k2 %= (cnt + 1);
      ans = count(k2);
    }

    cout << ans + 1 << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define FLASH                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long int
#define MAX 1e9
#define MIN -1e9
#define mod 1000000007
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << '=' << (x) << endl;
#define N 200200
using namespace std;

int n;
string s;
int power1[N], power2[N];

void precompute(int cnt) {
  int k = cnt - 1;
  power1[n - 1] = 1 % k;
  for (int i = n - 2; i >= 0; i--) {
    power1[i] = (power1[i + 1] % k) * (2 % k);
    power1[i] %= k;
  }
  k = cnt + 1;
  power2[n - 1] = 1 % k;
  for (int i = n - 2; i >= 0; i--) {
    power2[i] = power2[i + 1] * (2 % k);
    power2[i] %= k;
  }
}

int getmod1(string str, int n, int k) {
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n && j >= 0) {
    if (str[j] == '1') {
      res += (power1[j]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
int getmod2(string str, int n, int k) {
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n && j >= 0) {
    if (str[j] == '1') {
      res += (power2[j]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}
int count(int n) {
  int cnt = 0;
  while (n > 0) {
    cnt++;
    n = n % (__builtin_popcountll(n));
  }
  return cnt;
}

int32_t main() {
  cin >> n;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (s[i] == '1');
  precompute(cnt);
  int k1 = getmod1(s, n, cnt - 1);
  int k2 = getmod2(s, n, cnt + 1);
  int temp1 = k1;
  int temp2 = k2;
  for (int i = 0; i < n; i++) {
    int ans = 0;
    k1 = temp1, k2 = temp2;
    if (s[i] == '1') {
      k1 -= power1[i];
      if (cnt - 1 > 0)
        while (k1 < 0)
          k1 += (cnt - 1);
      ans = count(k1);
    } else {
      k2 += power2[i];
      k2 %= (cnt + 1);
      ans = count(k2);
    }

    cout << ans + 1 << '\\n';
  }
  return 0;
}