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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

vector<int> sq;
const int MAX = 1 << 15;
int c[MAX];
void recur(int num, int after, int sum) {
  if (sum >= MAX)
    return;
  else if (sum) {
    ++c[sum];
    if (num == 0)
      return;
  }

  for (int i = after; i < sq.size(); ++i)
    recur(num - 1, i, sum + sq[i]);
}
int main() {
  for (int i = 1; i * i < MAX; ++i)
    sq.push_back(i * i);

  recur(4, 0, 0);

  int n;
  while (scanf("%d", &n), n)
    printf("%d\\n", c[n]);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

vector<int> sq;
int dp[4][190][1 << 15];
int solve(int num, int after, int n) {
  if (~dp[num][after][n])
    return dp[num][after][n];

  if (num == 0)
    return binary_search(sq.begin() + after, sq.end(), n) ? 1 : 0;

  int res = 0;
  for (int i = after; i < sq.size() && sq[i] < n; ++i)
    res += solve(num - 1, i, n - sq[i]);

  return dp[num][after][n] = res;
}
int main() {
  CLEAR(dp, -1);
  for (int i = 1; i * i <= 1 << 15; ++i)
    sq.push_back(i * i);

  int n;
  while (scanf("%d", &n), n) {
    int res = 0;
    for (int i = 0; i < 4; ++i)
      res += solve(i, 0, n);
    printf("%d\\n", res);
  }
}