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

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};

const static ll N = 1001;

short fib_table[10000000];

int fib(int n) {
  if (n == -1 || n == 0)
    return 1;
  else if (fib_table[n] != -1)
    return fib_table[n];
  else
    return (fib_table[n] = ((fib(n - 1) % N) + (fib(n - 2) % N)) % N);
}

int main() {
  memset(fib_table, -1, sizeof(fib_table));

  int V, d;
  while (~scanf("%d %d", &V, &d)) {
    int rank[N];
    int par[N];

    vector<int> fibs;
    for (int i = 1; i <= V; i++) {
      fibs.push_back(fib(i));
    }

    sort(fibs.begin(), fibs.end());
    int res = 1;
    for (int i = 1; i < fibs.size(); i++) {
      if (fibs[i] - fibs[i - 1] >= d)
        res++;
    }

    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {0, 1, 0, -1};
const int ty[] = {-1, 0, 1, 0};

const static ll N = 1001;

short fib_table[20000000];

int fib(int n) {
  if (n == -1 || n == 0)
    return 1;
  else if (fib_table[n] != -1)
    return fib_table[n];
  else
    return (fib_table[n] = ((fib(n - 1) % N) + (fib(n - 2) % N)) % N);
}

void tree_init(int n, int *par, int *rank) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rank[i] = 0;
  }
}

int tree_find(int x, int *par, int *rank) {

  if (par[x] == x) {
    return x;
  } else {
    return par[x] = tree_find(par[x], par, rank);
  }
}

void tree_unite(int x, int y, int *par, int *rank) {

  x = tree_find(x, par, rank);

  y = tree_find(y, par, rank);

  if (x == y)
    return;

  if (rank[x] < rank[y]) {

    par[x] = y;

  } else {

    par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }
}

bool same(int x, int y, int *par, int *rank) {
  return tree_find(x, par, rank) == tree_find(y, par, rank);
}

int main() {
  memset(fib_table, -1, sizeof(fib_table));

  int V, d;
  while (~scanf("%d %d", &V, &d)) {
    int rank[N];
    int par[N];
    tree_init(N, par, rank);

    vector<int> fibs;
    for (int i = 1; i <= V; i++) {
      fibs.push_back(fib(i));
      for (int j = i + 1; j <= V; j++) {
        if (abs(fib(i) - fib(j)) < d) {
          tree_unite(fib(i), fib(j), par, rank);
        }
      }
    }

    set<int> s;
    for (int i = 0; i < fibs.size(); i++) {
      s.insert(par[fibs[i]]);
    }

    cout << s.size() << endl;
  }
}