prithivMLmods/Coder-Stat huggingface dataset:
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...)                                                           \
  do {                                                                         \
  } while (0)
#endif
#include <cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end();             \
       i != i##_end; ++i)

template <class T> inline void amin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <class T> inline void amax(T &x, const T &y) {
  if (x < y)
    x = y;
}
template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}

int game(int a, int k) {
  if (a < k)
    return 0;
  if (a % k == 0)
    return a / k;
  return game(a - a / k - 1, k);
}

int N;

void MAIN() {
  scanf("%d", &N);
  int ans = 0;
  REP(i, N) {
    int a, k;
    scanf("%d%d", &a, &k);
    ans ^= game(a, k);
  }
  puts(ans ? "Takahashi" : "Aoki");
}

int main() {
  int TC = 1;
  //    scanf("%d", &TC);
  REP(tc, TC) MAIN();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...)                                                           \
  do {                                                                         \
  } while (0)
#endif
#include <cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end();             \
       i != i##_end; ++i)

template <class T> inline void amin(T &x, const T &y) {
  if (y < x)
    x = y;
}
template <class T> inline void amax(T &x, const T &y) {
  if (x < y)
    x = y;
}
template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}

int game(int a, int k) {
  while (a >= k) {
    int r = a % k;
    if (r == 0)
      return a / k;
    int d = a / k + 1;
    int p = max((r + d - 1) / d, 1);
    a -= p * d;
  }
  return 0;
}

int N;

void MAIN() {
  scanf("%d", &N);
  int ans = 0;
  REP(i, N) {
    int a, k;
    scanf("%d%d", &a, &k);
    ans ^= game(a, k);
  }
  puts(ans ? "Takahashi" : "Aoki");
}

int main() {
  int TC = 1;
  //    scanf("%d", &TC);
  REP(tc, TC) MAIN();
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n;
int main() {
  int i, j, k;
  scanf("%d", &n);
  int ans = 0;
  for (i = 1; i <= n; i++) {
    int x, y, ret = 0;
    scanf("%d%d", &x, &y);
    if (y == 1) {
      ans ^= x;
      continue;
    }
    while (x >= y) {
      if (x % y == 0) {
        ret = x / y;
        break;
      }
      x -= x / y + 1;
    }
    ans ^= ret;
  }
  if (ans == 0)
    printf("Aoki\n");
  else
    printf("Takahashi\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n;
int main() {
  int i, j, k;
  scanf("%d", &n);
  int ans = 0;
  for (i = 1; i <= n; i++) {
    int x, y, ret = 0;
    scanf("%d%d", &x, &y);
    if (y == 1) {
      ans ^= x;
      continue;
    }
    while (x >= y) {
      if (x % y == 0) {
        ret = x / y;
        break;
      }
      int d = x / y + 1;
      int p = x / y * y;
      if (x - d <= p)
        x -= d;
      else
        x -= (x - p) / d * d;
    }
    ans ^= ret;
  }
  if (ans == 0)
    printf("Aoki\n");
  else
    printf("Takahashi\n");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define ll long long
using namespace std;
ll n, m, cnt, ans;
int cal(int a, int b) {
  if (a < b)
    return 0;
  if (!(a % b))
    return a / b;
  int k = a - (ll)a * (b - 1) / b, j;
  int l1, r1, m1;
  l1 = 1;
  r1 = a / k;
  while (l1 < r1) {
    m1 = (l1 + r1) / 2 + 1;
    j = a - m1 * k;
    if (m1 - (ll)m1 * (b - 1) / b == k)
      l1 = m1;
    else
      r1 = m1 - 1;
  }
  return cal(a - l1 * k, b);
}
int main() {
  int i, s, a, b, j, k;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++)
    scanf("%d%d", &a, &b), ans ^= cal(a, b);
  if (ans)
    printf("Takahashi");
  else
    printf("Aoki");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define ll long long
using namespace std;
ll n, m, cnt, ans;
int cal(int a, int b) {
  if (a < b)
    return 0;
  if (!(a % b))
    return a / b;
  int k = a - (ll)a * (b - 1) / b, j;
  int l1, r1, m1;
  l1 = 1;
  r1 = a / k;
  while (l1 < r1) {
    m1 = (l1 + r1) / 2 + 1;
    j = a - m1 * k;
    if (j - (ll)j * (b - 1) / b == k)
      l1 = m1;
    else
      r1 = m1 - 1;
  }
  return cal(a - l1 * k, b);
}
int main() {
  int i, s, a, b, j, k;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++)
    scanf("%d%d", &a, &b), ans ^= cal(a, b);
  if (ans)
    printf("Takahashi");
  else
    printf("Aoki");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

template <typename T> void cmax(T &x, const T &y) {
  if (y > x)
    x = y;
}

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    f = 1, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  if (f)
    x = -x;
}

int g(int x, int k) {
  if (x % k == 0)
    return x / k;
  else if (x < k)
    return 0;
  else
    return g(x - (x / k) - 1, k);
}

int main() {
  int n, a, k, xo = 0;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a), read(k);
    xo ^= g(a, k);
  }
  puts((xo == 0) ? "Aoki" : "Takahashi");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

template <typename T> void cmax(T &x, const T &y) {
  if (y > x)
    x = y;
}

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c) && c != '-')
    c = getchar();
  if (c == '-')
    f = 1, c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  if (f)
    x = -x;
}

