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

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define int long long

using namespace std;
const int N = 500077;
int n, m, d, t;
char a[N];
int s1 = 0, s2 = 0;

int binpow(int a, int n, int k) {
  if (n == 0)
    return 1 % k;
  if (n % 2 == 1)
    return binpow(a, n - 1, k) * a % k;
  else {
    int b = binpow(a, n / 2, k) % k;
    return b * b % k;
  }
}

int make(int x) {
  int cnt = 0;
  while (x > 0) {
    if (x % 2 == 1) {
      cnt++;
    }
    x /= 2;
  }
  return cnt;
}

void check(int x) {
  int cnt = 1;
  while (x > 0) {
    cnt++;
    x %= make(x);
  }
  cout << cnt << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  cin >> n;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == '1') {
      cnt++;
    }
  }
  if (cnt == 0) {
    for (int i = 1; i <= n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }
  if (cnt == 1) {
    for (int i = 1; i <= n; i++) {
      if (a[i] == '1') {
        s1 = binpow(2, n - i, 2);
      }
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] == '1') {
        cout << 0 << endl;
      } else {
        int s2 = binpow(2, n - i, 2) + s1;
        s2 %= 2;
        check(s2);
      }
    }
    return 0;
  }

  for (int i = 1; i <= n; i++) {
    if (a[i] == '1') {
      s1 += (binpow(2, n - i, cnt - 1) % (cnt - 1));
      s2 += (binpow(2, n - i, cnt + 1) % (cnt + 1));
    }
  }

  for (int i = 1; i <= n; i++) {
    int sum = 0;
    if (a[i] == '1') {
      sum = s1;
      sum -= binpow(2, n - i, cnt - 1);
      sum %= (cnt - 1);
      sum += (cnt - 1);
      sum %= (cnt - 1);
      check(sum);
    } else {
      sum = s2;
      sum += binpow(2, n - i, cnt + 1) % (cnt + 1);
      sum %= (cnt + 1);
      sum += (cnt + 1);
      sum %= (cnt + 1);
      check(sum);
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define int long long

using namespace std;
const int N = 500077;
int n, m, d, t;
char a[N];
int s1 = 0, s2 = 0;

int binpow(int a, int n, int k) {
  if (n == 0)
    return 1 % k;
  if (n % 2 == 1)
    return binpow(a, n - 1, k) * a % k;
  else {
    int b = binpow(a, n / 2, k) % k;
    return b * b % k;
  }
}

int make(int x) {
  int cnt = 0;
  while (x > 0) {
    if (x % 2 == 1) {
      cnt++;
    }
    x /= 2;
  }
  return cnt;
}

void check(int x) {
  int cnt = 1;
  while (x > 0) {
    cnt++;
    x %= make(x);
  }
  cout << cnt << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  cin >> n;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == '1') {
      cnt++;
    }
  }
  if (cnt == 0) {
    for (int i = 1; i <= n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }
  if (cnt == 1) {
    for (int i = 1; i <= n; i++) {
      if (a[i] == '1') {
        s1 = binpow(2, n - i, 2);
      }
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] == '1') {
        cout << 0 << endl;
      } else {
        int s2 = binpow(2, n - i, 2) + s1;
        s2 %= 2;
        check(s2);
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    if (a[i] == '1') {
      s1 += (binpow(2, n - i, cnt - 1) % (cnt - 1));
      s2 += (binpow(2, n - i, cnt + 1) % (cnt + 1));
    }
  }

  for (int i = 1; i <= n; i++) {
    int sum = 0;
    if (a[i] == '1') {
      sum = s1;
      sum -= binpow(2, n - i, cnt - 1);
      sum %= (cnt - 1);
      sum += (cnt - 1);
      sum %= (cnt - 1);
      check(sum);
    } else {
      sum = s2;
      sum += binpow(2, n - i, cnt + 1) % (cnt + 1);
      sum %= (cnt + 1);
      sum += (cnt + 1);
      sum %= (cnt + 1);
      check(sum);
    }
  }
}