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

/* 	* In the name of GOD
 * Thanks God */
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define int long long
const int N = 201234;
int pow21[N], pow22[N], pow23[N];

int calc(int x) {
  int a[60];
  int y = x;
  int num = 0;
  for (int i = 0; i < 60; i++) {
    a[i] = y % 2;
    y /= 2;
    if (a[i])
      num++;
  }
  if (num == 0) {
    return 0;
  }
  if (x % num == 0) {
    return 1;
  }
  return 1 + calc(x % num);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  int x = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      x++;
  if (x == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << endl;
    return 0;
  }
  int y;
  y = 1;
  for (int i = 0; i < n; i++) {
    if (x == 1)
      continue;
    pow21[i] = y;
    pow21[i] %= (x - 1);
    y *= 2;
    y %= (x - 1);
  }
  y = 1;
  for (int i = 0; i < n; i++) {
    pow22[i] = y;
    pow22[i] %= (x);
    y *= 2;
    y %= (x);
  }
  y = 1;
  for (int i = 0; i < n; i++) {
    pow23[i] = y;
    pow23[i] %= (x + 1);
    y *= 2;
    y %= (x + 1);
  }
  int num1 = 0;
  for (int i = 0; i < n; i++) {
    if (x == 1)
      continue;
    if (s[i] == '1')
      num1 += pow21[n - i - 1], num1 %= (x - 1);
  }
  int num2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num2 += pow22[n - i - 1], num2 %= (x);
  }
  int num3 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num3 += pow23[n - i - 1], num3 %= (x + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int z = num3 + pow23[n - i - 1];
      z %= (x + 1);
      cout << 1 + calc(z) << endl;
    } else {
      int z = num1 - pow21[n - i - 1];
      if (x - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      z %= (x - 1);
      if (z < 0)
        z = (x - 1) + z;
      cout << 1 + calc(z) << endl;
    }
  }
}


Error: 0

Fixed submission:

/* 	* In the name of GOD
 * Thanks God */
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define int long long
const int N = 201234;
int pow21[N], pow22[N], pow23[N];

int calc(int x) {
  int a[30];
  int y = x;
  int num = 0;
  for (int i = 0; i < 30; i++) {
    a[i] = y % 2;
    y /= 2;
    if (a[i])
      num++;
  }
  if (num == 0) {
    return 0;
  }
  if (x % num == 0) {
    return 1;
  }
  return 1 + calc(x % num);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  int x = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      x++;
  if (x == 0) {
    for (int i = 0; i < n; i++)
      cout << 0 << endl;
    return 0;
  }
  int y;
  y = 1;
  for (int i = 0; i < n; i++) {
    if (x == 1)
      continue;
    pow21[i] = y;
    pow21[i] %= (x - 1);
    y *= 2;
    y %= (x - 1);
  }
  y = 1;
  for (int i = 0; i < n; i++) {
    pow22[i] = y;
    pow22[i] %= (x);
    y *= 2;
    y %= (x);
  }
  y = 1;
  for (int i = 0; i < n; i++) {
    pow23[i] = y;
    pow23[i] %= (x + 1);
    y *= 2;
    y %= (x + 1);
  }
  int num1 = 0;
  for (int i = 0; i < n; i++) {
    if (n == 1)
      continue;
    if (s[i] == '1')
      num1 += pow21[n - i - 1], num1 %= (x - 1);
  }
  int num2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num2 += pow22[n - i - 1], num2 %= (x);
  }
  int num3 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num3 += pow23[n - i - 1], num3 %= (x + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int z = num3 + pow23[n - i - 1];
      z %= (x + 1);
      cout << 1 + calc(z) << endl;
    } else {
      int z = num1 - pow21[n - i - 1];
      z %= (x - 1);
      if (z < 0)
        z = (x - 1) + z;
      cout << 1 + calc(z) << endl;
    }
  }
}