Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge))                  \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << '\n'
#define debugMatrix(x, h, w)                                                   \
  cerr << __LINE__ << ": " << #x << " =\n";                                    \
  for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) {                \
    cerr << ((hoge ? " {" : "{{"));                                            \
    for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga))                \
      cerr << ((fuga ? ", " : "")) << x[hoge][fuga];                           \
    cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n';           \
  }
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  int X = 0;
  for (int i = 0; i < N; i++) {
    int A, K;
    cin >> A >> K;
    while (A % K != 0 && A >= 2 * K) {
      A -= (A + K) / K;
    }
    if (A % K == 0) {
      X ^= A / K;
    } else if (K < A) {
      X ^= (A - K + 1) & 1;
    }
  }
  cout << (X != 0 ? "Takahashi" : "Aoki") << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge))                  \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << '\n'
#define debugMatrix(x, h, w)                                                   \
  cerr << __LINE__ << ": " << #x << " =\n";                                    \
  for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) {                \
    cerr << ((hoge ? " {" : "{{"));                                            \
    for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga))                \
      cerr << ((fuga ? ", " : "")) << x[hoge][fuga];                           \
    cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n';           \
  }
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  int X = 0;
  for (int i = 0; i < N; i++) {
    int A, K;
    cin >> A >> K;
    while (A % K != 0 && A >= 2 * K) {
      int m = A / K;
      int l = (A - K * m + m) / (m + 1);
      A -= (m + 1) * l;
    }
    if (A % K == 0) {
      X ^= A / K;
    } else if (K < A) {
      X ^= (A - K + 1) & 1;
    }
  }
  cout << (X != 0 ? "Takahashi" : "Aoki") << '\n';
  return 0;
}