int g(int x, int k) {
  if (x % k == 0)
    return x / k;
  else if (x < k)
    return 0;
  else
    return g(x - (x / k + 1) * ((x % k - 1) / (x / k + 1) + 1), k);
}

int main() {
  int n, a, k, xo = 0;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a), read(k);
    xo ^= g(a, k);
  }
  puts((xo == 0) ? "Aoki" : "Takahashi");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int cal(int x, int y) {
  if (x % y == 0)
    return x / y;
  if (x < y)
    return 0;
  return cal(x - x / y - 1, y);
}
int main() {
  int n, i, a, k, s = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d%d", &a, &k), s ^= cal(a, k);
  puts(s ? "Takahashi" : "Aoki");
}

Fixed submission:
#include <cstdio>
int cal(int x, int y) {
  if (x % y == 0)
    return x / y;
  if (x < y)
    return 0;
  return cal(x - (x - x / y * y + x / y) / (x / y + 1) * (x / y + 1), y);
}
int main() {
  int n, i, a, k, s = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d%d", &a, &k), s ^= cal(a, k);
  puts(s ? "Takahashi" : "Aoki");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  int n;
  cin >> n;
  vl a(n), k(n);
  vl grundy(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> k[i];
  }
  for (int i = 0; i < n; ++i) {
    ll curr = a[i];
    while (true) {
      if (curr / k[i] == 0) {
        grundy[i] = 0;
        break;
      }
      if (curr % k[i] == 0) {
        grundy[i] = curr / k[i];
        break;
      }
      curr -= 1 + curr / k[i];
    }
  }
  ll ret = 0;
  for (int i = 0; i < n; ++i) {
    ret ^= grundy[i];
  }
  if (ret == 0)
    cout << "Aoki"
         << "\n";
  else
    cout << "Takahashi"
         << "\n";
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  int n;
  cin >> n;
  vl a(n), k(n);
  vl grundy(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> k[i];
  }
  for (int i = 0; i < n; ++i) {
    ll curr = a[i];
    while (true) {
      if (curr / k[i] == 0) {
        grundy[i] = 0;
        break;
      }
      if (curr % k[i] == 0) {
        grundy[i] = curr / k[i];
        break;
      }
      ll l = curr / k[i] * k[i] + 1;

      curr -= max(1LL, (curr - l) / (1 + curr / k[i])) * (1 + curr / k[i]);
    }
  }
  ll ret = 0;
  for (int i = 0; i < n; ++i) {
    ret ^= grundy[i];
  }
  if (ret == 0)
    cout << "Aoki"
         << "\n";
  else
    cout << "Takahashi"
         << "\n";
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int calc_gr(int a, int k) {
  while (a % k > 0) {
    a -= a / k + 1;
  }
  return a / k;
}

int main() {
  int n;
  cin >> n;
  int gr = 0;
  for (int ni = 0; ni < n; ni++) {
    int a, k;
    cin >> a >> k;
    gr ^= calc_gr(a, k);
  }
  if (gr == 0) {
    cout << "Aoki\n";
  } else {
    cout << "Takahashi\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int calc_gr(int a, int k) {
  while (a % k > 0) {
    int step = (a - a / k * k) / (a / k + 1);
    if ((a - a / k * k) % (a / k + 1) > 0)
      step++;
    a -= (a / k + 1) * step;
  }
  return a / k;
}

int main() {
  int n;
  cin >> n;
  int gr = 0;
  for (int ni = 0; ni < n; ni++) {
    int a, k;
    cin >> a >> k;
    gr ^= calc_gr(a, k);
  }
  if (gr == 0) {
    cout << "Aoki\n";
  } else {
    cout << "Takahashi\n";
  }
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a[205], k[205], sg[205];
int dfs(int A, int K) {
  if (A % K == 0)
    return A / K;
  if (A < K)
    return 0;
  return dfs(A - A / K - 1, K);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &k[i]);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans ^= dfs(a[i], k[i]);
  }
  if (ans)
    puts("Takahashi");
  else
    puts("Aoki");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[205], k[205], sg[205];
int dfs(int A, int K) {
  if (A % K == 0)
    return A / K;
  if (A < K)
    return 0;
  int w = A / K + 1, S = A / K * K;
  A -= (A - S) / w * w;
  if (A > S)
    A -= w;
  return dfs(A, K);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &k[i]);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans ^= dfs(a[i], k[i]);
  }
  if (ans)
    puts("Takahashi");
  else
    puts("Aoki");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int ans, n, a, k;

int f(int a, int k) {
  if (a < k)
    return 0;
  if (a % k == 0)
    return a / k;
  return f(a - a / k - 1, k);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a, &k);
    ans ^= f(a, k);
  }
  if (ans == 0)
    puts("Aoki");
  else
    puts("Takahashi");

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int ans, n, a, k;

