Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep(i, j, n) for (ll(i) = (j); (i) < (n); (i)++)
#define FOR(i, c)                                                              \
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(), (hoge).end()
#define F first
#define S second
#define en "\n"
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
const long long MOD = 1e9 + 7;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
const int loose = 0;
const int tight = 1;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// グラフ関連
struct Edge { // グラフ
  ll to, cap, rev;
  Edge(ll _to, ll _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  ll n;
  cin >> n;

  ll ans = 0;

  REP(i, n) {
    ll a, k;
    cin >> a >> k;
    while (a % k != 0) {
      a -= a / k + 1;
    }
    ans ^= a / k;
  }

  cout << ((ans != 0) ? "Takahashi" : "Aoki") << en;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep(i, j, n) for (ll(i) = (j); (i) < (n); (i)++)
#define FOR(i, c)                                                              \
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(), (hoge).end()
#define F first
#define S second
#define en "\n"
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
const long long MOD = 1e9 + 7;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
const int loose = 0;
const int tight = 1;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// グラフ関連
struct Edge { // グラフ
  ll to, cap, rev;
  Edge(ll _to, ll _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  ll n;
  cin >> n;

  ll ans = 0;

  REP(i, n) {
    ll a, k;
    cin >> a >> k;
    while (a % k != 0) {
      if ((a % k) % (a / k + 1) == 0) {
        a -= a % k;
      } else {
        a -= ((a % k) / (a / k + 1) + 1) * (a / k + 1);
      }
    }
    ans ^= a / k;
  }

  cout << ((ans != 0) ? "Takahashi" : "Aoki") << en;

  return 0;
}