Problem p00770 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef ostringstream OSS;
typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<VVD> VVVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<VVS> VVVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<VVB> VVVB;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
typedef vector<VVPII> VVVPII;
typedef vector<PLL> VPLL;
typedef vector<VPLL> VVPLL;
typedef vector<VVPLL> VVVPLL;

typedef unsigned int UI;
typedef vector<UI> VUI;
typedef vector<VUI> VVUI;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define RANGEBOX(x, y, maxX, maxY)                                             \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

template <typename T = int> inline void dump(vector<T> vs, bool ent = false) {
  rep(i, vs.size()) cout << vs[i] << (i + 1 == vs.size() ? '\\n' : ' ');
  if (ent)
    cout << endl;
}
template <typename T = int>
inline void dump(vector<vector<T>> vs, bool ent = false) {
  rep(i, vs.size()) dump<T>(vs[i]);
  if (ent)
    cout << endl;
}

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0};
const int DY[] = {0, -1, 0, 1};
const double EPS = 1e-12;
// const double PI   = acos(-1.0);
// lambda: [](T1 x)->T2{return y;}
// simple lambda: [](T x){return x;}

VB prime;
// O(sqrt(n) * n)
void initprime(LL num) {
  prime = VB(num + 1, true);
  prime[1] = prime[0] = false;
  for (LL i = 2; i * i <= num; i++) {
    if (prime[i]) {
      for (LL j = i + i; j <= num; j += i) {
        prime[j] = false;
      }
    }
  }
}

const int MAX_M = 1000010;
const int HW = 1010;
const int OFFSET = 505;
VVI ts = VVI(HW, VI(HW));
VPII toIdx(MAX_M);
int M, N;

void gen_ts() {
  int dir = 0;
  int x = OFFSET, y = OFFSET;
  int idx = 1;
  int cnt = 1;
  ts[y][x] = 1;
  toIdx[idx] = MP(y, x);
  while (true) {
    rep(i, 2) {
      rep(j, cnt) {
        ++idx;
        if (idx > MAX_M) {
          return;
        }
        x += DX[dir];
        y += DY[dir];
        ts[y][x] = idx;
        toIdx[idx] = MP(y, x);
      }
      dir = (dir + 1) % 4;
    }
    ++cnt;
  }
}

VVPII memo(HW, VPII(HW));

PII dfs(PII p) {
  int x = p.snd;
  int y = p.fst;
  if (memo[y][x].fst)
    return memo[y][x];

  int num = ts[y][x];
  bool is_b = prime[num];

  PII res = is_b ? MP(1, num) : MP(0, 0);

  rep(i, 3) {
    int nx = x + (i - 1);
    int ny = y + 1;
    int nnum = ts[ny][nx];
    if (nnum != 0 && nnum <= M) {
      PII tmp = dfs(MP(ny, nx));
      tmp.fst += is_b;
      res = max(res, tmp);
    }
  }

  return memo[y][x] = res;
}

// price, num
PII solve() {
  memo = VVPII(HW, VPII(HW));
  PII start = toIdx[N];
  PII res = dfs(start);
  return res;
}

int main(void) {
  initprime(1000010);
  gen_ts();

  while (cin >> M >> N, M) {
    PII x = solve();
    cout << x.fst << " " << x.snd << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

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

typedef ostringstream OSS;
typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<VVLL> VVVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<VVD> VVVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<VVS> VVVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<VVB> VVVB;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
typedef vector<VVPII> VVVPII;
typedef vector<PLL> VPLL;
typedef vector<VPLL> VVPLL;
typedef vector<VVPLL> VVVPLL;

typedef unsigned int UI;
typedef vector<UI> VUI;
typedef vector<VUI> VVUI;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define RANGEBOX(x, y, maxX, maxY)                                             \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N) for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

template <typename T = int> inline void dump(vector<T> vs, bool ent = false) {
  rep(i, vs.size()) cout << vs[i] << (i + 1 == vs.size() ? '\\n' : ' ');
  if (ent)
    cout << endl;
}
template <typename T = int>
inline void dump(vector<vector<T>> vs, bool ent = false) {
  rep(i, vs.size()) dump<T>(vs[i]);
  if (ent)
    cout << endl;
}

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0};
const int DY[] = {0, -1, 0, 1};
const double EPS = 1e-12;
// const double PI   = acos(-1.0);
// lambda: [](T1 x)->T2{return y;}
// simple lambda: [](T x){return x;}

VB prime;
// O(sqrt(n) * n)
void initprime(LL num) {
  prime = VB(num + 1, true);
  prime[1] = prime[0] = false;
  for (LL i = 2; i * i <= num; i++) {
    if (prime[i]) {
      for (LL j = i + i; j <= num; j += i) {
        prime[j] = false;
      }
    }
  }
}

const int MAX_M = 1000010;
const int HW = 5000;
const int OFFSET = 2000;
VVI ts = VVI(HW, VI(HW));
VPII toIdx(MAX_M);
int M, N;

void gen_ts() {
  int dir = 0;
  int x = OFFSET, y = OFFSET;
  int idx = 1;
  int cnt = 1;
  ts[y][x] = 1;
  while (true) {
    rep(i, 2) {
      rep(j, cnt) {
        ++idx;
        if (idx > MAX_M)
          return;
        x += DX[dir];
        y += DY[dir];
        ts[y][x] = idx;
        toIdx[idx] = MP(y, x);
      }
      dir = (dir + 1) % 4;
    }
    ++cnt;
  }
}

VVPII memo(HW, VPII(HW));

PII dfs(PII p) {
  int x = p.snd;
  int y = p.fst;
  if (memo[y][x].fst)
    return memo[y][x];

  int num = ts[y][x];
  bool is_b = prime[num];
  PII res(is_b ? 1 : 0, is_b ? num : 0);
  rep(i, 3) {
    int nx = x + (i - 1);
    int ny = y + 1;
    int nnum = ts[ny][nx];
    if (nnum != 0 && nnum <= M) {
      PII tmp = dfs(MP(ny, nx));
      tmp.fst += is_b;
      res = max(res, tmp);
    }
  }

  if (!res.snd && is_b) {
    res.snd = num;
  }

  return memo[y][x] = res;
}

// price, num
PII solve() {
  memo = VVPII(HW, VPII(HW));
  PII start = toIdx[N];
  PII res = dfs(start);
  return res;
}

int main(void) {
  initprime(1000010);
  gen_ts();

  while (cin >> M >> N, M) {
    PII x = solve();
    cout << x.fst << " " << x.snd << endl;
  }

  return 0;
}