alexjercan/bugnet huggingface dataset:
Problem p00820 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

using namespace std;
int memo[34000][5];

int main() {

  memo[0][0] = 1;
  for (int i = 1; i * i <= 34000; i++) {
    for (int j = i * i; j <= 34000; j++) {
      for (int k = 1; k <= 4; k++) {
        memo[j][k] += memo[j - i * i][k - 1];
      }
    }
  }
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;

    int r = 0;
    for (int j = 1; j <= 4; j++) {
      r += memo[n][j];
    }

    cout << r << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>

using namespace std;
int n, r;
int memo[200][5][33000];

int rec(int i, int j, int k) {

  if (j > 4) {
    return 0;
  } else if (k == n) {
    return 1;
  } else {
    if (memo[i][j][k])
      return memo[i][j][k];
    int ret = 0;
    for (int l = i; k + l * l <= n; l++) {
      ret += rec(l, j + 1, k + l * l);
    }
    return memo[i][j][k] = ret;
  }
}

int main() {

  while (1) {
    cin >> n;
    if (!n)
      break;

    for (int i = 0; i < 200; i++) {
      for (int j = 0; j < 5; j++) {
        for (int k = 0; k < 33000; k++) {
          memo[i][j][k] = 0;
        }
      }
    }
    cout << rec(1, 0, 0) << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for (ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n)&1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

short memo[1 << 15][200][5];

short calc(int n, int last, int c) {
  if (n == 0)
    return 1;
  if (c == 4)
    return 0;
  if (memo[n][last][c] != -1)
    return memo[n][last][c];

  short &res = memo[n][last][c] = 0;
  for (int i = last; i * i <= n; i++) {
    res += calc(n - i * i, i, c + 1);
  }
  return res;
}

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

  int N;
  while (cin >> N && N) {
    cout << calc(N, 1, 0) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for (ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n)&1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

int memo[1 << 15][200][5];

int calc(int n, int last, int c) {
  if (n == 0)
    return 1;
  if (c == 4)
    return 0;
  if (memo[n][last][c] != -1)
    return memo[n][last][c];

  int &res = memo[n][last][c] = 0;
  for (int i = last; i * i <= n; i++) {
    res += calc(n - i * i, i, c + 1);
  }
  return res;
}

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

  int N;
  while (cin >> N && N) {
    cout << calc(N, 1, 0) << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 60) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
ll dp[2][(1 << 15) + 1][10];
ll h = 0;

int main() {
  dp[0][0][0] = 1;
  for (ll i = 1; i * i <= (1 << 15); i++) {
    h = i % 2;
    rep(j, (1 << 15) + 1) rep(k, 5) dp[h][j][k] = 0;
    rep(j, (1 << 15) + 1) {
      int nxt = j + i * i;
      rep(k, 5) {
        dp[i % 2][j][k] += dp[(i - 1) % 2][j][k];
        if (k != 4 && nxt < ((1 << 15) + 1)) {
          dp[i % 2][nxt][k + 1] += dp[i % 2][j][k];
        }
      }
    }
  }
  while (cin >> n && n) {
    ll ans = 0;
    rep(i, 5) { ans += dp[h][n][i]; }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 60) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n;
ll dp[200][(1 << 15) + 1][10];
ll h = 0;

int main() {
  dp[0][0][0] = 1;
  for (ll i = 1; i * i <= (1 << 15); i++) {
    h = max(h, i);
    rep(j, (1 << 15) + 1) {
      int nxt = j + i * i;
      rep(k, 5) {
        dp[i][j][k] += dp[i - 1][j][k];
        if (k != 4 && nxt < ((1 << 15) + 1))
          dp[i][nxt][k + 1] += dp[i][j][k];
      }
    }
  }
  // rep(j, 26) {
  //	cout << j << " ";
  // }
  // cout << endl;
  // rep(k, 5) {
  //	rep(i, 10) {
  //		rep(j, 26) {
  //			cout << dp[i][j][k] << " ";
  //		}
  //		cout << endl;
  //	}
  //	cout << endl;
  // }
  while (cin >> n && n) {
    ll ans = 0;
    rep(i, 5) { ans += dp[h][n][i]; }
    cout << ans << endl;
  }
}
Problem p00820 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int MAX_N = 1 << 15;

vector<int> sq;

void make() {
  for (int i = 0; i * i <= MAX_N; ++i) {
    sq.push_back(i * i);
  }
}

int solve(int n) {
  int res = 0;
  for (int i = 0; i < sq.size(); ++i) {
    for (int j = i; j < sq.size(); ++j) {
      for (int k = j; k < sq.size(); ++k) {
        int t = n - (sq[i] + sq[j] + sq[k]);
        if (t < 0)
          break;
        vector<int>::iterator it = lower_bound(sq.begin(), sq.end(), t);
        if (it == sq.end())
          continue;
        if (*it != t)
          continue;
        if (*it >= sq[k])
          ++res;
      }
    }
  }
  return res;
}

int main() {
  make();
  int n;
  while ((cin >> n) && n)
    cout << solve(n) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int MAX_N = 1 << 15;

vector<int> v;
void make() {
  vector<int> sq;
  for (int i = 0; i * i <= MAX_N; ++i) {
    sq.push_back(i * i);
  }
  for (int i = 0; i < sq.size(); ++i) {
    for (int j = i; j < sq.size() && sq[i] + sq[j] <= MAX_N; ++j) {
      for (int k = j; k < sq.size() && sq[i] + sq[j] + sq[k] <= MAX_N; ++k) {
        for (int l = k; l < sq.size() && sq[i] + sq[j] + sq[k] + sq[l] <= MAX_N;
             ++l) {
          v.push_back(sq[i] + sq[j] + sq[k] + sq[l]);
        }
      }
    }
  }
  sort(v.begin(), v.end());
}

int solve(int n) {
  return distance(lower_bound(v.begin(), v.end(), n),
                  upper_bound(v.begin(), v.end(), n));
}

int main() {
  make();
  int n;
  while ((cin >> n) && n)
    cout << solve(n) << endl;
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>
#define MAX 32768
using namespace std;

vector<int> sq;
bool isSqNum[MAX + 1];
void makeTable() {
  int tmp;
  for (int i = 0; i <= MAX; i++) {
    isSqNum[i] = false;
  }
  sq.clear();
  for (int i = 1; (tmp = (int)pow((double)i, 2.0)) <= MAX; i++) {
    sq.push_back(tmp);
    isSqNum[tmp] = true;
  }
}

int n;
int ans;

void rec(int cnt, int sum, int level) {
  if (cnt >= 4)
    return;
  if (sum > n)
    return;
  if (n - sum >= sq[level] && isSqNum[n - sum]) {
    ans++;
  }
  for (int i = level; i < sq.size() && sum + sq[i] <= n; i++) {
    rec(cnt + 1, sum + sq[i], i);
  }
}

int main() {
  makeTable();
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = 0;
    rec(0, 0, 0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
#define MAX 32768
using namespace std;

vector<int> sq;
bool isSqNum[MAX + 1];
void makeTable() {
  int tmp;
  for (int i = 0; i <= MAX; i++) {
    isSqNum[i] = false;
  }
  sq.clear();
  for (int i = 1; (tmp = (int)pow((double)i, 2.0)) <= MAX; i++) {
    sq.push_back(tmp);
    isSqNum[tmp] = true;
  }
}

int n;
int ans;

void rec(int cnt, int sum, int level) {
  if (cnt >= 4)
    return;
  if (sum > n)
    return;
  if (n - sum >= sq[level] && isSqNum[n - sum]) {
    ans++;
  }
  for (int i = level; i < sq.size(); i++) {
    rec(cnt + 1, sum + sq[i], i);
  }
}

int main() {
  makeTable();
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ans = 0;
    rec(0, 0, 0);
    cout << ans << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

const int MAX = 32770; // 1<<15
const int SQRT = 182;

int dp[MAX][5][SQRT];

int main() {

  dp[0][0][1] = 1;
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = 1; k < SQRT; k++)
        if (dp[i][j][k] > 0) {
          for (int l = k; l < SQRT; l++) { // l*lを加算
            if (i + l * l >= MAX || j + 1 >= 5)
              break;
            dp[i + l * l][j + 1][l] += dp[i][j][k];
          }
        }
    }
  }

  int n;
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 1; i < 5; i++) {
      for (int j = 1; j < SQRT; j++)
        ans += dp[n][i][j];
    }
    cout << ans << endl;
  }
}


Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

const int MAX = 33000;
const int SQRT = 200;

int dp[MAX][5][SQRT];

int main() {

  dp[0][0][1] = 1;
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < 5; j++) {
      for (int k = 1; k < SQRT; k++)
        if (dp[i][j][k] > 0) {
          for (int l = k; l < SQRT; l++) { // l*lを加算
            if (i + l * l >= MAX || j + 1 >= 5)
              break;
            dp[i + l * l][j + 1][l] += dp[i][j][k];
          }
        }
    }
  }

  int n;
  while (cin >> n, n) {
    int ans = 0;
    for (int i = 1; i < 5; i++) {
      for (int j = 1; j < SQRT; j++)
        ans += dp[n][i][j];
    }
    cout << ans << endl;
  }
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

int main() {
  int a[190];
  int n;
  rep(i, 190) a[i] = i * i;

  while (cin >> n, n) {
    int cnt = 0;
    range(i, 1, 190) {
      if (a[i] == n) {
        cnt++;
        break;
      }
      range(j, i, 190) {
        if (a[i] + a[j] == n) {
          cnt++;
          break;
        }
        range(k, j, 190) {
          if (a[i] + a[j] + a[k] == n) {
            cnt++;
            break;
          }
          range(l, k, 190) {
            if (a[i] + a[j] + a[k] + a[l] == n) {
              cnt++;
              break;
            }
          }
        }
      }
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

int main() {
  int a[200];
  int n;
  rep(i, 200) a[i] = i * i;

  while (cin >> n, n) {
    int cnt = 0;
    range(i, 1, 200) {
      if (a[i] == n) {
        cnt++;
        break;
      }
      range(j, i, 200) {
        if (a[i] + a[j] == n) {
          cnt++;
          break;
        }
        range(k, j, 200) {
          if (a[i] + a[j] + a[k] == n) {
            cnt++;
            break;
          }
          range(l, k, 200) {
            if (a[i] + a[j] + a[k] + a[l] == n) {
              cnt++;
              break;
            }
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define M 32768 + 1
int main() {
  int n, num[M] = {0}, i, j, k, l;
  for (i = 1; i * i <= M; i++) {
    num[i * i]++;
    for (j = i; i * i + j * j <= M; j++) {
      num[i * i + j * j]++;
      for (k = j; i * i + j * j + k * k < M; k++) {
        num[i * i + j * j + k * k]++;
        for (l = k; i * i + j * j + k * k + l * l < M; l++) {
          num[i * i + j * j + k * k + l * l]++;
        }
      }
    }
  }
  while (cin >> n, n) {
    cout << num[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define M 32768 + 1
int main() {
  int n, num[M] = {0}, i, j, k, l;
  for (i = 1; i < M; i++) {
    num[i * i]++;
    for (i = i; j < M; i++) {
      num[i * i + j * j]++;
      for (i = j; k < M; i++) {
        num[i * i + j * j + k * k]++;
        for (l = k; l < M; l++) {
          num[i * i + j * j + k * k + l * l]++;
        }
      }
    }
  }
  while (cin >> n, n) {
    cout << num[n] << endl;
  }
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define rep(i, n) for (int i = 0; i < n; i++)
#define SORT(x) sort((x).begin(), (x).end())
#define clr(a, b) memset((a), (b), sizeof(a))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<P> Vp;
typedef priority_queue<P, vector<P>, greater<P>> PQ;

int main() {
  int n;
  while (scanf("%d", &n), n) {
    int cnt = 0;
    for (int i = 1; i < 200; i++) {
      if (i * i == n)
        cnt++;
    }
    for (int i = 1; i < 200; i++) {
      for (int j = i; j < 200; j++) {
        if (i * i + j * j == n)
          cnt++;
      }
    }
    for (int i = 1; i < 200; i++) {
      for (int j = i; j < 200; j++) {
        for (int k = j; k < 200; k++) {
          if (i * i + j * j + k * k == n)
            cnt++;
        }
      }
    }
    for (int i = 1; i < 200; i++) {
      if (i * i * 4 > n)
        break;
      for (int j = i; j < 200; j++) {
        if (i * i + j * j * 3 > n)
          break;
        for (int k = j; k < 200; k++) {
          if (i * i + j * j + k * k * 2 > n)
            break;
          for (int l = k; l < 200; l++) {
            if (i * i + j * j + k * k + l * l == n)
              cnt++;
            if (i * i + j * j + k * k + l * l > n)
              break;
          }
        }
      }
    }
    printf("%d\\n", cnt);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 100000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define rep(i, n) for (int i = 0; i < n; i++)
#define SORT(x) sort((x).begin(), (x).end())
#define clr(a, b) memset((a), (b), sizeof(a))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<P> Vp;
typedef priority_queue<P, vector<P>, greater<P>> PQ;

int main() {
  int n;
  while (scanf("%d", &n), n) {
    int cnt = 0;
    for (int i = 1; i < 200; i++) {
      for (int j = i; j < 200; j++) {
        for (int k = j; k < 200; k++) {
          for (int l = k; l < 200; l++) {
            if (i * i + j * j + k * k + l * l == n)
              cnt++;
          }
        }
      }
    }
    printf("%d\\n", cnt);
  }
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int dfs(int n, int sum, int k, int sn) {
  int ret = 0;
  if (k < 0) {
    return 0;
  } else {
    if (sn == sum) {
      return 1;
    }
  }
  if (sn > sum || n == 0) {
    return 0;
  }
  if (sn + (k * pow(n, 2)) < sum) {
    return 0;
  }

  ret += dfs(n, sum, k - 1, sn + pow(n, 2));
  ret += dfs(n - 1, sum, k, sn);

  return ret;
}

int main(void) {
  int n;
  cin >> n;

  int lim, ret;
  bool find = false;
  while (n != 0) {
    find = false;
    for (int i = 1; !(find); i++) {
      if (!(pow(i, 2) < n)) {
        lim = i;
        find = true;
      }
    }
    ret = dfs(lim, n, 4, 0);
    cout << ret << endl;
    cin >> n;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int dfs(int n, int sum, int k, int sn) {
  int ret = 0;
  if (k < 0) {
    return 0;
  } else {
    if (sn == sum) {
      return 1;
    }
  }
  if (sn > sum || n == 0) {
    return 0;
  }

  ret += dfs(n, sum, k - 1, sn + pow(n, 2));
  ret += dfs(n - 1, sum, k, sn);

  return ret;
}

int main(void) {
  int n;
  cin >> n;

  int lim, ret;
  bool find = false;
  while (n != 0) {
    find = false;
    for (int i = 1; !(find); i++) {
      if (!(pow(i, 2) < n)) {
        lim = i;
        find = true;
      }
    }
    ret = dfs(lim, n, 4, 0);
    cout << ret << endl;
    cin >> n;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int dp[(1 << 15) + 1][201][4];
int dfs(int n, int last, int idx) {
  if (n == 0)
    return 1;
  if (dp[n][last][idx] != -1)
    return dp[n][last][idx];
  if (idx == 4)
    return 0;
  int sum = 0;
  for (int i = last; i <= 200; i++) {
    if (n - i * i >= 0)
      sum += dfs(n - i * i, i, idx + 1);
  }
  return dp[n][last][idx] = sum;
}

int main() {
  int n;
  while (cin >> n && n != 0) {
    // memset(dp,-1,sizeof(dp));
    // int cnt=dfs(n,1,0);
    int cnt = 0;
    for (int i = 0; i * i <= n; i++) {
      for (int j = i; j * j + i * i <= n; j++) {
        for (int k = j; k * k + j * j + i * i <= n; k++) {
          for (int l = k; l * l + j * j + k * k + i * i <= n; l++) {
            int sum = i * i + j * j + k * k + l * l;
            if (sum == n)
              cnt++;
          }
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int dp[(1 << 15) + 1][201][4];
int dfs(int n, int last, int idx) {
  if (n == 0)
    return 1;
  if (dp[n][last][idx] != -1)
    return dp[n][last][idx];
  if (idx == 4)
    return 0;
  int sum = 0;
  for (int i = last; i <= 200; i++) {
    if (n - i * i >= 0)
      sum += dfs(n - i * i, i, idx + 1);
  }
  return dp[n][last][idx] = sum;
}

int main() {

  int n;
  while (cin >> n && n != 0) {
    memset(dp, -1, sizeof(dp));
    int cnt = dfs(n, 1, 0);
    // for(int i=0;i<=200;i++){
    //     for(int j=i;j<=200;j++){
    //         for(int k=j;k<=200;k++){
    //             for(int l=k;l<=200;l++){
    //                 int sum=i*i+j*j+k*k+l*l;
    //                 if(sum==n)
    //                     cnt++;
    //             }
    //         }
    //     }
    // }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  while (1) {
    int n;
    scanf(" %d", &n);
    if (n == 0)
      break;

    int ans = 0;

    for (int a = 0; a * a <= n; ++a) {
      int p = a * a;
      for (int b = a; p + b * b <= n; ++b) {
        int q = b * b;
        for (int c = b; p + q + c * c <= n; ++c) {
          int r = c * c;
          int s = n - p - q - r;

          double sq = sqrt(s);
          if (sq >= c && sq - (int)sq == 0) {
            // printf("%d,%d,%d,%f\\n", a, b, c, sq);
            ans++;
          }
        }
      }
    }

    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n;
int dp[5][32769][182];
// 2^15=32768, 182^2=33124だから，182以上の数は候補にならない

int rec(int a, int b, int c) { // a個目，残りb，前に足した数c
  if (dp[a][b][c] >= 0)
    return dp[a][b][c];

  int ret = 0;

  if (a == 4) {
    if (b == 0)
      ret = 1;
  } else if (b == 0)
    ret = 1;
  else {
    for (int i = c; i * i <= n; ++i)
      ret += rec(a + 1, b - i * i, i);
  }

  // if(ret>0) printf(" %d, %d, %d\\n", a, b,c);

  return dp[a][b][c] = ret;
}

int main() {
  while (1) {
    scanf(" %d", &n);
    if (n == 0)
      break;

    memset(dp, -1, sizeof(dp));
    printf("%d\\n", rec(0, n, 1));
  }
}