Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long
#define int long long
#define ld long double
#define endl "\n"
#define debugv(v)                                                              \
  cerr << #v << " : ";                                                         \
  for (auto x : v)                                                             \
    cerr << x << ' ';                                                          \
  cerr << endl;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugp(p) cerr << #p << '=' << (p.first) << " " << p.second << endl;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define output(x) cout << (x ? "YES" : "NO") << endl;
const ll MOD = 1e9 + 7;
const int N = 1e4 + 5;
const int demo = 10;
using namespace std;
const ll cfmod = 998244353;
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char button[4] = {'L', 'R', 'U', 'D'};

// Ahhiya thi lakhvanu chalu thasse....

int calc(int first, int x) {
  int ones = 0;
  int sec = first;
  while (sec) {
    if (sec % 2) {
      ones++;
    }
    sec /= 2;
  }
  if (ones == 0)
    return 0;
  if (ones == 1)
    return 1;
  return 1 + calc(first % ones, ones);
}

signed main() {
  FAST int q = 1;
  // cin>>q;
  while (q--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int arr[2][n];
    int ones = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        ones++;
      }
    }
    if (ones > 0) {
      // ones-1
      int x = ones - 1;
      int num = 1;
      int curr = 0;
      for (int i = n - 1; i >= 0; i--) {
        arr[0][i] = num;
        num *= 2;
        num %= x;
      }
      int sum0 = 0;
      for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
          sum0 += arr[0][i];
        }
      }
      x = ones + 1;
      num = 1;
      for (int i = n - 1; i >= 0; i--) {
        arr[1][i] = num;
        num *= 2;
        num %= x;
      }
      int sum1 = 0;
      for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
          sum1 += arr[1][i];
        }
      }
      // debugv(arr[1])
      // cout<<sum0<<" "<<sum1<<endl;

      // main ans
      for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
          x = ones - 1;
          int shrey = sum0 - arr[0][i] + x;
          int p = 1 + calc(shrey % x, x);
          cout << p << endl;
          // debug(shrey)
        } else {
          x = ones + 1;
          int shrey = sum1 + arr[1][i];
          int p = 1 + calc(shrey % x, x);
          cout << p << endl;
          // debug(shrey)
        }
      }
      // ones+1
    } else {
      for (int i = 0; i < n; i++) {
        cout << 1 << endl;
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long
#define int long long
#define ld long double
#define endl "\n"
#define debugv(v)                                                              \
  cerr << #v << " : ";                                                         \
  for (auto x : v)                                                             \
    cerr << x << ' ';                                                          \
  cerr << endl;
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugp(p) cerr << #p << '=' << (p.first) << " " << p.second << endl;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define output(x) cout << (x ? "YES" : "NO") << endl;
const ll MOD = 1e9 + 7;
const int N = 1e4 + 5;
const int demo = 10;
using namespace std;
const ll cfmod = 998244353;
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char button[4] = {'L', 'R', 'U', 'D'};

// Ahhiya thi lakhvanu chalu thasse....

int calc(int first, int x) {
  int ones = 0;
  int sec = first;
  while (sec) {
    if (sec % 2) {
      ones++;
    }
    sec /= 2;
  }
  if (ones == 0)
    return 0;
  if (ones == 1)
    return 1;
  return 1 + calc(first % ones, ones);
}

signed main() {
  FAST int q = 1;
  // cin>>q;
  while (q--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int arr[2][n];
    int ones = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        ones++;
      }
    }
    if (ones == 1) {
      int x = ones + 1;
      int num = 1;
      for (int i = n - 1; i >= 0; i--) {
        arr[1][i] = num;
        num *= 2;
        num %= x;
      }
      int sum1 = 0;
      for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
          sum1 += arr[1][i];
        }
      }
      // debugv(arr[1])
      // cout<<sum0<<" "<<sum1<<endl;

      // main ans
      for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
          cout << 0 << endl;
        } else {
          x = ones + 1;
          int shrey = sum1 + arr[1][i];
          int p = 1 + calc(shrey % x, x);
          cout << p << endl;
          // debug(shrey)
        }
      }
      // ones+1
    } else if (ones > 0) {
      // ones-1
      int x = ones - 1;
      int num = 1;
      int curr = 0;
      for (int i = n - 1; i >= 0; i--) {
        arr[0][i] = num;
        num *= 2;
        num %= x;
      }
      int sum0 = 0;
      for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
          sum0 += arr[0][i];
        }
      }
      x = ones + 1;
      num = 1;
      for (int i = n - 1; i >= 0; i--) {
        arr[1][i] = num;
        num *= 2;
        num %= x;
      }
      int sum1 = 0;
      for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
          sum1 += arr[1][i];
        }
      }
      // debugv(arr[1])
      // cout<<sum0<<" "<<sum1<<endl;

      // main ans
      for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
          x = ones - 1;
          int shrey = sum0 - arr[0][i] + x;
          int p = 1 + calc(shrey % x, x);
          cout << p << endl;
          // debug(shrey)
        } else {
          x = ones + 1;
          int shrey = sum1 + arr[1][i];
          int p = 1 + calc(shrey % x, x);
          cout << p << endl;
          // debug(shrey)
        }
      }
      // ones+1
    } else {
      for (int i = 0; i < n; i++) {
        cout << 1 << endl;
      }
    }
  }
}