Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

void expr() {
  int N, K;
  cin >> N >> K;

  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++) {
    int f = i / K;
    vector<int> a;
    for (int j = 1; j <= min(i, f); j++) {
      a.emplace_back(A[i - j]);
    }
    sort(a.begin(), a.end());

    int ptr = 0, n;
    for (n = 0;; n++) {
      int c = 0;
      while (ptr < a.size() and a[ptr] == n)
        c++, ptr++;
      if (c == 0)
        break;
    }
    A[i] = n;
  }

  /*
  vector< vector<int> > lis(N+1);
  for(int i=0; i<=N; i++) {
      lis[ A[i] ].emplace_back(i);
  }
  for(int i=0; i<=N; i++) {
      printf("%2d:", i);
      for(auto e : lis[i]) cerr << " " << e;
      cerr << endl;
  }
  */

  for (int i = 0; i <= N; i++) {
    cerr << A[i] << " ";
    if ((i + 1) % K == 0)
      cerr << endl;
  }
  cerr << endl;
}

int solve(ll A, ll K) {
  if (A % K == 0)
    return A / K;
  if (A >= K * K)
    return solve(A - A / K - 1, K);

  ll NA = A / K * K + A % K;
  if (NA < A)
    return solve(NA, K);
  return solve(A - A / K - 1, K);
}

int main() {
  // expr();
  // return 0;

  int N;
  cin >> N;
  vector<int> A(N), K(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> K[i];

  int grundy = 0;
  for (int i = 0; i < N; i++) {
    int g = solve(A[i], K[i]);
    grundy ^= g;
    // fprintf(stderr, "%d: %d\n", i, g);
  }
  cout << (grundy ? "Takahashi" : "Aoki") << endl;
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

void expr() {
  int N, K;
  cin >> N >> K;

  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++) {
    int f = i / K;
    vector<int> a;
    for (int j = 1; j <= min(i, f); j++) {
      a.emplace_back(A[i - j]);
    }
    sort(a.begin(), a.end());

    int ptr = 0, n;
    for (n = 0;; n++) {
      int c = 0;
      while (ptr < a.size() and a[ptr] == n)
        c++, ptr++;
      if (c == 0)
        break;
    }
    A[i] = n;
  }

  /*
  vector< vector<int> > lis(N+1);
  for(int i=0; i<=N; i++) {
      lis[ A[i] ].emplace_back(i);
  }
  for(int i=0; i<=N; i++) {
      printf("%2d:", i);
      for(auto e : lis[i]) cerr << " " << e;
      cerr << endl;
  }
  */

  for (int i = 0; i <= N; i++) {
    cerr << A[i] << " ";
    if ((i + 1) % K == 0)
      cerr << endl;
  }
  cerr << endl;
}

int solve(ll A, ll K) {
  if (A % K == 0)
    return A / K;
  if (A >= K * K)
    return solve(A - A / K - 1, K);

  ll lim = A / K * K, sub = A - lim;
  ll div = (sub + A / K) / (A / K + 1);
  return solve(A - div * (A / K + 1), K);
}

int main() {
  // expr();
  // return 0;

  int N;
  cin >> N;
  vector<int> A(N), K(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> K[i];

  int grundy = 0;
  for (int i = 0; i < N; i++) {
    int g = solve(A[i], K[i]);
    grundy ^= g;
    // fprintf(stderr, "%d: %d\n", i, g);
  }
  cout << (grundy ? "Takahashi" : "Aoki") << endl;
  return 0;
}