int f(int a, int k) {
  if (a < k)
    return 0;
  if (a % k == 0)
    return a / k;
  int now = (a / k) * k;
  int val = a / k + 1;
  int ci = (a - now - 1) / val + 1;
  return f(a - val * ci, k);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a, &k);
    ans ^= f(a, k);
  }
  if (ans == 0)
    puts("Aoki");
  else
    puts("Takahashi");

  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;

int f(int a, int k) {
  if (a % k == 0) {
    return a / k;
  }
  if (a < k) {
    return 0;
  }
  return f(a - a / k - 1, k);
}
int main() {
  int N;
  cin >> N;
  LL r = 0;
  for (int i = 0; i < N; i++) {
    int a, k;
    cin >> a >> k;
    r ^= f(a, k);
  }

  if (r == 0) {
    cout << "Aoki" << endl;
  } else {
    cout << "Takahashi" << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;

int f(int a, int k) {
  if (a % k == 0) {
    return a / k;
  }
  if (a < k) {
    return 0;
  }
  int x = max(1, (a - a / k * k) / (a / k + 1));
  return f(a - x * (a / k + 1), k);
}
int main() {
  int N;
  cin >> N;
  LL r = 0;
  for (int i = 0; i < N; i++) {
    int a, k;
    cin >> a >> k;
    r ^= f(a, k);
  }

  if (r == 0) {
    cout << "Aoki" << endl;
  } else {
    cout << "Takahashi" << endl;
  }
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
template <class T, class T2> inline void chkmax(T &x, const T2 &y) {
  if (x < y)
    x = y;
}
template <class T, class T2> inline void chkmin(T &x, const T2 &y) {
  if (x > y)
    x = y;
}
const int MAXN = (1 << 20);

int mex(vector<int> a) {
  sort(a.begin(), a.end());
  for (int i = 0; i < (int)a.size(); i++)
    if (i != a[i])
      return i;
  return (int)a.size();
}

int n;
int A[MAXN], K[MAXN];

void read() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i] >> K[i];
}

int solve(int a, int k) {
  if (a % k == 0)
    return a / k;

  int v = a / k + 1;
  if (v == 0)
    return 0;

  int bg = v * k;
  if (bg + v <= a)
    return solve(a % v + bg, k);
  return solve(a - v, k);
}

void solve() {
  int answer = 0;
  for (int i = 0; i < n; i++)
    answer ^= solve(A[i], K[i]);

  if (answer == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
}

int tmp_sg[MAXN];

void prepare() {
  for (int k = 1; k <= 20; k++) {
    cout << k << ": ";
    tmp_sg[0] = 0;
    for (int v = 1; v <= 60; v++) {
      vector<int> li;
      int mx = v / k;

      for (int i = v - 1; i >= v - mx; i--)
        li.push_back(tmp_sg[i]);

      tmp_sg[v] = mex(li);
      cout << tmp_sg[v] << " ";
    }

    cout << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // prepare();
  read();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
template <class T, class T2> inline void chkmax(T &x, const T2 &y) {
  if (x < y)
    x = y;
}
template <class T, class T2> inline void chkmin(T &x, const T2 &y) {
  if (x > y)
    x = y;
}
const int MAXN = (1 << 20);

int mex(vector<int> a) {
  sort(a.begin(), a.end());
  for (int i = 0; i < (int)a.size(); i++)
    if (i != a[i])
      return i;
  return (int)a.size();
}

int n;
int A[MAXN], K[MAXN];

void read() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i] >> K[i];
}

int solve(int a, int k) {
  if (a % k == 0)
    return a / k;
  if (a < k)
    return 0;

  int v = a / k + 1;
  if (v == 0)
    return 0;

  int bg = v * k;
  if (bg + v <= a)
    return solve(a % v + bg, k);
  return solve(a - v, k);
}

void solve() {
  int answer = 0;
  for (int i = 0; i < n; i++)
    answer ^= solve(A[i], K[i]);

  if (answer == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
}

int tmp_sg[MAXN];

void prepare() {
  for (int k = 1; k <= 20; k++) {
    cout << k << ": ";
    tmp_sg[0] = 0;
    for (int v = 1; v <= 60; v++) {
      vector<int> li;
      int mx = v / k;

      for (int i = v - 1; i >= v - mx; i--)
        li.push_back(tmp_sg[i]);

      tmp_sg[v] = mex(li);
      cout << tmp_sg[v] << " ";
    }

    cout << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // prepare();
  read();
  solve();
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int grundy(int m, int k) {
  if (m / k == 0)
    return 0;
  if (m % k == 0)
    return m / k;
  grundy(m - m / k - 1, k);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  int N;
  cin >> N;
  int total = 0;
  for (int i = 0; i < N; i++) {
    int a, k;
    cin >> a >> k;
    total ^= grundy(a, k);
  }
  cout << (total == 0 ? "Aoki" : "Takahashi") << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int grundy(int m, int k) {
  if (m / k == 0)
    return 0;
  if (m % k == 0)
    return m / k;
  return grundy(m - m / k - 1, k);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  int N;
  cin >> N;
  int total = 0;
  for (int i = 0; i < N; i++) {
    int a, k;
    cin >> a >> k;
    total ^= grundy(a, k);
  }
  cout << (total == 0 ? "Aoki" : "Takahashi") << endl;
}
Problem p03422 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;

int calc(int x, int k) {
  if (x % k == 0)
    return x / k;
  int d = x / k;
  if (x / k != (x - d - 1) / k)
    return calc(x - d - 1, k);
  int to = d * x;
  int g = (x - to) / (d + 1);
  x -= g * (d + 1);
  return calc(x, k);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    ans ^= calc(x, y);
  }
  if (ans)
    cout << "Takahashi\n";
  else
    cout << "Aoki\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;

int calc(int x, int k) {
  if (x % k == 0)
    return x / k;
  int d = x / k;
  if (x / k != (x - d - 1) / k)
    return calc(x - d - 1, k);
  int to = d * k;
  int g = (x - to) / (d + 1);
  x -= g * (d + 1);
  return calc(x, k);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    ans ^= calc(x, y);
  }
  if (ans)
    cout << "Takahashi\n";
  else
    cout << "Aoki\n";
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int N, ans;

int F(int a, int b) {
  if (a < b)
    return 0;
  if (a % b == 0)
    return a / b;
  return F(a - a / b - 1, b);
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    ans ^= F(a, b);
  }
  printf(ans ? "Takahashi\n" : "Aoki\n");
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int N, ans;

int F(int a, int b) {
  if (a < b)
    return 0;
  if (a % b == 0)
    return a / b;
  int x = a % b, y = a / b + 1;
  if (x % y)
    x += y;
  x -= x % y;
  return F(a - x, b);
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    ans ^= F(a, b);
  }
  printf(ans ? "Takahashi\n" : "Aoki\n");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans;
ll f(ll a, ll k) {
  if (a < k)
    return 0;
  if (a % k == 0)
    return a / k;
  else
    return f(a * (k - 1) / k, k);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    ll a, k;
    scanf("%lld%lld", &a, &k);
    ans ^= f(a, k);
  }
  if (!ans)
    puts("Aoki");
  else
    puts("Takahashi");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans;
ll f(ll a, ll k) {
  if (a < k)
    return 0;
  if (a % k == 0)
    return a / k;
  else {
    ll d = a / k;
    ll step = (a - d * k + d) / (d + 1);
    return f(a - step * (d + 1), k);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    ll a, k;
    scanf("%lld%lld", &a, &k);
    ans ^= f(a, k);
  }
  if (!ans)
    puts("Aoki");
  else
    puts("Takahashi");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

int IT_MAX = 1 << 16;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define Se second
#define Fi first

ll gr(ll A, ll K) {
  if (A < K)
    return 0;
  if (A % K == 0)
    return A / K;
  return gr(A - (A / K + 1), K);
}
int main() {
  int N, i;

  ll G = 0;
  scanf("%d", &N);
  while (N--) {
    ll t1, t2;
    scanf("%lld %lld", &t1, &t2);
    G ^= gr(t1, t2);
  }
  if (G)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

int IT_MAX = 1 << 16;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define Se second
#define Fi first

ll gr(ll A, ll K) {
  if (A < K)
    return 0;
  if (A % K == 0)
    return A / K;

  ll x = A / K;

  ll t = x * K + 1;
  ll v = A - t;
  A -= (v / (x + 1) + 1) * (x + 1);
  return gr(A, K);
}
int main() {
  int N, i;

  ll G = 0;
  scanf("%d", &N);
  while (N--) {
    ll t1, t2;
    scanf("%lld %lld", &t1, &t2);
    G ^= gr(t1, t2);
  }
  if (G)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define INF 987654321
#define ll long long

int N;

int f(int A, int K) {
  if (A < K)
    return 0;
  if (A % K == 0)
    return A / K;
  return f(A - (A / K + 1), K);
}

int main() {
  scanf("%d", &N);

  int res = 0, one = 0, zero = 0;
  for (int i = 0; i < N; i++) {
    int A, K;
    scanf("%d %d", &A, &K);
    int val = f(A, K);
    res ^= val;
    if (val == 0)
      zero++;
    else if (val == 1)
      one++;
  }

  if (one + zero == N) {
    if (one % 2 == 1)
      printf("Takahashi");
    else
      printf("Aoki");
  } else {
    if (res == 0)
      printf("Aoki");
    else
      printf("Takahashi");
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define INF 987654321
#define ll long long

int N;

int f(int A, int K) {
  if (A < K)
    return 0;
  if (A % K == 0)
    return A / K;

  int x = (A % K - 1) / (A / K + 1) +
          1; // A%K-1에서 1을 빼면 A%K==0인 경우만 예외로 뺄 수 있다.
  return f(A - x * (A / K + 1), K); // O(sqrt(A)) Optimization
}

int main() {
  scanf("%d", &N);

  int res = 0, one = 0, zero = 0;
  for (int i = 0; i < N; i++) {
    int A, K;
    scanf("%d %d", &A, &K);
    int val = f(A, K);
    res ^= val;
    if (val == 0)
      zero++;
    else if (val == 1)
      one++;
  }

  if (one + zero == N) {
    if (one % 2 == 1)
      printf("Takahashi");
    else
      printf("Aoki");
  } else {
    if (res == 0)
      printf("Aoki");
    else
      printf("Takahashi");
  }

  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ri register int
const int N = 205;
inline int SG(ri x, ri k) { return x % k == 0 ? x / k : SG(x - x / k - 1, k); }
int main() {
  ri n, x, k, sg;
  sg = 0;
  scanf("%d", &n);
  while (n--)
    scanf("%d%d", &x, &k), sg ^= SG(x, k);
  puts(sg ? "Takahashi" : "Aoki");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ri register int
const int N = 205;
inline int SG(ri x, ri k) {
  if (x < k)
    return 0;
  if (x % k == 0)
    return x / k;
  ri res = x / k + 1;
  if (x % k % res == 0)
    return x / k; // return (x-(x%k))/k;
  ri nxt = x / k * k - 1;
  x -= (x - nxt + res - 1) / res * res;
  return SG(x, k);
}
int main() {
  ri n, x, k, sg;
  sg = 0;
  scanf("%d", &n);
  while (n--)
    scanf("%d%d", &x, &k), sg ^= SG(x, k);
  puts(sg ? "Takahashi" : "Aoki");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int a, b, i, n, x;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++, x ^= a / b) {
    scanf("%d%d", &a, &b);
    while (a % b && a < (long long)b * b)
      a -= a / b + 1;
    if (a % b)
      while ((a -= a % b / (a / b + 1) * (a / b + 1)) % b)
        a -= a / b + 1;
  }
  return 0 * puts(x ? "Takahashi" : "Aoki");
}

Fixed submission:
#include <cstdio>
int a, b, i, n, x;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++, x ^= a / b) {
    scanf("%d%d", &a, &b);
    while (a % b && a > (long long)b * b)
      a -= a / b + 1;
    if (a % b)
      while ((a -= a % b / (a / b + 1) * (a / b + 1)) % b)
        a -= a / b + 1;
  }
  return 0 * puts(x ? "Takahashi" : "Aoki");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int g = 0;
  for (int i = 0; i < n; i++) {
    int a, k;
    cin >> a >> k;
    while (a % k != 0 && a > k) {
      a -= (a / k) + 1;
    }
    g ^= a / k;
  }
  cout << (g == 0 ? "Aoki" : "Takahashi") << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int g = 0;
  for (int i = 0; i < n; i++) {
    int a, k;
    cin >> a >> k;
    while (a % k != 0 && a > k) {
      int x = (a / k) + 1;
      int y = (x - 1) * k;
      int lo = 0, hi = 1e9;
      while (hi - lo > 1) {
        int mid = (hi + lo) / 2;
        if ((ll)a - 1LL * mid * x <= y) {
          hi = mid;
        } else {
          lo = mid;
        }
      }
      a -= hi * x;
    }
    g ^= a / k;
  }
  cout << (g == 0 ? "Aoki" : "Takahashi") << '\n';
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;

int main() {
  int N;
  scanf("%d", &N);

  int s = 0;

  for (; N--;) {
    int A, K;
    scanf("%d%d", &A, &K);
    while (A % K != 0)
      A -= (A + K - 1) / K;
    s ^= A / K;
  }

  puts(s == 0 ? "Aoki" : "Takahashi");
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;

int main() {
  int N;
  scanf("%d", &N);

  int s = 0;

  for (; N--;) {
    int A, K;
    scanf("%d%d", &A, &K);

    for (;;) {
      int x = (A + K - 1) / K, y = A % K;
      if (y % x == 0) {
        A -= y;
        break;
      }
      A -= (y + x - 1) / x * x;
    }
    s ^= A / K;
  }

  puts(s == 0 ? "Aoki" : "Takahashi");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int go(int n, int k) {
  if (n < k)
    return 0;
  if (n % k == 0)
    return n / k;
  return go(n - (n / k + 1), k);
}

int main() {
  int n;
  scanf("%d", &n);
  int an = 0;
  f(i, 0, n) {
    int a, k;
    scanf("%d%d", &a, &k);
    an ^= go(a, k);
  }
  printf("%s\n", an ? "Takahashi" : "Aoki");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int go(int n, int k) {
  if (n < k)
    return 0;
  if (n % k == 0)
    return n / k;
  int t = n / k * k;
  int d = (n - t + (n / k)) / (n / k + 1);
  return go(n - d * (n / k + 1), k);
}

int main() {
  int n;
  scanf("%d", &n);
  int an = 0;
  f(i, 0, n) {
    int a, k;
    scanf("%d%d", &a, &k);
    an ^= go(a, k);
  }
  printf("%s\n", an ? "Takahashi" : "Aoki");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int Grundy(int N, int K) { return N % K ? Grundy(N - 1 - N / K, K) : N / K; }
int main() {
  int N;
  cin >> N;
  int g = 0;
  for (auto i = 0; i < N; ++i) {
    int A, K;
    cin >> A >> K;
    g ^= Grundy(A, K);
  }
  cout << (g ? "Takahashi" : "Aoki") << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int Grundy(int N, int K) {
  if (N % K == 0)
    return N / K;
  auto d = (N + K - 1) / K;
  auto r = N % K;
  auto n = r / d;
  return Grundy(N - max(1, n) * d, K);
}
int main() {
  int N;
  cin >> N;
  int g = 0;
  for (auto i = 0; i < N; ++i) {
    int A, K;
    cin >> A >> K;
    g ^= Grundy(A, K);
  }
  cout << (g ? "Takahashi" : "Aoki") << endl;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 998244353;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const double EPS = 1e-13;
const double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int calc(int a, int b) {
  if (a % b == 0)
    return a / b;
  int to = a;
  int am = to % b / (a / b + 1);
  to -= am * (a / b + 1);
  return calc(to - a / b - 1, b);
}
int main() {
  int a;
  scanf("%d", &a);
  int ret = 0;
  for (int i = 0; i < a; i++) {
    int p, q;
    scanf("%d%d", &p, &q);
    int tmp = calc(p, q);
    ret ^= tmp;
  }
  if (ret == 0)
    printf("Aoki\n");
  else
    printf("Takahashi\n");
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 998244353;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const double EPS = 1e-13;
const double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }
int calc(int a, int b) {
  if (a % b == 0)
    return a / b;
  int to = a;
  int am = to % b / (a / b + 1);
  to -= am * (a / b + 1);
  if (am == 0) {
    to -= a / b + 1;
  }
  return calc(to, b);
}
int main() {
  int a;
  scanf("%d", &a);
  int ret = 0;
  for (int i = 0; i < a; i++) {
    int p, q;
    scanf("%d%d", &p, &q);
    int tmp = calc(p, q);
    ret ^= tmp;
  }
  if (ret == 0)
    printf("Aoki\n");
  else
    printf("Takahashi\n");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 205;

int n, ans, now;
int a[N], k[N], vis[N], sg[N];

int solve(int n, int m) {
  if (n < m)
    return 0;
  if (n % m == 0)
    return n / m;
  return solve(n - n / m - 1, m);
}

int main() {
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &k[i]);
    ans ^= solve(a[i], k[i]);
  }
  if (ans)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 205;

int n, ans, now;
int a[N], k[N], vis[N], sg[N];

int solve(int n, int m) {
  if (n < m)
    return 0;
  if (n % m == 0)
    return n / m;
  return solve(n - ((n % m - 1) / (n / m + 1) + 1) * (n / m + 1), m);
}

int main() {
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &k[i]);
    ans ^= solve(a[i], k[i]);
  }
  if (ans)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>

using namespace std;

int get(int a, int k) {
  while (a % k != 0) {
    a -= a / k + 1;
  }
  return a / k;
}

int main() {
  int n;
  cin >> n;

  int res = 0;

  for (int i = 0; i < n; i++) {
    int a, k;
    cin >> a >> k;

    int key = get(a, k);
    // cout << key << endl;
    res ^= key;
  }

  if (res == 0) {
    cout << "Aoki" << endl;
  } else {
    cout << "Takahashi" << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int get(int a, int k) {
  while (a % k != 0) {
    int c = a / k + 1;
    int l = a / k * k;
    a -= (a - l + c - 1) / c * c;
  }
  return a / k;
}

int main() {
  int n;
  cin >> n;

  int res = 0;

  for (int i = 0; i < n; i++) {
    int a, k;
    cin >> a >> k;

    int key = get(a, k);
    // cout << key << endl;
    res ^= key;
  }

  if (res == 0) {
    cout << "Aoki" << endl;
  } else {
    cout << "Takahashi" << endl;
  }

  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a, k, res;
int mex(int a, int k) {
  if (a % k == 0)
    return a / k;
  else if (a < k)
    return 0;
  else
    return mex(a - a / k - 1, k);
}
int main() {
  scanf("%d", &n);
  while (n--) {
    scanf("%d%d", &a, &k);
    res ^= mex(a, k);
  }
  if (res)
    puts("Takahashi");
  else
    puts("Aoki");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a, k, res;
int mex(int a, int k) {
  if (a % k == 0)
    return a / k;
  else if (a < k)
    return 0;
  else
    return mex(a - (a / k + 1) * ceil(1.0 * (a - a / k * k) / (a / k + 1)), k);
}
int main() {
  scanf("%d", &n);
  while (n--) {
    scanf("%d%d", &a, &k);
    res ^= mex(a, k);
  }
  if (res)
    puts("Takahashi");
  else
    puts("Aoki");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int ss = 0;
  for (int i = 1; i <= n; i++) {
    int x, k;
    scanf("%d%d", &x, &k);
    while (x % k != 0)
      x = x - x / k - 1;
    ss ^= x / k;
  }
  printf(ss ? "Takahashi\n" : "Aoki\n");
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int ss = 0;
  for (int i = 1; i <= n; i++) {
    int x, k;
    scanf("%d%d", &x, &k);
    while (x % k != 0) {
      int dx = x / k + 1;
      x = x - (x % k + dx - 1) / dx * dx;
    }
    ss ^= x / k;
  }
  printf(ss ? "Takahashi\n" : "Aoki\n");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()

using namespace std;

typedef long long ll;

ll grundy(ll a, ll k) {
  if (a % k == 0)
    return a / k;
  if (a == k + 1)
    return 0;
  return grundy(a - (a / k + 1) * max(1LL, (a % k) / k), k);
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  ll a, k, g = 0LL;
  rep(n) {
    cin >> a >> k;
    g = g ^ grundy(a, k);
  }

  if (g) {
    cout << "Takahashi" << endl;
  } else {
    cout << "Aoki" << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()

using namespace std;

typedef long long ll;

ll grundy(ll a, ll k) {
  if (a % k == 0)
    return a / k;
  if (a == k + 1)
    return 0;
  return grundy(a - (a / k + 1) * max(1LL, (a % k) / (a / k + 1)), k);
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  ll a, k, g = 0LL;
  rep(n) {
    cin >> a >> k;
    g = g ^ grundy(a, k);
  }

  if (g) {
    cout << "Takahashi" << endl;
  } else {
    cout << "Aoki" << endl;
  }
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define PB push_back
#define MP make_pair
#define FIR first
#define SEC second
#define ll long long
using namespace std;
template <class T> inline void rd(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    x = x * 10 - '0' + c, c = getchar();
  x *= f;
}
const int T = 30000;
int calc(int n, int k) {
  if (n < k)
    return 0;
  if (n % k == 0)
    return n / k;
  int t = (n / k + 1);
  if (t > T)
    return calc(n - t, k);
  int x = (n % k) / t;
  return calc(n - x * t, k);
}
int n, ans;
int main() {
  rd(n);
  for (int i = 1; i <= n; ++i) {
    int a, k;
    rd(a), rd(k);
    ans ^= calc(a, k);
  }
  if (ans)
    printf("Takahashi");
  else
    printf("Aoki");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define PB push_back
#define MP make_pair
#define FIR first
#define SEC second
#define ll long long
using namespace std;
template <class T> inline void rd(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    x = x * 10 - '0' + c, c = getchar();
  x *= f;
}
const int T = 30000;
int calc(int n, int k) {
  if (n < k)
    return 0;
  if (n % k == 0)
    return n / k;
  int t = (n / k + 1);
  if (t > T)
    return calc(n - t, k);
  int x = (n % k) / t;
  if ((n - x * t) % k)
    x++;
  return calc(n - x * t, k);
}
int n, ans;
int main() {
  rd(n);
  for (int i = 1; i <= n; ++i) {
    int a, k;
    rd(a), rd(k);
    ans ^= calc(a, k);
  }
  if (ans)
    printf("Takahashi");
  else
    printf("Aoki");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n;
long long x[210], k[210], a[210];

long long findsg(int x, int k) {
  if (x < k)
    return 0;
  if (x % k == 0)
    return x / k;
  if (k >= 100000) {
    long long op = x - k * (x / k);
    long long oq = op % (x / k + 1);
    x = k * (x / k) + oq;
    return findsg(x, k);
  } else
    return findsg(x - x / k - 1, k);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &x[i], &k[i]);
    a[i] = findsg(x[i], k[i]);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = ans ^ a[i];
  }
  if (ans != 0)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n;
long long x[210], k[210], a[210];

long long findsg(int x, int k) {
  if (x < k)
    return 0;
  if (x % k == 0)
    return x / k;
  if (k >= 100000) {
    long long op = x - k * (x / k);
    long long oq = op % (x / k + 1);
    x = k * (x / k) + oq;
    if (oq == 0)
      return x / k;
    return findsg(x - x / k - 1, k);
  } else
    return findsg(x - x / k - 1, k);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &x[i], &k[i]);
    a[i] = findsg(x[i], k[i]);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = ans ^ a[i];
  }
  if (ans != 0)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
template <typename X, typename Y>
istream &operator>>(istream &is, pair<X, Y> &p) {
  return is >> p.first >> p.second;
}
int grundy(int n, int k) {
  return n % k == 0 ? n / k : grundy(n - n / k - 1, k);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int ai, k;
    cin >> ai >> k;
    ans ^= grundy(ai, k);
  }
  cout << (ans ? "Takahashi" : "Aoki") << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define err(args...)                                                           \
  {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
  for (T &x : a) {
    is >> x;
  }
  return is;
}
template <typename X, typename Y>
istream &operator>>(istream &is, pair<X, Y> &p) {
  return is >> p.first >> p.second;
}
int grundy(int n, int k) {
  return n % k == 0 ? n / k : (n / k == 0 ? 0 : grundy(n - n / k - 1, k));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int ai, k;
    cin >> ai >> k;
    ans ^= grundy(ai, k);
  }
  cout << (ans ? "Takahashi" : "Aoki") << endl;
  return 0;
}