Problem p02736 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

ll mod = 1e9 + 7;
const ld error = 1e-9;
const ld PI = acosl(-1); // const ld PI = acosl(-1)

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define eq(x, y) (fabs((x) - (y)) < error)
#define bt(i) (1LL << (i))

#define debug(x) cerr << #x << " = " << (x) << "\n"
#define hoise cerr << "hoise - " << __LINE__ << "\n"
#define tham getchar()
mt19937 rng((unsigned)chrono::system_clock::now()
                .time_since_epoch()
                .count()); // mt199937_64 for ll

inline ll MOD(ll x, ll m = mod) {
  ll y = x % m;
  return (y >= 0) ? y : y + m;
}

const int inf = 1e9;
const ll infl = 1e18 + 1;
const int nmax = 1e3 + 10;
///===========================================  template
///=======================================================

int arr[nmax];

int pairity(int n, int r) {
  if ((~n) & r)
    return 0;
  else
    return 1;
}

int main() {
  FASTIO;

  int n;
  cin >> n;
  n--;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    arr[i] = abs(s[i + 1] - s[i]);
  }

  //    for(int i = 0; i<n; i++) debug(arr[i]);

  int res = 0;
  for (int i = 0; i < n; i++) {
    res = ((res + pairity(n - 1, i) * arr[i]) & 1);
  }

  if (res == 1) {
    cout << 1 << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    if (arr[i] == 1) {
      cout << 0 << endl;
      return 0;
    }
  }

  for (int i = 0; i < n; i++) {
    arr[i] /= 2;
  }

  res = 0;
  for (int i = 0; i < n; i++) {
    res = ((res + pairity(n - 1, i) * arr[i]) & 1);
  }
  if (res == 0)
    cout << 0 << endl;
  else
    cout << 2 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;

ll mod = 1e9 + 7;
const ld error = 1e-9;
const ld PI = acosl(-1); // const ld PI = acosl(-1)

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define eq(x, y) (fabs((x) - (y)) < error)
#define bt(i) (1LL << (i))

#define debug(x) cerr << #x << " = " << (x) << "\n"
#define hoise cerr << "hoise - " << __LINE__ << "\n"
#define tham getchar()
mt19937 rng((unsigned)chrono::system_clock::now()
                .time_since_epoch()
                .count()); // mt199937_64 for ll

inline ll MOD(ll x, ll m = mod) {
  ll y = x % m;
  return (y >= 0) ? y : y + m;
}

const int inf = 1e9;
const ll infl = 1e18 + 1;
const int nmax = 1e6 + 10;
///===========================================  template
///=======================================================

int arr[nmax];

int pairity(int n, int r) {
  if ((~n) & r)
    return 0;
  else
    return 1;
}

int main() {
  FASTIO;

  int n;
  cin >> n;
  n--;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    arr[i] = abs(s[i + 1] - s[i]);
  }

  //    for(int i = 0; i<n; i++) debug(arr[i]);

  int res = 0;
  for (int i = 0; i < n; i++) {
    res = ((res + pairity(n - 1, i) * arr[i]) & 1);
  }

  if (res == 1) {
    cout << 1 << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    if (arr[i] == 1) {
      cout << 0 << endl;
      return 0;
    }
  }

  for (int i = 0; i < n; i++) {
    arr[i] /= 2;
  }

  res = 0;
  for (int i = 0; i < n; i++) {
    res = ((res + pairity(n - 1, i) * arr[i]) & 1);
  }
  if (res == 0)
    cout << 0 << endl;
  else
    cout << 2 << endl;
  return 0;
}