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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T, class U> void assign(V<T> &v, int n, const U &a) {
  v.assign(n, a);
}
template <class T, class... Args>
void assign(V<T> &v, int n, const Args &...args) {
  v.resize(n);
  for (auto &&e : v)
    assign(e, args...);
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int g = 0;
  while (n--) {
    int a, k;
    cin >> a >> k;
    auto f = [&](const auto &f, int x) -> int {
      if (x % k == 0)
        return x / k;
      int y = (lint)(k - 1) * x / k;
      if (y == x - 1)
        return x / k;
      return f(f, y);
    };
    g ^= f(f, a);
  }
  cout << (g ? "Takahashi" : "Aoki") << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T, class U> void assign(V<T> &v, int n, const U &a) {
  v.assign(n, a);
}
template <class T, class... Args>
void assign(V<T> &v, int n, const Args &...args) {
  v.resize(n);
  for (auto &&e : v)
    assign(e, args...);
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int g = 0;
  while (n--) {
    int a, k;
    cin >> a >> k;
    auto f = [&](const auto &f, int x) -> int {
      if (x < k)
        return 0;
      int q = x / k, r = x - k * q;
      if (r == 0)
        return q;
      if (r < q + 1)
        return f(f, (lint)(k - 1) * x / k);
      return f(f, k * q + r % (q + 1));
    };
    g ^= f(f, a);
  }
  cout << (g ? "Takahashi" : "Aoki") << '\n';
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
void print(vector<int> x) {
  for (int i = 0; i < (int)x.size(); i++)
    printf("%d%c", x[i], " \n"[i == (int)x.size() - 1]);
}
int solve1(int n, int k) {
  if (n % k == 0)
    return n / k;
  return solve1(n - n / k - 1, k);
}
int solve2(int n, int k) {
  if (n % k == 0)
    return n / k;
  int d = n / k * k, go = (n - d) / (n / k + 1);
  if (!go)
    return solve2(n - n / k - 1, k);
  return solve2(n - go * (n / k + 1), k);
}
signed main() {
  int n = read(), ans = 0;
  for (int i = 1; i <= n; i++) {
    int a = read(), b = read();
    if (b >= 10000)
      ans ^= solve1(a, b);
    else
      ans ^= solve2(a, b);
  }
  if (ans)
    cout << "Takahashi\n";
  else
    cout << "Aoki\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
void print(vector<int> x) {
  for (int i = 0; i < (int)x.size(); i++)
    printf("%d%c", x[i], " \n"[i == (int)x.size() - 1]);
}
int solve1(int n, int k) {
  if (n % k == 0)
    return n / k;
  return solve1(n - n / k - 1, k);
}
int solve2(int n, int k) {
  if (n % k == 0)
    return n / k;
  int d = n / k * k, go = (n - d) / (n / k + 1);
  if (!go)
    return solve2(n - n / k - 1, k);
  return solve2(n - go * (n / k + 1), k);
}
signed main() {
  int n = read(), ans = 0;
  for (int i = 1; i <= n; i++) {
    int a = read(), b = read();
    if (b <= 10000)
      ans ^= solve1(a, b);
    else
      ans ^= solve2(a, b);
  }
  if (ans)
    cout << "Takahashi\n";
  else
    cout << "Aoki\n";
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
inline int calc(int n, int k) {
  while (n >= k && n % k)
    n -= n / k + 1;
  return n / k;
}
int main() {
  int t;
  cin >> t;
  int ans = 0;
  while (t--) {
    int n, k;
    cin >> n >> k;
    ans ^= calc(n, k);
  }
  return puts(ans ? "Takahashi" : "Aoki"), 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
inline int calc(int n, int k) {
  while (n % k) {
    int d = n / k + 1;
    n -= (n % k + d - 1) / d * d;
  }
  return n / k;
}
int main() {
  int t;
  cin >> t;
  int ans = 0;
  while (t--) {
    int n, k;
    cin >> n >> k;
    ans ^= calc(n, k);
  }
  return puts(ans ? "Takahashi" : "Aoki"), 0;
}
Problem p03422 (C++)

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

int work(int x, int y) {
  if (x < y)
    return 0;
  if (x % y == 0)
    return x / y;
  return work(x - x / y - 1, y);
}

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

  int sg = 0;
  for (int i = 1; i <= N; ++i) {
    int A, K;
    scanf("%d%d", &A, &K);
    sg ^= work(A, K);
  }
  if (sg == 0)
    puts("Aoki");
  else
    puts("Takahashi");
  return 0;
}


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

int work(int x, int y) {
  if (x < y)
    return 0;
  if (x % y == 0)
    return x / y;
  int d = x / y + 1;
  int nxt_x = y * (x / y);
  if ((x - nxt_x) % d == 0)
    return nxt_x / y;
  x -= ((x - nxt_x) / d + 1) * d;
  return work(x, y);
}

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

  int sg = 0;
  for (int i = 1; i <= N; ++i) {
    int A, K;
    scanf("%d%d", &A, &K);
    sg ^= work(A, K);
  }
  if (sg == 0)
    puts("Aoki");
  else
    puts("Takahashi");
  return 0;
}
Problem p03422 (Python)

Buggy submission (status: Runtime Error):
from math import ceil


def grundy(a, k):
    if a < k:
        return 0
    d, m = divmod(a, k)
    if m == 0:
        return d
    d1 = d + 1
    if d > k:
        return grundy(a - d1, k)
    e = ceil(m / d1) * d1
    return grundy(a - e, k)


n = int(input())
nim = 0
for _ in range(n):
    a, k = map(int, input().split())
    nim ^= grundy(a, k)
print("Takahashi" if nim > 0 else "Aoki")


Fixed submission:
import sys

sys.setrecursionlimit(10000000)
from math import ceil


def grundy(a, k):
    if a < k:
        return 0
    d, m = divmod(a, k)
    if m == 0:
        return d
    d1 = d + 1
    if d > k:
        return grundy(a - d1, k)
    e = ceil(m / d1) * d1
    return grundy(a - e, k)


n = int(input())
nim = 0
for _ in range(n):
    a, k = map(int, input().split())
    nim ^= grundy(a, k)
print("Takahashi" if nim > 0 else "Aoki")
Problem p03422 (C++)

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

int naive(int N, int K) {
  deque<int> q;
  q.push_front(0);
  int T = N / K;
  for (int i = 1; i <= T; i++) {
    int rot = K % q.size();
    for (int j = 0; j < rot; j++) {
      int cur = q.front();
      q.pop_front();
      q.push_back(cur);
    }
    q.push_front(i);
  }
  return q[(N % K) % q.size()];
}

int go(int N, int K) {
  while (1) {
    if (N % K == 0)
      return N / K;
    if (N < K)
      return 0;
    if (N / K <= 10000)
      return naive(N, K);
    N -= N / K + 1;
  }
}

int main() {
  int N;
  scanf("%d", &N);
  int g = 0;
  for (int i = 0; i < N; i++) {
    int a, k;
    scanf("%d%d", &a, &k);
    g ^= go(a, k);
  }
  printf(g ? "Takahashi\n" : "Aoki\n");
}


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

int naive(int N, int K) {
  deque<int> q;
  q.push_front(0);
  int T = N / K;
  for (int i = 1; i <= T; i++) {
    int rot = K % q.size();
    for (int j = 0; j < rot; j++) {
      int cur = q.front();
      q.pop_front();
      q.push_back(cur);
    }
    q.push_front(i);
  }
  return q[(N % K) % q.size()];
}

int go(int N, int K) {
  while (1) {
    if (N % K == 0)
      return N / K;
    if (N < K)
      return 0;
    if (N / K <= 100)
      return naive(N, K);
    N -= N / K + 1;
  }
}

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

Buggy submission (status: Time Limit Exceeded):
// Love and Freedom.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define ll long long
#define inf 20021225
#define N 210
using namespace std;
int read() {
  int s = 0, t = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      t = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * t;
}
int k;
int f(int x) {
  if (x % k == 0)
    return x / k;
  if (x < k)
    return 0;
  return f(x - x / k - 1);
}
int main() {
  int n = read(), sg = 0;
  for (int i = 1; i <= n; i++) {
    int x = read();
    k = read();
    sg ^= f(x);
  }
  puts(sg ? "Takahashi" : "Aoki");
  return 0;
}

Fixed submission:
// Love and Freedom.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define ll long long
#define inf 20021225
#define N 210
using namespace std;
int read() {
  int s = 0, t = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      t = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * t;
}
int k;
int f(int x) {
  if (x % k == 0)
    return x / k;
  if (x < k)
    return 0;
  int v = ((x % k - 1) / (x / k + 1) + 1) * (x / k + 1);
  return f(x - v);
}
int main() {
  int n = read(), sg = 0;
  for (int i = 1; i <= n; i++) {
    int x = read();
    k = read();
    sg ^= f(x);
  }
  puts(sg ? "Takahashi" : "Aoki");
  return 0;
}
Problem p03422 (C++)

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

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

int main() {
#ifdef isLOCAL
  freopen("work.in", "r", stdin);
  freopen("work.out", "w", stdout);
#endif

  int n, ans = 0, x, k;
  cin >> n;

  while (n--) {
    scanf("%d%d", &x, &k);
    ans ^= getsg(x, k);
  }

  if (ans == 0)
    puts("Aoki");
  else
    puts("Takahashi");

  return 0;
}


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

inline int getsg(int x, int k) {
  if (x % k == 0)
    return x / k;
  int d = x / k, t = (x - k * d - 1) / (d + 1) + 1;
  return getsg(x - t * (d + 1), k);
}

int main() {
#ifdef isLOCAL
  freopen("work.in", "r", stdin);
  freopen("work.out", "w", stdout);
#endif

  int n, ans = 0, x, k;
  cin >> n;

  while (n--) {
    scanf("%d%d", &x, &k);
    ans ^= getsg(x, k);
  }

  if (ans == 0)
    puts("Aoki");
  else
    puts("Takahashi");

  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
const int N = 50;
int f(int x, int k) {
  return x < k ? 0 : x % k ? f(x - (x / k) - 1, k) : (x / k);
}
int main() {
  int n, a, k, ans = 0;
  scanf("%d", &n);
  while (n--) {
    scanf("%d%d", &a, &k);
    ans ^= f(a, k);
  }
  puts(ans ? "Takahashi" : "Aoki");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
const int N = 50;
int f(int a, int k) {
  int x = a / k, b = a % k;
  if (!b)
    return x;
  int t = (b + x) / (x + 1);
  return f(a - (x + 1) * t, k);
}
int main() {
  int n, a, k, ans = 0;
  scanf("%d", &n);
  while (n--) {
    scanf("%d%d", &a, &k);
    ans ^= f(a, k);
  }
  puts(ans ? "Takahashi" : "Aoki");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;

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

  int g = 0;
  for (int i = 0; i < n; ++i) {
    int h = 0;
    while (a[i] >= k[i] && a[i] % k[i] != 0) {
      a[i] -= a[i] / k[i] + 1;
    }
    g ^= a[i] / k[i];
  }
  if (g != 0) {
    cout << "Takahashi\n";
  } else {
    cout << "Aoki\n";
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;

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

  int g = 0;
  for (int i = 0; i < n; ++i) {
    int h = 0;
    while (a[i] >= k[i] && a[i] % k[i] != 0) {
      a[i] = a[i] / k[i] * k[i] + (a[i] % k[i]) % (a[i] / k[i] + 1);
      if (a[i] >= k[i] && a[i] % k[i] != 0) {
        a[i] -= a[i] / k[i] + 1;
      }
    }
    g ^= a[i] / k[i];
  }
  if (g != 0) {
    cout << "Takahashi\n";
  } else {
    cout << "Aoki\n";
  }

  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

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

  int N;
  cin >> N;

  int G = 0;
  rep(i, N) {
    int A, K;
    cin >> A >> K;
    while (A % K) {
      A = (ll)A * (K - 1) / K;
    }
    G ^= A / K;
  }

  if (G)
    co("Takahashi");
  else
    co("Aoki");

  Would you please return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

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

  int N;
  cin >> N;

  int G = 0;
  rep(i, N) {
    int A, K;
    cin >> A >> K;
    while (A % K) {
      int kari = (A + K - 1) / K;
      int amari = A % K;
      int hiku = (amari + kari - 1) / kari * kari;
      A -= hiku;
    }
    G ^= A / K;
  }

  if (G)
    co("Takahashi");
  else
    co("Aoki");

  Would you please return 0;
}
Problem p03422 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<unsigned char, unsigned char> pcc;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> matrix;

// if x is a value and you have k, the next number is (kx)/(k-1) + 1

ll getNimber(ll n, ll k) {
  while (n % k && n > k) {
    // cout << n << " " << k << " " << n/k << endl;
    ll lhs = 0;
    ll rhs = n;
    bool found = false;
    ll decrement = 0;
    while (lhs <= rhs) {
      ll mid = (lhs + rhs) / 2;
      ll val = (mid * k) / (k - 1) + 1;
      if (val == n) {
        decrement = n - mid;
        found = true;
        break;
      }
      if (val > n)
        rhs = mid - 1;
      else
        lhs = mid + 1;
    }
    assert(found);
    for (ll dec = 1 << 30; dec > 0; dec /= 2) {
      ll candNext = n - dec * decrement;
      if (candNext / k == n / k || (dec == 1 && n % k != 0)) {
        n = candNext;
      }
    }
  }
  return n / k;
}

void solve() {
  int n;
  cin >> n;
  ll curr = 0;
  while (n--) {
    ll a, k;
    cin >> a >> k;
    curr ^= getNimber(a, k);
  }
  if (curr)
    cout << "Takahashi\n";
  else
    cout << "Aoki\n";
}

void casesolve() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cout << "Case #" << i << ":"
         << "\n";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  // casesolve();
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<unsigned char, unsigned char> pcc;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> matrix;

// if x is a value and you have k, the next number is (kx)/(k-1) + 1

ll getNimber(ll n, ll k) {
  while (n % k && n > k) {
    // cout << n << " " << k << " " << n/k << endl;
    // want mid * k / (k-1) + 1 == n
    // want mid * k == (n-1) * (k-1)
    ll mid = (n - 1) * (k - 1) / k;
    if ((mid * k) / (k - 1) + 1 != n)
      mid++;
    ll decrement = n - mid;
    for (ll dec = 1 << 30; dec > 0; dec /= 2) {
      ll candNext = n - dec * decrement;
      if (candNext / k == n / k || (dec == 1 && n % k != 0)) {
        n = candNext;
      }
    }
  }
  return n / k;
}

void solve() {
  int n;
  cin >> n;
  ll curr = 0;
  while (n--) {
    ll a, k;
    cin >> a >> k;
    curr ^= getNimber(a, k);
  }
  if (curr)
    cout << "Takahashi\n";
  else
    cout << "Aoki\n";
}

void casesolve() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cout << "Case #" << i << ":"
         << "\n";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  // casesolve();
}
Problem p03422 (C++)

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

#define y1 dmytxdy
#define pb push_back
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; }
template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; }

int readint() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int n, a, k, s;

ll qpow(ll x, ll p, ll cys) {
  ll ret = 1;
  for (; p; p >>= 1, x = x * x % cys)
    if (p & 1)
      ret = ret * x % cys;
  return ret;
}

int main() {
  n = readint();
  for (int i = 1; i <= n; i++) {
    a = readint();
    k = readint();
    while (a % k && a > 2 * k) {
      ll tmp = a / k + 1;
      ll num = (a - a / k * k) / k;
      a -= num * tmp;
      if (a % k == 0)
        break;
      a -= a / k + 1;
    }
    if (a % k == 0)
      s ^= a / k;
    else if (a > k)
      s ^= (a - k) & 1 ? 0 : 1;
  }
  if (s)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}

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

#define y1 dmytxdy
#define pb push_back
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; }
template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; }

int readint() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int n, a, k, s;

ll qpow(ll x, ll p, ll cys) {
  ll ret = 1;
  for (; p; p >>= 1, x = x * x % cys)
    if (p & 1)
      ret = ret * x % cys;
  return ret;
}

int main() {
  n = readint();
  for (int i = 1; i <= n; i++) {
    a = readint();
    k = readint();
    while (a % k && a > 2 * k) {
      ll r = a / k;
      ll tmp = r + 1;
      ll num = (a - r * k) / tmp;
      a -= num * tmp;
      if (a % k == 0)
        break;
      a -= a / k + 1;
    }
    if (a % k == 0)
      s ^= a / k;
    else if (a > k)
      s ^= (a - k) & 1 ? 0 : 1;
  }
  if (s)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
Cerberus97
Hanit Banga
*/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 10000;

inline int get(ll a, ll k);

int main() {
  // int k; cin >> k;
  // for (int i = 0; i < k; ++i) {
  // 	g[i] = 0;
  // }
  // for (int i = k; i < N; ++i) {
  // 	int l = i - i / k, r = i - 1;
  // 	set<int> s;
  // 	for (int j = l; j <= r; ++j) {
  // 		s.insert(g[j]);
  // 	}
  // 	while (s.find(g[i]) != s.end()) {
  // 		++g[i];
  // 	}
  // }
  // for (int i = 0; i < N; ++i) {
  // 	if (get(i, k) != g[i]) {
  // 		cout << i << ' ' << g[i] << ' ' << get(i, k) << endl;
  // 		break;
  // 	}
  // }
  int n;
  cin >> n;
  int x = 0;
  for (int i = 1; i <= n; ++i) {
    int a, k;
    cin >> a >> k;
    x ^= get(a, k);
  }
  if (x) {
    cout << "Takahashi";
  } else {
    cout << "Aoki";
  }
}

inline int get(ll a, ll k) {
  while (true) {
    if (a < k) {
      return 0;
    } else if (a % k == 0) {
      return a / k;
    }
    ll d = (a / k), i = (a - d * k) / (d + 1);
    a -= i * (d + 1);
  }
}

Fixed submission:
/*
Cerberus97
Hanit Banga
*/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 10000;

inline int get(ll a, ll k);

int main() {
  // int k; cin >> k;
  // for (int i = 0; i < k; ++i) {
  // 	g[i] = 0;
  // }
  // for (int i = k; i < N; ++i) {
  // 	int l = i - i / k, r = i - 1;
  // 	set<int> s;
  // 	for (int j = l; j <= r; ++j) {
  // 		s.insert(g[j]);
  // 	}
  // 	while (s.find(g[i]) != s.end()) {
  // 		++g[i];
  // 	}
  // }
  // for (int i = 0; i < N; ++i) {
  // 	if (get(i, k) != g[i]) {
  // 		cout << i << ' ' << g[i] << ' ' << get(i, k) << endl;
  // 		break;
  // 	}
  // }
  int n;
  cin >> n;
  int x = 0;
  for (int i = 1; i <= n; ++i) {
    int a, k;
    cin >> a >> k;
    x ^= get(a, k);
  }
  if (x) {
    cout << "Takahashi";
  } else {
    cout << "Aoki";
  }
}

inline int get(ll a, ll k) {
  while (true) {
    if (a < k) {
      return 0;
    } else if (a % k == 0) {
      return a / k;
    }
    ll d = (a / k), i = (a - d * (k - 1)) / (d + 1);
    a -= i * (d + 1);
  }
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, a, k, ans;
int find(int a, int k) {
  if (!(a % k))
    return a / k;
  if (a < k)
    return 0;
  int by = a / k + 1;
  int to = (by - 1 + a - a / k * k - 1) / by;
  return find(a - to * by, k);
}
int main() {
  //	freopen("readin.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &k);
    ans ^= find(a, k);
  }
  puts(ans ? "Takahashi" : "Aoki");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a, k, ans;
int find(int a, int k) {
  if (!(a % k))
    return a / k;
  if (a < k)
    return 0;
  int by = a / k + 1;
  if (a / k > (a - by) / k)
    return find(a - by, k);
  int to = a % k + by - 1;
  return find(a - to / by * by, k);
}
int main() {
  //	freopen("readin.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &k);
    ans ^= find(a, k);
  }
  puts(ans ? "Takahashi" : "Aoki");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  ll f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 1e5 + 10;
int main() {
  int n = read(), ans = 0;
  REP(i, 1, n) {
    int x = read(), y = read();
    while (x % y != 0) {
      x -= x / y + 1;
    }
    ans ^= x / y;
  }
  if (ans)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  ll f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 1e5 + 10;
int main() {
  int n = read(), ans = 0;
  REP(i, 1, n) {
    int x = read(), y = read();
    while (x % y != 0) {
      int r = (x / y) * y;
      x -= (x / y + 1) * max((x - r) / (x / y + 1), 1);
      if (x < y)
        break;
    }
    ans ^= x / y;
  }
  if (ans)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define seg_size 524288
long long calc(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  return calc(a - a / b - 1, b);
}
int main() {
  int n;
  cin >> n;
  long long hoge = 0;
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    hoge ^= calc(a, b);
  }
  if (hoge != 0) {
    cout << "Takahashi" << endl;
  } else {
    cout << "Aoki" << endl;
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define seg_size 524288
long long calc(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  long long hoge = a / b;
  long long geko = max(1LL, (a - hoge * b) / (hoge + 1));
  return calc(a - (a / b + 1) * geko, b);
}
int main() {
  int n;
  cin >> n;
  long long hoge = 0;
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    hoge ^= calc(a, b);
  }
  if (hoge != 0) {
    cout << "Takahashi" << endl;
  } else {
    cout << "Aoki" << endl;
  }
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = pair<lint, int>;
const int MAXN = 100005;
const int mod = 1e9 + 7;

int g(int x, int k) {
  while (x % k)
    x = (1ll * x * (k - 1)) / k;
  return x / k;
}

int main() {
  int q;
  cin >> q;
  int ans = 0;
  while (q--) {
    int x, y;
    cin >> x >> y;
    ans ^= g(x, y);
  }
  puts(ans ? "Takahashi" : "Aoki");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = pair<lint, int>;
const int MAXN = 100005;
const int mod = 1e9 + 7;

int g(int x, int k) {
  while (x % k) {
    int st = 0, ed = x;
    while (st != ed) {
      int m = (st + ed) / 2;
      if ((m + k - 1) / k == (x + k - 1) / k)
        ed = m;
      else
        st = m + 1;
    }
    int cur = (x + k - 1) / k;
    x -= cur * ((x - st) / cur + 1);
  }
  return x / k;
}

int main() {
  int q;
  cin >> q;
  int ans = 0;
  while (q--) {
    int x, y;
    cin >> x >> y;
    ans ^= g(x, y);
  }
  puts(ans ? "Takahashi" : "Aoki");
}
Problem p03422 (C++)

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

using namespace std;

using ll = long long;

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

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

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

using namespace std;

using ll = long long;

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

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

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

int N, K, sum;

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

int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    int x, y;
    cin >> x >> y;
    sum ^= solve(x, y);
  }
  puts(sum ? "Takahashi" : "Aoki");
  return 0;
}

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

int N, K, sum;

int solve(int x, int y) {
  if (x < y)
    return 0;
  if (x % y == 0)
    return x / y;
  int t = x / y + 1, tms = (x - y * (x / y) + t - 1) / t;
  return solve(x - t * tms, y);
}

int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    int x, y;
    cin >> x >> y;
    sum ^= solve(x, y);
  }
  puts(sum ? "Takahashi" : "Aoki");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;

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

int main(void) {
  LL N;
  cin >> N;
  LL ans = 0;
  for (int i = 0; i < N; ++i) {
    LL a, k;
    cin >> a >> k;
    ans ^= grundy(a, k);
  }
  if (ans) {
    cout << "Takahashi\n";
  } else {
    cout << "Aoki\n";
  }
  return 0;
}


Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;

LL grundy(LL a, LL k) {
  if (a < k) {
    return 0;
  }
  if (a % k == 0) {
    return a / k;
  }
  LL root = (a % k) / (a / k + 1);
  // root回まで変化させようとしてよい
  LL lim = (a % k) / (a / k + 1);
  LL tim = min(root, lim);
  if (tim <= 0)
    tim = 1;
  LL next = a - ((a / k) + 1);
  if (next < k) {
    return 0;
  }
  if (next % k == 0) {
    return next / k;
  }
  return grundy(a - ((a / k) + 1) * tim, k);
}

int main(void) {
  LL N;
  cin >> N;
  LL ans = 0;
  for (int i = 0; i < N; ++i) {
    LL a, k;
    cin >> a >> k;
    ans ^= grundy(a, k);
  }
  if (ans) {
    cout << "Takahashi\n";
  } else {
    cout << "Aoki\n";
  }
  return 0;
}
Problem p03422 (C++)

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


Fixed submission:
using namespace std;
#include <algorithm>
#include <cstdio>
#include <cstring>
int a, k;
int query(int a, int k, int x) {
  int t = x / k;
  x -= t * k;
  a -= t * k;
  int d = a / k;
  if (((x - k) % (d + 1) + (d + 1)) % (d + 1) == d)
    return d;
  return query(d * k - 1, k, ((x - k) % (d + 1) + (d + 1)) % (d + 1));
}
int main() {
  int n, xo = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &a, &k);
    int d = a / k;
    if (a < k)
      xo ^= 0;
    else if ((0 - (a % k + 1) % (d + 1) + (d + 1)) % (d + 1) == d)
      xo ^= d;
    else
      xo ^= query(d * k - 1, k,
                  ((0 - (a % k + 1)) % (d + 1) + (d + 1)) % (d + 1));
  }
  if (xo == 0)
    printf("Aoki\n");
  else
    printf("Takahashi\n");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
#define S(x) scanf("%d", &x)
#define S2(x, y) scanf("%d%d", &x, &y)
#define P(x) printf("%d\n", x)
#define all(v) v.begin(), v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int> pii;
typedef vector<int> vi;

int G[111];
set<int> s;
set<int>::iterator it;

int get(int x, int y) {
  if (x < y)
    return 0;
  int rem = x / y;
  s.clear();
  rep(i, 1, rem + 1) { s.insert(G[x - i]); }
  int cnt = 0;
  for (it = s.begin(); it != s.end(); it++) {
    if (*it != cnt) {
      return cnt;
    }
    cnt++;
  }
  return cnt;
}

void bf() {
  int k;
  S(k);
  rep(i, k, 100) {
    G[i] = get(i, k);
    printf("%d. %d\n", i, G[i]);
  }
}

int grundy(int x, int y) {
  if (x < y)
    return 0;
  if (x % y == 0)
    return x / y;
  while (x >= y) {
    if (x % y == 0)
      return x / y;
    x = (x * 1LL * (y - 1)) / y;
    // printf("%d %d\n",x,y);
  }
  return 0;
}

int main() {
  int n;
  int g = 0;
  S(n);
  rep(i, 0, n) {
    int x, y;
    S2(x, y);
    g ^= grundy(x, y);
  }
  if (g)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
#define S(x) scanf("%d", &x)
#define S2(x, y) scanf("%d%d", &x, &y)
#define P(x) printf("%d\n", x)
#define all(v) v.begin(), v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int> pii;
typedef vector<int> vi;

int G[111];
set<int> s;
set<int>::iterator it;

int get(int x, int y) {
  if (x < y)
    return 0;
  int rem = x / y;
  s.clear();
  rep(i, 1, rem + 1) { s.insert(G[x - i]); }
  int cnt = 0;
  for (it = s.begin(); it != s.end(); it++) {
    if (*it != cnt) {
      return cnt;
    }
    cnt++;
  }
  return cnt;
}

void bf() {
  int k;
  S(k);
  rep(i, k, 100) {
    G[i] = get(i, k);
    printf("%d. %d\n", i, G[i]);
  }
}

int grundy(int x, int y) {
  if (x < y)
    return 0;
  if (x % y == 0)
    return x / y;
  while (x >= y) {
    if (x % y == 0)
      return x / y;
    int diff = x / y;
    if (diff == 1) {
      if ((x - y) % 2)
        return 0;
      else
        return 1;
    }
    x -= diff + 1;
    // printf("%d %d\n",x,y);
  }
  return 0;
}

int main() {
  int n;
  int g = 0;
  S(n);
  rep(i, 0, n) {
    int x, y;
    S2(x, y);
    g ^= grundy(x, y);
  }
  if (g)
    printf("Takahashi\n");
  else
    printf("Aoki\n");
  return 0;
}
Problem p03422 (C++)

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

typedef long long ll;

int compute(int A, int K) {
  int B = A % K;
  int C = 1 + (A - A % K) / K;
  if (B % C == 0)
    return C - 1;
  else {
    return compute(A - C, K);
  }
}

int main() {
  int N;
  cin >> N;
  int A[N], K[N];
  int x = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> K[i];
    x = (x ^ compute(A[i], K[i]));
  }
  if (x == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
  return 0;
}


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

typedef long long ll;

int compute(int A, int K) {
  int B = A % K;
  int C = 1 + (A - A % K) / K;
  if (B % C == 0)
    return C - 1;
  else {
    return (compute(A - C * (1 + (B - B % C) / C), K));
  }
}

int main() {
  int N;
  cin >> N;
  int A[N], K[N];
  int x = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> K[i];
    x = (x ^ compute(A[i], K[i]));
  }
  if (x == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
int sg(int x, int y) {
  if (x < y)
    return 0;
  if (x % y == 0)
    return x / y;
  else
    return sg(x - x / y - 1, y);
}
int main() {
  int n, ans = 0;
  scanf("%d", &n);
  while (n--) {
    int x, y;
    scanf("%d%d", &x, &y);
    ans ^= sg(x, y);
  }
  printf("%s", ans ? "Takahashi" : "Aoki");
}


Fixed submission:
#include <cstdio>
int sg(int x, int y) {
  if (x < y)
    return 0;
  if (x % y == 0)
    return x / y;
  int v = x / y * y + x / y + 1;
  v = (v / (x / y + 1) + 1) * (x / y + 1) + x % (x / y + 1);
  if (v < x)
    return sg(v, y);
  else
    return sg(x - x / y - 1, y);
}
int main() {
  int n, ans = 0;
  scanf("%d", &n);
  while (n--) {
    int x, y;
    scanf("%d%d", &x, &y);
    ans ^= sg(x, y);
  }
  printf("%s", ans ? "Takahashi" : "Aoki");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
// Template
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
// using namespace atcoder;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T> inline bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
struct IOSET {
  IOSET() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  }
} ioset;

int main() {
  int n;
  cin >> n;
  int g = 0;
  auto rec = [&](auto f, int x, int y) -> int {
    if (x < y)
      return 0;
    if (x % y == 0)
      return x / y;
    int z = x / y + 1, w = x / y * y;
    // x -= (x - w) / z * z;
    return f(f, x - z, y);
  };
  rep(i, n) {
    int a, k;
    cin >> a >> k;
    g ^= rec(rec, a, k);
  }
  cout << (g ? "Takahashi\n" : "Aoki\n");
}


Fixed submission:
// Template
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
// using namespace atcoder;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T> inline bool chmin(T &x, const T &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmax(T &x, const T &y) {
  if (x < y) {
    x = y;
    return 1;
  }
  return 0;
}
struct IOSET {
  IOSET() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  }
} ioset;

int main() {
  int n;
  cin >> n;
  int g = 0;
  auto rec = [&](auto f, int x, int y) -> int {
    if (x < y)
      return 0;
    if (x % y == 0)
      return x / y;
    int z = x / y + 1, w = x / y * y;
    x -= (x - w) / z * z;
    if (x % y == 0)
      return x / y;
    return f(f, x - z, y);
  };
  rep(i, n) {
    int a, k;
    cin >> a >> k;
    g ^= rec(rec, a, k);
  }
  cout << (g ? "Takahashi\n" : "Aoki\n");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int sg(int a, int k) {
  int w = a / k, r = a % k;
  int d = r / (w + 1) + 1;
  return r ? sg(a - (w + 1) * d, k) : w;
}
int main() {
  int n, s = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int a, k;
    cin >> a >> k;
    s ^= sg(a, k);
  }
  cout << (s ? "Takahashi" : "Aoki") << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int sg(int a, int k) {
  int w = a / k, r = a % k;
  int d = r / (w + 1);
  return r % (w + 1) ? sg(a - (w + 1) * (d + 1), k) : w;
}
int main() {
  int n, s = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int a, k;
    cin >> a >> k;
    s ^= sg(a, k);
  }
  cout << (s ? "Takahashi" : "Aoki") << endl;
}
Problem p03422 (C++)

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

int T, n, k, ans;

int main() {
  scanf("%d", &T);
  while (T--) {
    function<int(int, int)> SG = [&](int n, int k) {
      if (n < k || !(n % k))
        return n / k;
      int v = n / k + 1, bound = n - n / (n / k);
      return SG(n - (bound + v - 1) / v * v, k);
    };
    scanf("%d %d", &n, &k), ans ^= SG(n, k);
  }
  printf("%s\n", ans ? "Takahashi" : "Aoki");
  return 0;
}

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

int T, n, k, ans;

int main() {
  scanf("%d", &T);
  while (T--) {
    function<int(int, int)> SG = [&](int n, int k) {
      if (n < k || !(n % k))
        return n / k;
      int v = n / k + 1, bound = n - k * (n / k);
      return SG(n - (bound + v - 1) / v * v, k);
    };
    scanf("%d %d", &n, &k), ans ^= SG(n, k);
  }
  printf("%s\n", ans ? "Takahashi" : "Aoki");
  return 0;
}