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

#include <stdio.h>
int n, m;
int a[2000][2000];
int b[2000][2000];
int c[2000][2000];
int e[2000][2000];
int sx[1000005];
int sy[1000005];
int p[1000005];
void prime() {
  int i, j;
  p[1] = 1;
  for (i = 2; i <= 1000000; i++) {
    if (p[i] == 0) {
      for (j = 2 * i; j <= 1000000; j += i) {
        p[j] = 1;
      }
    }
  }
}
void inil() {
  int i, j;
  prime();
  int x, y, d;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  d = 0;
  x = y = 1000;
  int k = 1;
  int td;
  while (k <= 1000000) {
    a[x][y] = k;
    sx[k] = x;
    sy[k] = y;
    if (p[k] == 0)
      b[x][y] = 1;
    td = (d + 1) % 4;
    if (a[dx[td] + x][dy[td] + y] == 0)
      d = td;
    x = x + dx[d];
    y = y + dy[d];
    k++;
  }
}
void process() {
  int i, j, k;
  int x, y;
  x = sx[m];
  y = sy[m];
  for (i = 0; i < 2000; i++)
    for (j = 0; j < 2000; j++)
      c[i][j] = e[i][j] = 0;
  if (p[a[x][y]] == 0) {
    c[x][y] = 1;
    e[x][y] = a[x][y];
  }
  for (i = x + 1; i < 1888; i++) {
    for (j = y - i + x; j <= y + i - x; j++) {
      if (j < 0)
        continue;
      if (j > 1888)
        continue;
      if (a[i][j] == 0 || a[i][j] > n)
        continue;
      // printf("%d %d  %d %d\\n",x,y,i,j);
      k = (p[a[i][j]] == 0);
      if (c[i][j] < c[i - 1][j - 1] + k) {
        c[i][j] = c[i - 1][j - 1] + k;
        e[i][j] = e[i - 1][j - 1];
        if (k)
          e[i][j] = a[i][j];
      } else if (c[i][j] == c[i - 1][j - 1] + k) {
        e[i][j] = e[i - 1][j - 1];
        if (k)
          e[i][j] = a[i][j];
      }
      if (c[i][j] < c[i - 1][j] + k) {
        c[i][j] = c[i - 1][j] + k;
        e[i][j] = e[i - 1][j];
        if (k)
          e[i][j] = a[i][j];
      } else if (c[i][j] == c[i - 1][j] + k) {
        e[i][j] = e[i - 1][j];
        if (k)
          e[i][j] = a[i][j];
      }
      if (c[i][j] < c[i - 1][j + 1] + k) {
        c[i][j] = c[i - 1][j + 1] + k;
        e[i][j] = e[i - 1][j + 1];
        if (k)
          e[i][j] = a[i][j];
      } else if (c[i][j] == c[i - 1][j + 1] + k) {
        e[i][j] = e[i - 1][j + 1];
        if (k)
          e[i][j] = a[i][j];
      }
    }
  }
  int max = 0;
  int mi = 0;
  for (i = n; i > 0; i--) {
    if (max < c[sx[i]][sy[i]]) {
      max = c[sx[i]][sy[i]];
      mi = e[sx[i]][sy[i]];
    } else if (max == c[sx[i]][sy[i]]) {
      if (mi < e[sx[i]][sy[i]])
        mi = e[sx[i]][sy[i]];
    }
  }
  if (max == 0)
    mi = 0;
  printf("%d %d\\n", max, mi);
}
int main() {
  inil();
  while (1) {
    scanf("%d %d", &n, &m);
    if (n + m == 0)
      break;
    process();
  }
}

Error: MLE

Fixed submission:

#include <stdio.h>
int n, m;
int a[3000][3000];
int b[3000][3000];
int c[3000][3000];
int e[3000][3000];
int sx[3000005];
int sy[3000005];
int p[3000005];
void prime() {
  int i, j;
  p[1] = 1;
  for (i = 2; i <= 3000000; i++) {
    if (p[i] == 0) {
      for (j = 2 * i; j <= 3000000; j += i) {
        p[j] = 1;
      }
    }
  }
}
void inil() {
  int i, j;
  prime();
  int x, y, d;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  d = 0;
  x = y = 1500;
  int k = 1;
  int td;
  while (k <= 2000000) {
    a[x][y] = k;
    sx[k] = x;
    sy[k] = y;
    if (p[k] == 0)
      b[x][y] = 1;
    td = (d + 1) % 4;
    if (a[dx[td] + x][dy[td] + y] == 0)
      d = td;
    x = x + dx[d];
    y = y + dy[d];
    k++;
  }
}
void process() {
  int i, j, k;
  int x, y;
  x = sx[m];
  y = sy[m];
  for (i = 0; i < 3000; i++)
    for (j = 0; j < 3000; j++)
      c[i][j] = e[i][j] = 0;
  if (p[a[x][y]] == 0)
    c[x][y] = 1;
  for (i = x + 1; i < 2888; i++) {
    for (j = y - i + x; j <= y + i - x; j++) {
      if (j < 0)
        continue;
      if (j > 2888)
        continue;
      if (a[i][j] == 0 || a[i][j] > n)
        continue;
      // printf("%d %d  %d %d\\n",x,y,i,j);
      k = (p[a[i][j]] == 0);
      if (c[i][j] < c[i - 1][j - 1] + k) {
        c[i][j] = c[i - 1][j - 1] + k;
        e[i][j] = e[i - 1][j - 1];
        if (k)
          e[i][j] = a[i][j];
      } else if (c[i][j] == c[i - 1][j - 1] + k) {
        e[i][j] = e[i - 1][j - 1];
        if (k)
          e[i][j] = a[i][j];
      }
      if (c[i][j] < c[i - 1][j] + k) {
        c[i][j] = c[i - 1][j] + k;
        e[i][j] = e[i - 1][j];
        if (k)
          e[i][j] = a[i][j];
      } else if (c[i][j] == c[i - 1][j] + k) {
        e[i][j] = e[i - 1][j];
        if (k)
          e[i][j] = a[i][j];
      }
      if (c[i][j] < c[i - 1][j + 1] + k) {
        c[i][j] = c[i - 1][j + 1] + k;
        e[i][j] = e[i - 1][j + 1];
        if (k)
          e[i][j] = a[i][j];
      } else if (c[i][j] == c[i - 1][j + 1] + k) {
        e[i][j] = e[i - 1][j + 1];
        if (k)
          e[i][j] = a[i][j];
      }
    }
  }
  int max = 0;
  int mi = 0;
  for (i = n; i > 0; i--) {
    if (max < c[sx[i]][sy[i]]) {
      max = c[sx[i]][sy[i]];
      mi = e[sx[i]][sy[i]];
    } else if (max == c[sx[i]][sy[i]]) {
      if (mi < e[sx[i]][sy[i]])
        mi = e[sx[i]][sy[i]];
    }
  }
  if (max == 0)
    mi = 0;
  printf("%d %d\\n", max, mi);
}
int main() {
  inil();
  while (1) {
    scanf("%d %d", &n, &m);
    if (n + m == 0)
      break;
    process();
  }
}