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

#include <iostream>

using namespace std;
const int maxn = 2e5 + 10;
int n, f[maxn];
int ans[3];
char s[maxn];
int l[4][maxn];
int _cnt(int x) {
  int rec = 0;
  while (x > 0) {
    if (x & 1)
      rec++;
    x >>= 1;
  }
  return rec;
}
int main() {
  cin >> n >> s;
  l[0][0] = l[1][0] = l[2][0] = 1;
  f[0] = 0;
  for (int i = 1; i < maxn; ++i) {
    f[i] = f[i % _cnt(i)] + 1;
  }
  // for(int i = 1; i < 10; ++i) cout << f[i] << endl;
  int cnt1 = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '1')
      cnt1++;
  }
  if (cnt1 == 0) {
    l[0][0] = l[1][0] = l[2][0] = 0;
  }
  if (cnt1 == 1) {
    l[0][0] = l[1][0] = 0;
  }
  if (cnt1 == 2) {
    l[0][0] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    if (cnt1 > 1)
      l[0][i] = (l[0][i - 1] << 1) % (cnt1 - 1);
    if (cnt1 > 0)
      l[1][i] = (l[1][i - 1] << 1) % (cnt1);
    l[2][i] = (l[2][i - 1] << 1) % (cnt1 + 1);
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0')
      continue;
    if (cnt1 > 1) {
      ans[0] += l[0][n - 1 - i];
      ans[0] %= (cnt1 - 1);
    }
    if (cnt1 > 0) {
      ans[1] += l[1][n - 1 - i];
      ans[1] %= cnt1;
    }
    ans[2] += l[2][n - 1 - i];
    ans[2] %= (cnt1 + 1);
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (cnt1 == 1) {
        cout << "0\\n";
        continue;
      }
      int x = ans[0];
      int p = ((x - l[0][n - 1 - i]) % (cnt1 - 1) + cnt1 - 1) % (cnt1 - 1);
      // cout << p << "\\n";
      cout << f[p] + 1 << "\\n";
    }
    if (s[i] == '0') {
      int x = ans[2];
      int p = ((x + l[2][n - 1 - i]) % (cnt1 + 1) + cnt1 + 1) % (cnt1 + 1);
      // cout << p << "\\n";
      cout << f[p] + 1 << "\\n";
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;
const int maxn = 5;
int n, f[maxn];
int ans[3];
char s[maxn];
int l[4][maxn];
int _cnt(int x) {
  int rec = 0;
  while (x > 0) {
    if (x & 1)
      rec++;
    x >>= 1;
  }
  return rec;
}

int main() {
  cin >> n >> s;
  l[0][0] = l[1][0] = l[2][0] = 1;
  f[0] = 0;
  for (int i = 1; i < maxn; ++i) {
    f[i] = f[i % _cnt(i)] + 1;
  }
  // for(int i = 1; i < 10; ++i) cout << f[i] << endl;
  int cnt1 = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '1')
      cnt1++;
  }
  for (int i = 1; i <= cnt1; ++i) {
    if (cnt1 > 1)
      l[0][i] = (l[0][i - 1] << 1) % (cnt1 - 1);
    if (cnt1 > 0)
      l[1][i] = (l[1][i - 1] << 1) % (cnt1);
    l[2][i] = (l[2][i - 1] << 1) % (cnt1 + 1);
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0')
      continue;
    if (cnt1 > 1) {
      ans[0] += l[0][n - 1 - i];
      ans[0] %= (cnt1 - 1);
    }
    if (cnt1 > 0) {
      ans[1] += l[1][n - 1 - i];
      ans[1] %= cnt1;
    }
    ans[2] += l[2][n - 1 - i];
    ans[2] %= cnt1 + 1;
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (cnt1 == 1) {
        cout << "0\\n";
        continue;
      }
      int x = ans[0];
      cout << f[((x - l[0][n - 1 - i]) % (cnt1 - 1) + cnt1 - 1) % (cnt1 - 1)] +
                  1
           << "\\n";
    }
    if (s[i] == '0') {
      int x = ans[2];
      cout << f[((x - l[2][n - 1 - i]) % (cnt1 + 1) + cnt1 + 1) % (cnt1 + 1)] +
                  1
           << "\\n";
    }
  }
}