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

#include <bits/stdc++.h>
using namespace std;

#define fast                                                                   \\
  {                                                                            \\
    ios ::sync_with_stdio(false);                                              \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define pb push_back
#define ll long long
#define ld long double
#define vll vector<ll>
#define infl LONG_LONG_MAX
#define infd LDBL_MAX
#define F first
#define S second
#define pll pair<ll, ll>
#define G(a, b) get<a>(b)
#define ALL(v) v.begin(), v.end()
#define MP make_pair
#define MT make_tuple
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fi(i, a, b) for (ll i = (b - 1); i >= a; i--)
#define endl "\\n"
#define nl cout << "\\n";
#define pr(x) cout << x;
#define pr1(x) cout << x << " ";
#define pr2(x, y) cout << x << " " << y;
#define sz(x) (ll) x.size()
#define ara cout << "ara-ara\\n";

void func() {
  ll n;
  cin >> n;
  char s[n + 5];
  f(i, 1, n + 1) cin >> s[i];
  ll cnt1 = 0;
  f(i, 1, n + 1) if (s[i] == '1') cnt1++;

  if (cnt1 == 1) {
    ll idx = -1, tt = 0, zz = 0;
    fi(i, 1, n + 1) {
      if (s[i] == '1') {
        idx = i;
        zz = tt;
        break;
      }
      tt++;
    }

    ll ans[n + 1];
    ll curr = 0;

    fi(i, 1, n + 1) {
      if (i == idx) {
        ans[i] = 0;
        curr++;
        continue;
      }

      ll pp = zz, qq = curr;
      if (pp == 0 || qq == 0)
        ans[i] = 2;
      else
        ans[i] = 1;
      curr++;
    }

    f(i, 1, n + 1) cout << ans[i] << endl;
    return;
  }

  ll x = cnt1 + 1, y = cnt1 - 1;

  ll totx = 0, toty = 0, curr1 = 1 % x, curr2 = 1 % y;
  fi(i, 1, n + 1) {
    if (s[i] == '1') {
      totx = (totx + curr1) % x;
      toty = (toty + curr2) % y;
    }
    curr1 = (curr1 * 2) % x;
    curr2 = (curr2 * 2) % y;
  }

  ll g[n + 1];
  g[0] = 0;
  f(i, 1, n + 1) {
    ll xx = __builtin_popcountll(i);
    g[i] = 1 + g[i % xx];
  }

  ll ans[n + 1];
  curr1 = 1 % x, curr2 = 1 % y;
  fi(i, 1, n + 1) {
    if (s[i] == '1') {
      ll z = (toty - curr2 + y) % y;
      ans[i] = 1 + g[z];
    } else {
      ll z = (totx + curr1) % x;
      ans[i] = 1 + g[z];
    }

    curr1 = (curr1 * 2) % x;
    curr2 = (curr2 * 2) % y;
  }

  f(i, 1, n + 1) cout << ans[i] << "\\n";
}

int main() {
  fast
      // #ifndef ONLINE_JUDGE
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif // ONLINE_JUDGE

      ll ntc = 1;
  // cin>>ntc;

  f(i, 1, ntc + 1) {
    // cout<<"Case #"<<i<<": ";
    func();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fast                                                                   \\
  {                                                                            \\
    ios ::sync_with_stdio(false);                                              \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }
#define pb push_back
#define ll long long
#define ld long double
#define vll vector<ll>
#define infl LONG_LONG_MAX
#define infd LDBL_MAX
#define F first
#define S second
#define pll pair<ll, ll>
#define G(a, b) get<a>(b)
#define ALL(v) v.begin(), v.end()
#define MP make_pair
#define MT make_tuple
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fi(i, a, b) for (ll i = (b - 1); i >= a; i--)
#define endl "\\n"
#define nl cout << "\\n";
#define pr(x) cout << x;
#define pr1(x) cout << x << " ";
#define pr2(x, y) cout << x << " " << y;
#define sz(x) (ll) x.size()
#define ara cout << "ara-ara\\n";

void func() {
  ll n;
  cin >> n;
  char s[n + 5];
  f(i, 1, n + 1) cin >> s[i];
  ll cnt1 = 0;
  f(i, 1, n + 1) if (s[i] == '1') cnt1++;

  if (cnt1 == 1) {
    ll idx = -1;
    f(i, 1, n + 1) {
      if (s[i] == '1') {
        idx = i;
        break;
      }
    }

    ll ans[n + 1];
    ll curr = 0;

    fi(i, 1, n + 1) {
      if (i == idx) {
        ans[i] = 0;
        continue;
      }

      ll pp = idx, qq = curr;
      if (pp == 0 || qq == 0)
        ans[i] = 1;
      else
        ans[i] = 0;
      curr++;
    }
  }

  ll x = cnt1 + 1, y = cnt1 - 1;

  ll totx = 0, toty = 0, curr1 = 1 % x, curr2 = 1 % y;
  fi(i, 1, n + 1) {
    if (s[i] == '1') {
      totx = (totx + curr1) % x;
      toty = (toty + curr2) % y;
    }
    curr1 = (curr1 * 2) % x;
    curr2 = (curr2 * 2) % y;
  }

  ll g[n + 1];
  g[0] = 0;
  f(i, 1, n + 1) {
    ll xx = __builtin_popcountll(i);
    g[i] = 1 + g[i % xx];
  }

  ll ans[n + 1];
  curr1 = 1 % x, curr2 = 1 % y;
  fi(i, 1, n + 1) {
    if (s[i] == '1') {
      ll z = (toty - curr2 + y) % y;
      ans[i] = 1 + g[z];
    } else {
      ll z = (totx + curr1) % x;
      ans[i] = 1 + g[z];
    }

    curr1 = (curr1 * 2) % x;
    curr2 = (curr2 * 2) % y;
  }

  f(i, 1, n + 1) cout << ans[i] << "\\n";
}

int main() {
  fast
      // #ifndef ONLINE_JUDGE
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif // ONLINE_JUDGE

      ll ntc = 1;
  // cin>>ntc;

  f(i, 1, ntc + 1) {
    // cout<<"Case #"<<i<<": ";
    func();
  }

  return 0;
}