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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define all(a) a.begin(), a.end()
#define lb(s, x) lower_bound(all(s), x) - s.begin()
#define debug(x) cout << #x << ": " << x << endl
#define INF 1e9
typedef long long ll;
int n;

int LIS(vector<int> v) {
  vector<int> dp(n, INF);
  rep(i, n) { *lower_bound(all(dp), v[i]) = v[i]; }
  return lower_bound(all(dp), INF) - dp.begin();
}

vector<int> make(int a, int b) {
  vector<int> v;
  b--;
  int mx = n, mn = 1;
  int rem = n;
  bool modeA = true;
  while (true) {
    vector<int> tmp;
    if (modeA) {
      rep(i, a) {
        tmp.push_back(mx);
        mx--;
        rem--;
        if (rem == 0)
          break;
      }
      a--;
      modeA = false;
    } else {
      rep(i, b) {
        tmp.push_back(mn);
        mn++;
        rem--;
        if (rem == 0)
          break;
      }
      b--;
    }
    reverse(all(tmp));
    for (auto x : tmp) {
      v.push_back(x);
    }
    if (rem == 0)
      return v;
  }
  return v;
}

int main(void) {
  int a, b;
  cin >> n >> a >> b;
  vector<int> v = make(a, b);
  vector<int> rev = v;
  reverse(all(rev));
  if (LIS(v) == a && LIS(rev) == b) {
    rep(i, n) { printf("%d%c", v[i], (i < n - 1 ? ' ' : '\n')); }
  } else {
    cout << -1 << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define all(a) a.begin(), a.end()
#define lb(s, x) lower_bound(all(s), x) - s.begin()
#define debug(x) cout << #x << ": " << x << endl
#define INF 1e9
typedef long long ll;
int n;

int LIS(vector<int> v) {
  vector<int> dp(n, INF);
  rep(i, n) { *lower_bound(all(dp), v[i]) = v[i]; }
  return lower_bound(all(dp), INF) - dp.begin();
}

vector<int> make(int a, int b) {
  vector<int> v;
  b--;
  int mx = n, mn = 1;
  int rem = n;
  bool modeA = true;
  while (true) {
    vector<int> tmp;
    if (modeA) {
      rep(i, a) {
        tmp.push_back(mx);
        mx--;
        rem--;
        if (rem == 0)
          break;
      }
      a--;
      modeA = false;
    } else {
      rep(i, b) {
        tmp.push_back(mn);
        mn++;
        rem--;
        if (rem == 0)
          break;
      }
      b--;
      modeA = true;
    }
    reverse(all(tmp));
    for (auto x : tmp) {
      v.push_back(x);
    }
    if (rem == 0)
      return v;
  }
  return v;
}

int main(void) {
  int a, b;
  cin >> n >> a >> b;
  vector<int> v = make(a, b);
  vector<int> rev = v;
  reverse(all(rev));
  if (LIS(v) == a && LIS(rev) == b) {
    rep(i, n) { printf("%d%c", v[i], (i < n - 1 ? ' ' : '\n')); }
  } else {
    cout << -1 << endl;
  }

  return 0;
}
Problem p03421 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
using namespace std;
int N, A, B, que[300005];
int main(void) {
  int i, bl, l, r, t, now = 0;
  scanf("%d%d%d", &N, &A, &B);
  if (A + B > N + 1 || 1ll * A * B < N) {
    puts("-1");
    return 0;
  }
  r = N, l = r - A + 1;
  for (; now < A;)
    que[++now] = l + now - 1;
  t = 1;
  r = l - 1, l = max(1, r - A + 1);
  while (t + r + 1 - A >= B) {
    for (i = 1; i <= A && l + i - 1 <= r; ++i)
      que[++now] = l + i - 1;
    ++t, r = l - 1, l = max(1, r - A + 1);
  }
  l = 1, r = B - t, bl = r + 1;
  for (i = r; i; --i)
    que[++now] = i;
  for (i = 1; now <= N; ++i)
    que[++now] = bl + i - 1;
  for (i = 1; i <= N; ++i)
    printf("%d ", que[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int N, A, B, que[300005];
int main(void) {
  int i, bl, l, r, t, now = 0;
  scanf("%d%d%d", &N, &A, &B);
  if (A + B > N + 1 || 1ll * A * B < N) {
    puts("-1");
    return 0;
  }
  r = N, l = r - A + 1;
  for (; now < A;)
    que[++now] = l + now - 1;
  t = 1;
  r = l - 1, l = max(1, r - A + 1);
  while (r && t + r + 1 - min(A, r - l + 1) >= B) {
    for (i = 1; i <= A && l + i - 1 <= r; ++i)
      que[++now] = l + i - 1;
    ++t, r = l - 1, l = max(1, r - A + 1);
  }
  l = 1, r = B - t, bl = r + 1;
  for (i = r; i; --i)
    que[++now] = i;
  for (i = 1; now <= N; ++i)
    que[++now] = bl + i - 1;
  for (i = 1; i <= N; ++i)
    printf("%d ", que[i]);
  return 0;
}
Problem p03421 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int ans[SIZE];

bool solve(int n, int a, int b) {

  if (n + 1 < a + b) {
    return false;
  }

  int t = n;
  int used[SIZE] = {};

  for (int i = 0; i < n - b;) {
    b--;
    int rest = (n - b) - i;
    t -= min(a, rest);
    for (int j = 0; j < min(a, rest); j++) {
      int q = t + 1 + j;
      ans[i] = q;
      used[q] = true;
      i++;
    }
  }

  if (t < 0 || b < 0) {
    return false;
  }

  int k = n;

  for (int i = n - b; i < n; i++) {
    while (used[k])
      k--;
    ans[i] = k;
    used[k] = true;
  }

  return true;
}

int main() {
  int n, a, b;

  scanf("%d%d%d", &n, &a, &b);

  if (solve(n, a, b)) {
    for (int i = 0; i < n; i++) {
      printf("%d%c", ans[i], " \n"[i + 1 == n]);
    }
  } else {
    puts("-1");
  }

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 300010

int ans[SIZE];

bool solve(int n, int a, int b) {

  if (n + 1 < a + b) {
    return false;
  }

  int t = n;
  int used[SIZE] = {};

  for (int i = 0; i < n - b;) {
    b--;
    int rest = (n - b) - i;
    t -= min(a, rest);
    for (int j = 0; j < min(a, rest); j++) {
      int q = t + 1 + j;
      ans[i] = q;
      used[q] = true;
      i++;
    }
  }

  if (t < 0 || b < 0) {
    return false;
  }

  int k = n;

  for (int i = n - b; i < n; i++) {
    while (used[k])
      k--;
    ans[i] = k;
    used[k] = true;
  }

  return true;
}

int main() {
  int n, a, b;

  scanf("%d%d%d", &n, &a, &b);

  if (solve(n, a, b)) {
    for (int i = 0; i < n; i++) {
      printf("%d%c", ans[i], " \n"[i + 1 == n]);
    }
  } else {
    puts("-1");
  }

  return 0;
}
Problem p03421 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
void File() {
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
}
#define REP(i, a, b) for (register int i = a; i <= b; ++i)
#define DREP(i, a, b) for (register int i = a; i >= n; --i)
#define ll long long
const int maxn = 1e6 + 10;
ll n, a, b;
int s[maxn];
int main() {
  // File();
  cin >> n >> a >> b;
  if (n > a * b || n < a + b - 1)
    cout << -1 << endl;
  else {
    REP(i, 1, a) s[i] = n - a + i;
    ll unit = (n - a) / (b - 1), res = (n - a) % (b - 1);
    int p = a, from = n - a - unit + 1, last = n - a;
    while (p != n) {
      if (res)
        --res, --from;
      REP(i, from, last)
      s[++p] = i;
      last = from - 1;
      from = from - unit;
    }
    REP(i, 1, n) cout << s[i] << " ";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
void File() {
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
}
#define REP(i, a, b) for (register int i = a; i <= b; ++i)
#define DREP(i, a, b) for (register int i = a; i >= n; --i)
#define ll long long
const int maxn = 1e6 + 10;
ll n, a, b;
int s[maxn];
int main() {
  // File();
  cin >> n >> a >> b;
  if (n > a * b || n < a + b - 1)
    cout << -1 << endl;
  else {
    REP(i, 1, a) s[i] = n - a + i;
    if (b != 1) {
      ll unit = (n - a) / (b - 1), res = (n - a) % (b - 1);
      int p = a, from = n - a - unit + 1, last = n - a;
      while (p != n) {
        if (res)
          --res, --from;
        REP(i, from, last)
        s[++p] = i;
        last = from - 1;
        from = from - unit;
      }
    }
    REP(i, 1, n) cout << s[i] << " ";
  }
  return 0;
}
Problem p03421 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

long long N, A, B, p[100009];

int main() {
  cin >> N >> A >> B;
  for (int i = 0; i <= N - B; i++) {
    long long L = A - i;
    if (L <= 0)
      continue;
    long long R = N - i;
    if (R > L * B || R < L + (B - 1))
      continue;

    for (int j = 0; j < i; j++)
      p[j] = j + 1;
    int cx = N;
    for (int j = N - B; j <= N - 1; j++) {
      p[j] = cx;
      cx--;
    }
    int res = N - B - (L - 1);
    cx = N - B;
    for (int j = L - 2; j >= 0; j--) {
      long long G = res;
      if (G >= B - 1)
        G = B - 1;
      res -= G;
      int F = cx - 1;
      for (int k = cx - (G + 1); k <= cx - 1; k++) {
        p[k] = F + 1;
        F--;
      }
      cx -= (G + 1);
    }
    for (int j = 0; j < N; j++) {
      if (j)
        cout << " ";
      cout << p[j];
    }
    cout << endl;
    return 0;
  }
  cout << "-1" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

long long N, A, B, p[300009];

int main() {
  cin >> N >> A >> B;
  for (int i = 0; i <= N - B; i++) {
    long long L = A - i;
    if (L <= 0)
      continue;
    long long R = N - i;
    if (R > L * B || R < L + (B - 1))
      continue;

    for (int j = 0; j < i; j++)
      p[j] = j + 1;
    int cx = N;
    for (int j = N - B; j <= N - 1; j++) {
      p[j] = cx;
      cx--;
    }
    int res = N - B - (L - 1);
    cx = N - B;
    for (int j = L - 2; j >= 0; j--) {
      long long G = res;
      if (G >= B - 1)
        G = B - 1;
      res -= G;
      int F = cx - 1;
      for (int k = cx - (G + 1); k <= cx - 1; k++) {
        p[k] = F + 1;
        F--;
      }
      cx -= (G + 1);
    }
    for (int j = 0; j < N; j++) {
      if (j)
        cout << " ";
      cout << p[j];
    }
    cout << endl;
    return 0;
  }
  cout << "-1" << endl;
  return 0;
}
Problem p03421 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define mod 1000000007
#define mad(a, b) a = (a + b) % mod;
#define mul(a, b) a = a * b % mod;
vector<ll> v[100010];
int main() {
  ll n, a, b;
  cin >> n >> a >> b;
  if (n - a > a * (b - 1) || n - a < b - 1) {
    cout << -1 << endl;
    return 0;
  }
  ll s = 0;
  for (ll p = n - a; p > 0; p--) {
    if (v[s].size() == b - 1)
      s++;
    v[s].push_back(p);
  }
  for (ll i = a - 1; i >= 0; i--) {
    cout << n - i << endl;
    for (auto t : v[i])
      cout << t << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define mod 1000000007
#define mad(a, b) a = (a + b) % mod;
#define mul(a, b) a = a * b % mod;
vector<ll> v[300010];
int main() {
  ll n, a, b;
  cin >> n >> a >> b;
  if (n - a > a * (b - 1) || n - a < b - 1) {
    cout << -1 << endl;
    return 0;
  }
  ll s = 0;
  for (ll p = n - a; p > 0; p--) {
    if (v[s].size() == b - 1)
      s++;
    v[s].push_back(p);
  }
  for (ll i = a - 1; i >= 0; i--) {
    cout << n - i << endl;
    for (auto t : v[i])
      cout << t << endl;
  }
  return 0;
}
Problem p03421 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
// const int MOD=998244353;
const int INF = 1e9;
const long long LINF = 1e18;
#define int long long
// template
template <typename T> void fin(T a) {
  cout << a << endl;
  exit(0);
}
// main
signed main() {
  int N, A, B;
  cin >> N >> A >> B;
  if (A * B < N)
    fin(-1);
  if (A + B > N + 1)
    fin(-1);
  std::vector<int> ans(N);
  for (int i = 0; i < A; i++)
    ans[i] = N - A + 1 + i;
  int res = N - (A + B - 1);
  int now = A, mx = N - A;
  for (int i = 1; i < B; i++) {
    int p = min(res, A - 1);
    for (int j = 0; j <= p; j++)
      ans[now + j] = mx - p + j;
    mx -= p + 1;
    now += p + 1;
  }
  for (int i = 0; i < N; i++)
    cout << ans[i] << " ";
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
// const int MOD=998244353;
const int INF = 1e9;
const long long LINF = 1e18;
#define int long long
// template
template <typename T> void fin(T a) {
  cout << a << endl;
  exit(0);
}
// main
signed main() {
  int N, A, B;
  cin >> N >> A >> B;
  if (A * B < N)
    fin(-1);
  if (A + B > N + 1)
    fin(-1);
  std::vector<int> ans(N);
  for (int i = 0; i < A; i++)
    ans[i] = N - A + 1 + i;
  int res = N - (A + B - 1);
  int now = A, mx = N - A;
  for (int i = 1; i < B; i++) {
    int p = min(res, A - 1);
    for (int j = 0; j <= p; j++)
      ans[now + j] = mx - p + j;
    mx -= p + 1;
    now += p + 1;
    res -= p;
  }
  for (int i = 0; i < N; i++)
    cout << ans[i] << " ";
  cout << endl;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int grundy = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, k;
    cin >> a >> k;
    while (a % k != 0) {
      a -= (a / k) + 1;
    }
    grundy ^= (a / k);
  }
  if (grundy == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int grundy = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, k;
    cin >> a >> k;
    while (a % k != 0) {
      int old = a - a % k;
      int diff = a - old;
      int um = a / k + 1;
      int dl = diff / um;
      if (diff % um == 0)
        a -= dl * um;
      else
        a -= (dl + 1) * um;
    }
    grundy ^= (a / k);
  }
  if (grundy == 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;
using ll = long long;
template <class T, class U> inline bool chmin(T &x, U y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &x, U y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  unsigned x{}, a, k;
  fr(_, n) {
    cin >> a >> k;
    while (a >= k and a % k)
      a -= a / k + 1;
    if (a % k == 0)
      x ^= a / k;
  }
  puts(x ? "Takahashi" : "Aoki");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> inline bool chmin(T &x, U y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &x, U y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  unsigned x{}, a, k;
  fr(_, n) {
    cin >> a >> k;
    while (a >= k and a % k)
      a -= max(1u, a % k / (a / k + 1)) * (a / k + 1);
    if (a % k == 0)
      x ^= a / k;
  }
  puts(x ? "Takahashi" : "Aoki");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> PP;

/*
    freopen("input","r",stdin);
    freopen("output","w",stdout);
*/

ll A[200], K[200];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i] >> K[i];
  ll sol = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] >= K[i]) {
      ll crt = A[i];
      while (crt % K[i] != 0) {
        ll r = crt / K[i];
        ll rem = crt % K[i];
        crt -= (r + 1);
      }
      sol = sol ^ (crt / K[i]);
    }
  }
  if (sol != 0)
    cout << "Takahashi" << endl;
  else
    cout << "Aoki" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> PP;

/*
    freopen("input","r",stdin);
    freopen("output","w",stdout);
*/

ll A[200], K[200];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i] >> K[i];
  ll sol = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] >= K[i]) {
      ll crt = A[i];
      while (crt % K[i] != 0) {
        ll r = crt / K[i];
        ll d = (crt % K[i]) / (r + 1);
        crt -= (r + 1) * max(1LL, d);
      }
      sol = sol ^ (crt / K[i]);
    }
  }
  if (sol != 0)
    cout << "Takahashi" << endl;
  else
    cout << "Aoki" << endl;
  return 0;
}
Problem p03422 (C++)

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

using namespace std;
using ll = long long;

static ll grundy(ll a, ll k) {
  while (a % k != 0 && a >= k) {
    a = a * (k - 1) / k;
  }
  return a / k;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  ll g = 0;
  for (int i = 0; i < n; ++i) {
    ll a, k;
    cin >> a >> k;
    g ^= grundy(a, k);
  }
  if (g == 0) {
    cout << "Aoki" << endl;
  } else {
    cout << "Takahashi" << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

static ll grundy(ll a, ll k) {
  while (a % k != 0) {
    const ll t = a / k;
    const ll x = (a - t * k + t) / (t + 1);
    a -= x * (t + 1);
  }
  return a / k;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  ll g = 0;
  for (int i = 0; i < n; ++i) {
    ll a, k;
    cin >> a >> k;
    g ^= grundy(a, k);
  }
  if (g == 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;
#define REP(i, st, ed)                                                         \
  for (int i = (int)(st), i##end = (int)(ed); i <= i##end; ++i)
#define DREP(i, st, ed)                                                        \
  for (int i = (int)(st), i##end = (int)(ed); i >= i##end; --i)
template <typename T> bool chkmin(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> bool chkmax(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template <typename T> T read() {
  T x = 0, f = 1;
  char c = getchar();
  while ((c < '0') || (c > '9')) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ((c >= '0') && (c <= '9'))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
#define read() read<int>()
template <typename T> void write(T x, char c) {
  static char t[25];
  static int tlen;
  t[tlen = 1] = c;
  if (x < 0)
    putchar('-'), x = -x;
  do
    t[++tlen] = (x % 10) ^ 48;
  while (x /= 10);
  while (tlen)
    putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
int n, m, k, ans;
int main() {
  n = read();
  REP(i, 1, n) {
    m = read(), k = read();
    while (1) {
      if (m % k == 0) {
        ans ^= (m / k);
        break;
      }
      if (m < k)
        break;
      m -= m / k + 1;
    }
  }
  puts(ans ? "Takahashi" : "Aoki");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed)                                                         \
  for (int i = (int)(st), i##end = (int)(ed); i <= i##end; ++i)
#define DREP(i, st, ed)                                                        \
  for (int i = (int)(st), i##end = (int)(ed); i >= i##end; --i)
template <typename T> bool chkmin(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> bool chkmax(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template <typename T> T read() {
  T x = 0, f = 1;
  char c = getchar();
  while ((c < '0') || (c > '9')) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ((c >= '0') && (c <= '9'))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
#define read() read<int>()
template <typename T> void write(T x, char c) {
  static char t[25];
  static int tlen;
  t[tlen = 1] = c;
  if (x < 0)
    putchar('-'), x = -x;
  do
    t[++tlen] = (x % 10) ^ 48;
  while (x /= 10);
  while (tlen)
    putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
int n, m, k, ans;
int main() {
  n = read();
  REP(i, 1, n) {
    m = read(), k = read();
    while (1) {
      if (m % k == 0) {
        ans ^= (m / k);
        break;
      }
      m -= max(1, (m - (m / k) * k) / (m / k + 1)) * (m / k + 1);
    }
  }
  puts(ans ? "Takahashi" : "Aoki");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// ios::sync_with_stdio(false);
// cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppii;
typedef pair<int, pii> pipi;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> plpl;
typedef tuple<ll, ll, ll> tl;
ll mod = 1000000007;
ll mod2 = 998244353;
ll inf = 1000000000000000000;
double pi = 2 * acos(0);
#define rep(i, m, n) for (int i = m; i < n; i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
ll lmax(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll lmin(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  ll a[n], k[n];
  rep(i, 0, n) cin >> a[i] >> k[i];
  ll ans = 0;
  rep(i, 0, n) {
    if (a[i] < k[i])
      continue;
    ll now = a[i];
    for (;;) {
      if (now % k[i] == 0) {
        ans = ans ^ (now / k[i]);
        break;
      }
      now -= now / k[i] + 1;
    }
  }
  if (ans == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// ios::sync_with_stdio(false);
// cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppii;
typedef pair<int, pii> pipi;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> plpl;
typedef tuple<ll, ll, ll> tl;
ll mod = 1000000007;
ll mod2 = 998244353;
ll inf = 1000000000000000000;
double pi = 2 * acos(0);
#define rep(i, m, n) for (int i = m; i < n; i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
ll lmax(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll lmin(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  ll a[n], k[n];
  rep(i, 0, n) cin >> a[i] >> k[i];
  ll ans = 0;
  rep(i, 0, n) {
    if (a[i] < k[i])
      continue;
    ll now = a[i];
    for (;;) {
      if (now % k[i] == 0) {
        ans = ans ^ (now / k[i]);
        break;
      }
      ll u = now % k[i];
      ll y = now / k[i] + 1;
      u /= y;
      if (u == 0) {
        now -= y;
      } else {
        now -= u * y;
      }
    }
  }
  if (ans == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
void File() {
  freopen("ARC91F.in", "r", stdin);
  freopen("ARC91F.out", "w", stdout);
}
template <typename T> bool chkmax(T &_, T __) {
  return _ < __ ? (_ = __, 1) : 0;
}
template <typename T> bool chkmin(T &_, T __) {
  return _ > __ ? (_ = __, 1) : 0;
}
#define REP(i, a, b) for (register int i = a; i <= b; ++i)
#define DREP(i, a, b) for (register int i = a; i >= b; --i)
#define MREP(i, x) for (register int i = beg[x]; i; i = E[i].last)
#define mem(a) memset(a, 0, sizeof(a))
#define ll long long
#define inf INT_MAX
const int maxn = 1000 + 10;
int n, a, k, ans;
int get_SG(int x) {
  // cout<<x<<endl;
  if (x % k == 0)
    return x / k;
  return get_SG(x - (x / k) - 1);
}
int main() {
  // File();
  cin >> n;
  REP(i, 1, n) {
    cin >> a >> k;
    ans ^= get_SG(a);
  }
  if (ans)
    puts("Takahashi");
  else
    puts("Aoki");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
void File() {
  freopen("ARC91F.in", "r", stdin);
  freopen("ARC91F.out", "w", stdout);
}
template <typename T> bool chkmax(T &_, T __) {
  return _ < __ ? (_ = __, 1) : 0;
}
template <typename T> bool chkmin(T &_, T __) {
  return _ > __ ? (_ = __, 1) : 0;
}
#define REP(i, a, b) for (register int i = a; i <= b; ++i)
#define DREP(i, a, b) for (register int i = a; i >= b; --i)
#define MREP(i, x) for (register int i = beg[x]; i; i = E[i].last)
#define mem(a) memset(a, 0, sizeof(a))
#define ll long long
#define inf INT_MAX
const int maxn = 1000 + 10;
int n, a, k, ans;
int get_SG(int x) {
  // cout<<x<<endl;
  if (x % k == 0)
    return x / k;
  if ((x - (x / k) - 1) / k != x / k)
    return get_SG(x - (x / k) - 1);
  int b = x - x / k * k, t = b / ((x / k) + 1);
  return get_SG(x - t * ((x / k) + 1));
}
int main() {
  // File();
  cin >> n;
  REP(i, 1, n) {
    cin >> a >> k;
    ans ^= get_SG(a);
  }
  if (ans)
    puts("Takahashi");
  else
    puts("Aoki");
  return 0;
}
Problem p03422 (C++)

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

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

int main() {
  int n;
  cin >> n;
  int gn = 0;
  while (n--) {
    int a, k;
    cin >> a >> k;
    gn ^= g(a, k);
  }
  cout << (gn == 0 ? "Aoki\n" : "Takahashi\n");
}


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

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

int main() {
  int n;
  cin >> n;
  int gn = 0;
  while (n--) {
    int a, k;
    cin >> a >> k;
    gn ^= g(a, k);
  }
  cout << (gn == 0 ? "Aoki\n" : "Takahashi\n");
}
Problem p03422 (C++)

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
int A, K;
int gr[101010];

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

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  /*
  for(i=0;i<=100;i++) {
          set<int> S;
          for(x=1;x<=i/N;x++) S.insert(gr[i-x]);
          while(S.count(gr[i])) gr[i]++;
          cout<<i<<" "<<gr[i]<<" "<<hoge(i,N)<<endl;
  }

  return;
  */
  int nim = 0;
  FOR(i, N) {
    cin >> A >> K;
    nim ^= hoge(A, K);
  }
  if (nim == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
int A, K;
int gr[101010];

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

  int dif = ((A / K) + 1);
  return hoge(A - max((A % K) / dif, 1) * dif, K);
}

void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  /*
  for(i=0;i<=100;i++) {
          set<int> S;
          for(x=1;x<=i/N;x++) S.insert(gr[i-x]);
          while(S.count(gr[i])) gr[i]++;
          cout<<i<<" "<<gr[i]<<" "<<hoge(i,N)<<endl;
  }

  return;
  */
  int nim = 0;
  FOR(i, N) {
    cin >> A >> K;
    nim ^= hoge(A, K);
  }
  if (nim == 0)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
Problem p03422 (C++)

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

int main() {
  auto Solve = [&](int n, int k) {
    while (n % k != 0)
      n -= n / k + 1;
    return n / k;
  };

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


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

int main() {
  auto Solve = [&](int n, int k) {
    while (n % k != 0) {
      int pv = n / k * k;
      int g = max(1, (n - pv) / (n / k + 1));
      n -= g * (n / k + 1);
    }
    return n / k;
  };

  int n;
  scanf("%d", &n);
  int sg = 0;
  for (int i = 0; i < n; ++i) {
    int a, k;
    scanf("%d%d", &a, &k);
    sg ^= Solve(a, k);
  }
  if (sg == 0)
    puts("Aoki");
  else
    puts("Takahashi");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x, y, h, w) x >= 0 && x < h &&y >= 0 && y < w

#define int long long
// typedef    long long          ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

signed main() {
  int n, g = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    while (a >= b && a % b)
      a -= a / b + 1;
    g ^= (a / b);
  }
  if (!g)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x, y, h, w) x >= 0 && x < h &&y >= 0 && y < w

#define int long long
// typedef    long long          ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

signed main() {
  int n, g = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    while (a >= b && a % b) {
      if ((a - (a / b + 1)) / b < a / b)
        a -= a / b + 1;
      else {
        int t = b * (int)(a / b), d = a / b + 1;
        a = t + (a % d - t % d + d) % d;
      }
    }
    g ^= (a / b);
  }
  if (!g)
    cout << "Aoki" << endl;
  else
    cout << "Takahashi" << endl;
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define ll long long
#define rep2(i, a, b) for (int i = a; i <= b; ++i)
#define rep(i, n) for (int i = 0; i < n; i++)
#define pii pair<int, int>
#define tii tuple<int, int, int>
#define pq priority_queue<int>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define edge(v, a, b)                                                          \
  v[a].pb(b);                                                                  \
  v[b].pb(a);
ll int MOD = 1e9 + 7;
#define INF 2 * 1e9
#define N 310000
using namespace std;
string alphabet("abcdefghijklmnopqrstuvwxyz");

int nim(int a, int b) {
  if (a < b)
    return 0;
  if (!(a % b)) {
    return a / b;
  }
  return nim(a - (a - (a / b) * b - 1) / (a / b + 1) * (a / b + 1), b);
}
int main() {
  int n, ans = 0;
  cin >> n;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    ans ^= nim(a, b);
  }
  cout << ((ans == 0) ? "Aoki" : "Takahashi");
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define ll long long
#define rep2(i, a, b) for (int i = a; i <= b; ++i)
#define rep(i, n) for (int i = 0; i < n; i++)
#define pii pair<int, int>
#define tii tuple<int, int, int>
#define pq priority_queue<int>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define edge(v, a, b)                                                          \
  v[a].pb(b);                                                                  \
  v[b].pb(a);
ll int MOD = 1e9 + 7;
#define INF 2 * 1e9
#define N 310000
using namespace std;
string alphabet("abcdefghijklmnopqrstuvwxyz");

int nim(int a, int b) {
  if (a < b)
    return 0;
  if (!(a % b)) {
    return a / b;
  }
  return nim(a - ((a - (a / b) * b - 1) / (a / b + 1) + 1) * (a / b + 1), b);
}
int main() {
  int n, ans = 0;
  cin >> n;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    ans ^= nim(a, b);
  }
  cout << ((ans == 0) ? "Aoki" : "Takahashi");
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

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

  int N;
  cin >> N;
  int x = 0;
  while (N--) {
    int a, b;
    cin >> a >> b;
    while (a % b) {
      a -= a / b + 1;
    }
    x ^= a / b;
  }
  if (x == 0) {
    cout << "Aoki\n";
  } else {
    cout << "Takahashi\n";
  }
}

Fixed submission:
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

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

  int N;
  cin >> N;
  int x = 0;
  while (N--) {
    int a, b;
    cin >> a >> b;
    while (a % b && a > b) {
      int amari = a % b;
      int by = max(1, amari / (a / b + 1));
      a -= (a / b + 1) * by;
    }
    x ^= a / b;
  }
  if (x == 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 gao(int n, int k) {
  if (n % k == 0)
    return n / k;
  return gao(n - n / k - 1, k);
}

int main() {
  int T, n, k, ans = 0;
  for (cin >> T; T--;) {
    cin >> n >> k;
    ans ^= gao(n, k);
  }
  puts(ans ? "Takahashi" : "Aoki");
}

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

int gao(int n, int k) {
  if (n % k == 0)
    return n / k;
  int dc = n / k + 1, nex = n / k * k + 1;
  if (n - nex < dc)
    return gao(n - dc, k);
  else
    return gao(n - (n - nex) / dc * dc, k);
}

int main() {
  int T, n, k, ans = 0;
  for (cin >> T; T--;) {
    cin >> n >> k;
    ans ^= gao(n, k);
  }
  puts(ans ? "Takahashi" : "Aoki");
}
Problem p03422 (C++)

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

const int MAGIC = 35000;

int n;
int res;

int grundy(int n, int k) {
  // if n % k == 0 -> grundy(n) = n / k
  // else -> grundy(n) = grundy(n - ceil(n / k))

  if (k > MAGIC) {
    while (n % k != 0) {
      n -= n / k + 1;
    }
    return n / k;
  } else {
    int val = n / k + 1;

    while (val > 0 && n % k != 0) {
      int low = 0, high = 1e9;
      while (low < high) {
        int mid = ((low + high) >> 1);
        if (n - 1LL * mid * val <= 1LL * (val - 1) * k)
          high = mid;
        else
          low = mid + 1;
      }
      n -= 1LL * low * val;
      --val;
    }

    return n / k;
  }
}

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

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int a, k;
    cin >> a >> k;
    res ^= grundy(a, k);
  }

  if (res > 0)
    cout << "Takahashi\n";
  else
    cout << "Aoki\n";
}

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

const int MAGIC = 35000;

int n;
int res;

int grundy(int n, int k) {
  // if n % k == 0 -> grundy(n) = n / k
  // else -> grundy(n) = grundy(n - ceil(n / k))

  if (k < MAGIC) {
    while (n % k != 0) {
      n -= n / k + 1;
    }
    return n / k;
  } else {
    int val = n / k + 1;

    while (val > 0 && n % k != 0) {
      int low = 0, high = 1e9;
      while (low < high) {
        int mid = ((low + high) >> 1);
        if (n - 1LL * mid * val <= 1LL * (val - 1) * k)
          high = mid;
        else
          low = mid + 1;
      }
      n -= 1LL * low * val;
      --val;
    }

    return n / k;
  }
}

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

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int a, k;
    cin >> a >> k;
    res ^= grundy(a, k);
  }

  if (res > 0)
    cout << "Takahashi\n";
  else
    cout << "Aoki\n";
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200009
#define MAXM 320
#define MOD 1000000007
#define INF 1000000009
#define FOR(x, n) for (int x = 0; x < n; x++)
#define FOR1e(x, n) for (int x = 1; x <= n; x++)
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;

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

  int f = a / k;
  return grundy(a - f - 1, k);
}
int main() {
  int n, k, a;
  cin >> n;

  int x = 0;
  FOR(i, n) {
    scanf("%d %d", &a, &k);
    x ^= grundy(a, k);
  }
  if (x)
    puts("Takahashi");
  else
    puts("Aoki");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200009
#define MAXM 320
#define MOD 1000000007
#define INF 1000000009
#define FOR(x, n) for (int x = 0; x < n; x++)
#define FOR1e(x, n) for (int x = 1; x <= n; x++)
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;

int grundy(int a, int k) {
  if (a % k == 0)
    return a / k;
  if (a < k)
    return 0;
  int diff = a / k + 1;
  int f = (a % k + diff - 1) / diff;
  return grundy(a - f * diff, k);
}
int main() {
  int n, k, a;
  cin >> n;

  int x = 0;
  FOR(i, n) {
    scanf("%d %d", &a, &k);
    x ^= grundy(a, k);
  }
  if (x)
    puts("Takahashi");
  else
    puts("Aoki");
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

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

int main() {
  int n;
  cin >> n;
  int32_t grundy = 0;
  REP(i, n) {
    int a, k;
    cin >> a >> k;
    grundy ^= score(a, k);
  }
  if (grundy == 0) {
    cout << "Aoki" << endl;
  } else {
    cout << "Takahashi" << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

int score(int a, int k) {
  if (a < k)
    return 0;
  if (a % k == 0)
    return a / k;
  int rem = a % k;
  int c = (rem + a / k) / (a / k + 1);
  return score(a - c * (a / k + 1), k);
}

int main() {
  int n;
  cin >> n;
  int32_t grundy = 0;
  REP(i, n) {
    int a, k;
    cin >> a >> k;
    grundy ^= score(a, k);
  }
  if (grundy == 0) {
    cout << "Aoki" << endl;
  } else {
    cout << "Takahashi" << endl;
  }
  return 0;
}
Problem p03422 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-3-12 16:05:13
 * Powered by Visual Studio Code
 */

#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <iomanip> // << fixed << setprecision(xxx)
#include <iostream>
#include <map> // if (M.find(key) != M.end()) { }
#include <queue>
#include <random> // random_device rd; mt19937 mt(rd());
#include <set>
#include <stack>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <tuple>
#include <vector>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

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

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

Fixed submission:
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-3-12 16:05:13
 * Powered by Visual Studio Code
 */

#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <iomanip> // << fixed << setprecision(xxx)
#include <iostream>
#include <map> // if (M.find(key) != M.end()) { }
#include <queue>
#include <random> // random_device rd; mt19937 mt(rd());
#include <set>
#include <stack>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <tuple>
#include <vector>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int k;
int loop(int a) {
  if (a < k)
    return 0;
  if (a % k == 0)
    return a / k;
  int tmp = a / k * k, tmp1 = a % k, tmp2 = a / k;
  return loop(tmp - tmp2 + tmp1 - 1);
}

int main() {
  int n, a;
  while (cin >> n) {
    ll tmp = 0;
    for (int i = 0; i < n; ++i) {
      cin >> a >> k;
      tmp ^= loop(a);
    }
    if (tmp == 0)
      cout << "Aoki" << endl;
    else
      cout << "Takahashi" << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int k;
int loop(int a) {
  if (a < k)
    return 0;
  if (a % k == 0)
    return a / k;
  int tmp = a / k * k, tmp1 = a % k, tmp2 = a / k;
  tmp1 %= tmp2 + 1;
  if (tmp1 == 0)
    return tmp2;
  return loop(tmp - tmp2 + tmp1 - 1);
}

int main() {
  int n, a;
  while (cin >> n) {
    ll tmp = 0;
    for (int i = 0; i < n; ++i) {
      cin >> a >> k;
      tmp ^= loop(a);
    }
    if (tmp == 0)
      cout << "Aoki" << endl;
    else
      cout << "Takahashi" << endl;
  }
  return 0;
}