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

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;

const int MAX = 1000010;

int prime[MAX];
bool is_prime[MAX];
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

const int CENTER = 1000;
int b[CENTER * 2][CENTER * 2];
bool v[CENTER * 2][CENTER * 2];
int pi[MAX], pj[MAX];
P dp[CENTER * 2][CENTER * 2];

bool comp(P p1, P p2) {
  if (p1.first != p2.first)
    return p1.first < p2.first;
  return p1.second < p2.second;
}

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

  sieve(1000000);
  int nx = CENTER, ny = CENTER, dirx = 0, diry = 1;
  for (int i = 1; i <= 1000000; i++) {
    b[ny][nx] = i;
    int dlx = diry, dly = -dirx;
    if (b[ny + dly][nx + dlx] == 0) {
      dirx = dlx;
      diry = dly;
    }
    nx += dirx;
    ny += diry;
  }

  int n, m;
  while (cin >> m >> n, n | m) {
    for (int y = 0; y < CENTER * 2; y++) {
      for (int x = 0; x < CENTER * 2; x++) {
        dp[y][x] = make_pair(0, 0);
        v[y][x] = false;
        if (b[y][x] == n)
          v[y][x] = true;
      }
    }

    P ans = make_pair(0, 0);
    for (int y = 0; y < CENTER * 2; y++) {
      for (int x = 0; x < CENTER * 2; x++)
        if (v[y][x]) {
          if (is_prime[b[y][x]]) {
            dp[y][x] = make_pair(dp[y][x].first + 1, b[y][x]);
          }

          for (int dx = -1; dx <= 1; dx++) {
            if (b[y + 1][x + dx] != 0 && b[y + 1][x + dx] <= m) {
              v[y + 1][x + dx] = true;
              dp[y + 1][x + dx] = max(dp[y + 1][x + dx], dp[y][x]);
            }
          }
          ans = max(ans, dp[y][x]);
        }
    }
    if (ans.first) {
      cout << ans.first << " " << ans.second << endl;
    } else {
      cout << "0 0" << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;

const int MAX = 1000010;

int prime[MAX];
bool is_prime[MAX];
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

const int CENTER = 2000;
int b[CENTER * 2][CENTER * 2];
bool v[CENTER * 2][CENTER * 2];
int pi[MAX], pj[MAX];
P dp[CENTER * 2][CENTER * 2];

bool comp(P p1, P p2) {
  if (p1.first != p2.first)
    return p1.first < p2.first;
  return p1.second < p2.second;
}

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

  sieve(1000000);
  int nx = CENTER, ny = CENTER, dirx = 0, diry = 1;
  for (int i = 1; i <= 1000000; i++) {
    b[ny][nx] = i;
    int dlx = diry, dly = -dirx;
    if (b[ny + dly][nx + dlx] == 0) {
      dirx = dlx;
      diry = dly;
    }
    nx += dirx;
    ny += diry;
  }

  int n, m;
  while (cin >> m >> n, n | m) {
    for (int y = 0; y < CENTER * 2; y++) {
      for (int x = 0; x < CENTER * 2; x++) {
        dp[y][x] = make_pair(0, 0);
        v[y][x] = false;
        if (b[y][x] == n)
          v[y][x] = true;
      }
    }

    P ans = make_pair(0, 0);
    for (int y = 0; y < CENTER * 2; y++) {
      for (int x = 0; x < CENTER * 2; x++)
        if (v[y][x]) {
          if (is_prime[b[y][x]]) {
            dp[y][x] = make_pair(dp[y][x].first + 1, b[y][x]);
          }

          for (int dx = -1; dx <= 1; dx++) {
            if (b[y + 1][x + dx] != 0 && b[y + 1][x + dx] <= m) {
              v[y + 1][x + dx] = true;
              dp[y + 1][x + dx] = max(dp[y + 1][x + dx], dp[y][x]);
            }
          }
          ans = max(ans, dp[y][x]);
        }
    }
    if (ans.first) {
      cout << ans.first << " " << ans.second << endl;
    } else {
      cout << "0 0" << endl;
    }
  }
}