Problem p00770 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

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

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

bool isPrime[1000100];
int prime[1000100];

int p;
void erats() {
  for (int i = 0; i < 1000100; i++)
    isPrime[i] = true;
  p = 0;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < 1000100; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j < 1000100; j += i)
        isPrime[j] = false;
    }
  }
}

int N, M;
map<pair<short, short>, int> m;
pair<short, short> idToPii[1000200];
pii dp[1000100];

pii dfs(int pos) {
  if (dp[pos].first >= 0)
    return dp[pos];
  pii res = pii(isPrime[pos], isPrime[pos] ? pos : 0);
  for (int i = -1; i <= 1; i++) {
    pii nxt = idToPii[pos];
    nxt.first++;
    nxt.second += i;
    if (m.count(nxt) && m[nxt] <= M) {
      pii p = dfs(m[nxt]);
      if (isPrime[pos])
        p.first++;
      if (res.first < p.first)
        res = p;
      else if (res.first == p.first) {
        if (res.second < p.second)
          res = p;
      }
    }
  }
  return dp[pos] = res;
}

int main() {
  erats();

  int sum = 1;
  int pos = 0;
  pii now = pii(0, 0);
  m[now] = 1;
  idToPii[sum] = now;
  sum++;
  for (int i = 1;; i++) {
    for (int j = 0; j < 2; j++) {
      // posの方向へiだけ進む
      for (int k = 0; k < i; k++) {
        now.first += dy[pos];
        now.second += dx[pos];
        m[now] = sum;
        idToPii[sum] = now;
        sum++;
        if (sum >= 1000100)
          break;
      }
      pos = (pos + 1) % 4;
    }
    if (sum >= 1000100)
      break;
  }
  // return 0;
  while (cin >> M >> N && (N | M)) {
    for (int i = 0; i <= M; i++)
      dp[i].first = dp[i].second = -1;
    pii p = dfs(N);
    cout << p.first << " " << p.second << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

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

bool isPrime[1000100];
int prime[1000100];

int p;
void erats() {
  for (int i = 0; i < 1000100; i++)
    isPrime[i] = true;
  p = 0;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i < 1000100; i++) {
    if (isPrime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j < 1000100; j += i)
        isPrime[j] = false;
    }
  }
}

int N, M;
map<pair<short, short>, int> m;
pair<short, short> idToPii[1000200];
pii dp[1000100];

pii dfs(int pos) {
  if (dp[pos].first >= 0)
    return dp[pos];
  pii res = pii(isPrime[pos], isPrime[pos] ? pos : 0);
  for (int i = -1; i <= 1; i++) {
    pii nxt = idToPii[pos];
    nxt.first++;
    nxt.second += i;
    if (m.count(nxt) && m[nxt] <= M) {
      pii p = dfs(m[nxt]);
      if (isPrime[pos])
        p.first++;
      if (res.first < p.first)
        res = p;
      else if (res.first == p.first) {
        if (res.second < p.second)
          res = p;
      }
    }
  }
  return dp[pos] = res;
}

int main() {
  erats();

  int sum = 1;
  int pos = 0;
  pii now = pii(0, 0);
  m[now] = 1;
  idToPii[sum] = now;
  sum++;
  for (int i = 1;; i++) {
    for (int j = 0; j < 2; j++) {
      // posの方向へiだけ進む
      for (int k = 0; k < i; k++) {
        now.first += dy[pos];
        now.second += dx[pos];
        m[now] = sum;
        idToPii[sum] = now;
        sum++;
      }
      pos = (pos + 1) % 4;
    }
    if (sum >= 1000100)
      break;
  }
  return 0;
  while (cin >> M >> N && (N | M)) {
    for (int i = 0; i <= M; i++)
      dp[i].first = dp[i].second = -1;
    pii p = dfs(N);
    cout << p.first << " " << p.second << endl;
  }

  return 0;
}