Problem p02736 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int N;
vector<int> pw2;
vector<int> A;

int ff(int a, int b) {
  if (a == 1 && b == 2)
    return 1;
  if (a == 2 && b == 1)
    return 1;
  return a ^ b;
}

bool isok(int x, int key) {
  // code here
  if (pw2[x] < key)
    return true;
  else
    return false;
}
int binsch(int key) {
  // set value
  int ng = N;
  int ok = 0;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isok(mid, key))
      ok = mid;
    else
      ng = mid;
  }
  return pw2[ok];
}

int f(int d, int n) {
  // cout << d<< " " << n << endl;
  if (d == 1)
    return A[n - 1];
  int dd = binsch(d);
  int a = f(d - dd, n);
  int b = f(d - dd, n + dd);
  return ff(a, b);
}

int main() {
  cin >> N;
  string S;
  cin >> S;
  A.resize(N);
  rep(i, N) { A[i] = (int)(S[i] - '0'); }
  int now = 1;
  while (1) {
    pw2.pb(now);
    if (now >= 200000)
      break;
    now *= 2;
  }
  int ans;
  if (N % 2 == 0) {
    ans = f(N, 1);
  } else {
    int a = f(N - 1, 1);
    int b = f(N - 1, 2);
    ans = ff(a, b);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mod 1000000007
using ll = long long;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int N;
vector<int> pw2;
vector<int> A;

int ff(int a, int b) {
  if (a == 1 && b == 2)
    return 1;
  if (a == 2 && b == 1)
    return 1;
  return a ^ b;
}

bool isok(int x, int key) {
  // code here
  if (pw2[x] < key)
    return true;
  else
    return false;
}
int binsch(int key) {
  // set value
  int ng = sz(pw2);
  int ok = 0;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (isok(mid, key))
      ok = mid;
    else
      ng = mid;
  }
  return pw2[ok];
}

int f(int d, int n) {
  // cout << d<< " " << n << endl;
  if (d == 1)
    return A[n - 1];
  int dd = binsch(d);
  int a = f(d - dd, n);
  int b = f(d - dd, n + dd);
  return ff(a, b);
}

int main() {
  cin >> N;
  string S;
  cin >> S;
  A.resize(N);
  rep(i, N) { A[i] = (int)(S[i] - '0'); }
  int now = 1;
  while (1) {
    pw2.pb(now);
    if (now >= 200000)
      break;
    now *= 2;
  }
  int ans;
  if (N % 2 == 0) {
    ans = f(N, 1);
  } else {
    int a = f(N - 1, 1);
    int b = f(N - 1, 2);
    ans = ff(a, b);
  }
  cout << ans << endl;

  return 0;
}