Problem p03422 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

long long N;
int A[210], K[210];

string sente = "Takahashi";
string gote = "Aoki";

int g[1000];
int rec(int v, int k) {
  if (g[v] != -1)
    return g[v];
  if (v == 0)
    return 0;
  set<int> se;
  for (int i = 1; i <= v / k; ++i) {
    int val = rec(v - i, k);
    se.insert(val);
  }
  int res = 0;
  while (se.count(res))
    ++res;
  return g[v] = res;
}

void testGrundy(int k) {
  memset(g, -1, sizeof(g));
  g[0] = 0;
  for (int i = 1; i <= 900; ++i) {
    cout << i << ": " << rec(i, k) << endl;
  }
}

int memo[210000];
int subGrundy(int a, int k) {
  if (a < k)
    return memo[a] = 0;
  if (memo[a] != -1)
    return memo[a];
  if (a % k == 0)
    return a / k;

  int p = a / k;
  int res = subGrundy(a - p - 1, k);

  if (a < 210000)
    return memo[a] = res;
  else
    return res;
}

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

  memset(memo, -1, sizeof(memo));
  return subGrundy(a, k);
}

string solve() {
  int res = 0;
  for (int i = 0; i < N; ++i) {
    int tmp = Grundy(A[i], K[i]);
    res ^= tmp;
  }
  if (res)
    return sente;
  else
    return gote;
}

int main() {

  // testGrundy(12);

  while (cin >> N) {
    for (int i = 0; i < N; ++i)
      cin >> A[i] >> K[i];
    cout << solve() << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s)                                                             \
  for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  EACH(it, P) { s << "<" << *it << "> "; }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; }
  return s << endl;
}

long long N;
int A[210], K[210];

string sente = "Takahashi";
string gote = "Aoki";

int g[1000];
int rec(int v, int k) {
  if (g[v] != -1)
    return g[v];
  if (v == 0)
    return 0;
  set<int> se;
  for (int i = 1; i <= v / k; ++i) {
    int val = rec(v - i, k);
    se.insert(val);
  }
  int res = 0;
  while (se.count(res))
    ++res;
  return g[v] = res;
}

void testGrundy(int k) {
  memset(g, -1, sizeof(g));
  g[0] = 0;
  for (int i = 1; i <= 900; ++i) {
    cout << i << ": " << rec(i, k) << endl;
  }
}

int memo[210000];
int subGrundy(int a, int k) {
  if (a < k)
    return memo[a] = 0;
  if (a < 210000 && memo[a] != -1)
    return memo[a];
  if (a % k == 0)
    return a / k;

  int p = a / k;
  int res = subGrundy(a - p - 1, k);

  if (a < 210000)
    return memo[a] = res;
  else
    return res;
}

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

  memset(memo, -1, sizeof(memo));
  return subGrundy(a, k);
}

string solve() {
  int res = 0;
  for (int i = 0; i < N; ++i) {
    int tmp = Grundy(A[i], K[i]);
    res ^= tmp;
  }
  if (res)
    return sente;
  else
    return gote;
}

int main() {

  // testGrundy(12);

  while (cin >> N) {
    for (int i = 0; i < N; ++i)
      cin >> A[i] >> K[i];
    cout << solve() << endl;
  }
}