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

#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define CSE(x, y) memset(x, y, sizeof(x))
#define INF 0x3f3f3f3f
#define Abs(x) (x >= 0 ? x : (-x))
#define FAST                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;

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

const int maxn = 211111;
int sp[] = {1,    2,    4,    8,    16,    32,    64,    128,    256,   512,
            1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144};
char str[maxn];
int mod1[maxn], mod2[maxn];

int popcount(int x) {
  int ans = 0;
  for (int i = 0; i < 19; i++) {
    if (x & sp[i]) {
      ans++;
    }
  }
  return ans;
}

int main() {
  int n, all = 0;
  cin >> n;
  cin >> str;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1')
      all++;
  }
  int all1 = all - 1, all2 = all + 1;
  if (all1)
    mod1[n - 1] = 1 % all1;
  else
    mod1[n - 1] = 0;
  mod2[n - 1] = 1 % all2;
  for (int i = n - 2; i >= 0; i--) {
    if (all1)
      mod1[i] = mod1[i + 1] * 2 % all1;
    else
      mod1[i] = 0;
    mod2[i] = mod2[i + 1] * 2 % all2;
  }
  ll summ1 = 0, summ2 = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1') {
      summ1 += mod1[i];
      summ2 += mod2[i];
    }
  }
  for (int i = 0; i < n; i++) {
    int pp;
    if (str[i] == '1') {
      if (all1 == 0) {
        cout << "0" << endl;
        continue;
      }
      pp = (summ1 - mod1[i]) % all1;
    } else {
      pp = (summ2 + mod2[i]) % all2;
    }
    int ans = 1;
    while (pp) {
      pp = pp % popcount(pp);
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lowbit(x) (x & (-x))
#define CSE(x, y) memset(x, y, sizeof(x))
#define INF 0x3f3f3f3f
#define Abs(x) (x >= 0 ? x : (-x))
#define FAST                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;

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

const int maxn = 211111;
int sp[] = {1,    2,    4,    8,    16,    32,    64,    128,    256,   512,
            1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144};
char str[maxn];
int mod1[maxn], mod2[maxn];

int popcount(int x) {
  int ans = 0;
  for (int i = 0; i < 19; i++) {
    if (x & sp[i]) {
      ans++;
    }
  }
  return ans;
}

int main() {
  int n, all = 0;
  cin >> n;
  cin >> str;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1')
      all++;
  }
  int all1 = all - 1, all2 = all + 1;
  mod1[n - 1] = 1 % all1;
  mod2[n - 1] = 1 % all2;
  for (int i = n - 2; i >= 0; i--) {
    mod1[i] = mod1[i + 1] * 2 % all1;
    mod2[i] = mod2[i + 1] * 2 % all2;
  }
  ll summ1 = 0, summ2 = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == '1') {
      summ1 += mod1[i];
      summ2 += mod2[i];
    }
  }
  for (int i = 0; i < n; i++) {
    int pp;
    if (str[i] == '1') {
      pp = (summ1 - mod1[i]) % (all - 1);
    } else {
      pp = (summ2 + mod2[i]) % (all + 1);
    }
    int ans = 1;
    while (pp) {
      pp = pp % popcount(pp);
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}