Problem p00820 (C++)

Buggy submission (status: Memory Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \
  int x;                                                                       \
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};
short memo[1 << 15][200][5];
int x;
int dp(int a, int n, int now) {
  if (now <= 4 && a == x)
    return 1;
  if (now >= 4 || x - a < n * n || a > x)
    return 0;
  if (memo[a][n][now] != -1)
    return memo[a][n][now];
  int ans = 0;
  for (int i = 0; i <= 4; i++) {
    ans += dp(a + n * n * i, n + 1, now + i);
  }
  return memo[a][n][now] = ans;
}

signed main() {

  while (cin >> x && x) {
    memset(memo, -1, sizeof memo);
    cout << dp(0, 1, 0) << endl;
  }
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// # define M_PI 3.141592
#define INF ((int)(1 << 30))
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \
  int x;                                                                       \
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};
short memo[1 << 15][182][5];
int x;
int dp(int a, int n, int now) {
  if (now <= 4 && a == x)
    return 1;
  if (now >= 4 || x - a < n * n || a > x)
    return 0;
  if (memo[a][n][now] != -1)
    return memo[a][n][now];
  int ans = 0;
  for (int i = 0; i <= 4; i++) {
    ans += dp(a + n * n * i, n + 1, now + i);
  }
  return memo[a][n][now] = ans;
}

signed main() {

  while (cin >> x && x) {
    memset(memo, -1, sizeof memo);
    cout << dp(0, 1, 0) << endl;
  }
}