Problem p02736 (C++)

Buggy submission (status: Runtime Error):
// Author: Vamsi Krishna Reddy Satti
// With love for Competitive Programming!

#pragma GCC optimize("Ofast,unroll-loops,-ffloat-store")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")

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

string to_string(const string &s) { return '"' + s + '"'; }
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 3
#endif

#define cin_exception cin.exceptions(cin.failbit);
#define cout_precision                                                         \
  cout.setf(ios::fixed);                                                       \
  cout.precision(15);
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
using ld = long double;
using vi = vector<int>;
using ll = int_fast64_t;
using ull = uint_fast64_t;
using vl = vector<ll>;
using vul = vector<ull>;

// -----------------------------------------------------------------------------

const int N = 1e6 + 5;
int n;
string s;
int a[N];
int cn[N];

void preprocess() {
  for (int i = 1; i < N; ++i) {
    if (i & 1)
      continue;
    cn[i] = cn[i >> 1] + 1;
  }
  for (int i = 1; i < N; ++i) {
    cn[i] += cn[i - 1];
  }
}

int ln_nCr(int n, int r) {
  assert(n >= r);
  return cn[n] - cn[n - r] - cn[r];
}

void solve() {
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    a[i] = s[i] - '1';
  }
  int k = 1;
  while (1) {
    int p = 0;
    for (int i = 0; i < n; ++i) {
      if (!(a[i] & 1))
        continue;
      p ^= (ln_nCr(n - 1, i) == 0);
    }
    if (p) {
      cout << k << '\n';
      return;
    }
    int oe = 0, te = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i] == 1)
        oe = 1;
      if (a[i] == 2)
        te = 1;
    }
    if (oe || (!te)) {
      cout << 0 << '\n';
      return;
    }
    for (int i = 0; i < n; ++i) {
      a[i] >>= 1;
    }
    ++k;
  }
}

int main() {
  fast_io;
  cin_exception;
  preprocess();
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
}

Fixed submission:
// Author: Vamsi Krishna Reddy Satti
// With love for Competitive Programming!

// #pragma GCC optimize ("Ofast,unroll-loops,-ffloat-store")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")

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

string to_string(const string &s) { return '"' + s + '"'; }
void debug_out() { cout << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 3
#endif

#define cin_exception cin.exceptions(cin.failbit);
#define cout_precision                                                         \
  cout.setf(ios::fixed);                                                       \
  cout.precision(15);
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
using ld = long double;
using vi = vector<int>;
using ll = int_fast64_t;
using ull = uint_fast64_t;
using vl = vector<ll>;
using vul = vector<ull>;

// -----------------------------------------------------------------------------

const int N = 1e6 + 5;
int n;
string s;
int a[N];
int cn[N];

void preprocess() {
  for (int i = 1; i < N; ++i) {
    if (i & 1)
      continue;
    cn[i] = cn[i >> 1] + 1;
  }
  for (int i = 1; i < N; ++i) {
    cn[i] += cn[i - 1];
  }
}

int ln_nCr(int n, int r) {
  assert(n >= r);
  return cn[n] - cn[n - r] - cn[r];
}

void solve() {
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    a[i] = s[i] - '1';
  }
  int k = 1;
  while (1) {
    int p = 0;
    for (int i = 0; i < n; ++i) {
      if (!(a[i] & 1))
        continue;
      p ^= (ln_nCr(n - 1, i) == 0);
    }
    if (p) {
      cout << k << '\n';
      return;
    }
    int oe = 0, te = 0;
    for (int i = 0; i < n; ++i) {
      if (a[i] == 1)
        oe = 1;
      if (a[i] == 2)
        te = 1;
    }
    if (oe || (!te)) {
      cout << 0 << '\n';
      return;
    }
    for (int i = 0; i < n; ++i) {
      a[i] >>= 1;
    }
    ++k;
  }
}

int main() {
  fast_io;
  cin_exception;
  preprocess();
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
}