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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int x;
  while (cin >> x, x) {
    int ans = 0;
    FOR(i, 0, 185) {
      FOR(j, i, 185) {
        if (i * i + j * j > x)
          break;
        FOR(k, j, 185) {
          if (i * i + j * j + k * k > x)
            break;
          FOR(l, k, 185) {
            if (i * i + j * j + k * k + l * l > x)
              break;
            if (i * i + j * j + k * k + l * l == x)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int x;
  while (cin >> x, x) {
    int ans = 0;
    FOR(i, 0, 200) {
      FOR(j, i, 200) {
        FOR(k, j, 200) {
          FOR(l, k, 200) {
            if (i * i + j * j + k * k + l * l == x)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

namespace std {
template <> struct hash<std::vector<int>> {
  size_t operator()(const vector<int> &v) const {
    std::hash<int> hasher;
    size_t seed = 0;
    for (int i : v) {
      seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    return seed;
  }
};
} // namespace std

int main() {
  int n;
  unordered_set<vector<int>> used;
  while (cin >> n, n) {
    int ans = 0;
    used.clear();
    int nr = (int)sqrt(n) + 1;
    rep(i, nr) {
      int p = i * i;
      if (p > n)
        break;
      rep(j, nr) {
        int q = p + j * j;
        if (q > n)
          break;
        rep(k, nr) {
          int r = q + k * k;
          if (r > n)
            break;
          int l = sqrt(n - r);
          if (r + l * l != n)
            continue;
          vector<int> v = {i, j, k, l};
          sort(v.begin(), v.end());
          if (!used.count(v)) {
            used.insert(v);
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

namespace std {
template <> struct hash<std::vector<int>> {
  size_t operator()(const vector<int> &v) const {
    std::hash<int> hasher;
    size_t seed = 0;
    for (int i : v) {
      seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    return seed;
  }
};
} // namespace std

int main() {
  int n;
  unordered_set<vector<int>> used;
  while (cin >> n, n) {
    int ans = 0;
    used.clear();
    int nr = (int)sqrt(n) + 1;
    rep(i, nr) {
      int p = i * i;
      if (p > n)
        break;
      rep(j, nr) {
        int q = p + j * j;
        if (q > n)
          break;
        rep(k, nr) {
          int r = q + k * k;
          if (r > n)
            break;
          rep(l, nr) {
            int s = r + l * l;
            if (s > n)
              break;
            if (s == n) {
              vector<int> v = {i, j, k, l};
              sort(v.begin(), v.end());
              if (!used.count(v)) {
                used.insert(v);
                ans++;
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ll long long

vector<int> v;
int n;
short dp[4][200][33000];
int solve(int i, int j, int res) {
  if (i == 4) {
    if (res == n)
      return 1;
    else
      return 0;
  }
  if (j == v.size() || v[j] > res + n || res > n)
    return 0;
  if (dp[i][j][res] >= 0)
    return dp[i][j][res];
  return dp[i][j][res] = solve(i, j + 1, res) + solve(i + 1, j, res + v[j]);
}

int main() {
  for (int i = 0;; i++) {
    v.push_back(i * i);
    if (i * i > 33000)
      break;
  }

  while (cin >> n, n) {
    REP(i, 4) REP(j, v.size()) REP(k, n + 1) dp[i][j][k] = -1;
    cout << solve(0, 0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ll long long

vector<int> v;
int n;
short dp[4][200][33000];
int solve(int i, int j, int res) {
  if (i == 4) {
    if (res == n)
      return 1;
    else
      return 0;
  }
  if (j == v.size())
    return 0;
  if (dp[i][j][res] >= 0)
    return dp[i][j][res];
  return dp[i][j][res] = solve(i, j + 1, res) + solve(i + 1, j, res + v[j]);
}

int main() {
  for (int i = 0;; i++) {
    v.push_back(i * i);
    if (i * i > 33000)
      break;
  }

  while (cin >> n) {
    REP(i, 4) REP(j, v.size()) REP(k, n + 1) dp[i][j][k] = -1;
    cout << solve(0, 0, 0) << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

vector<long long> sq;

// int memo[1<<15][200][4];

int dfs(long long sum, int pos, int num) {

  if (sum == 0)
    return 1;
  else if (num == 4 || sum < 0)
    return 0;

  //	if(memo[sum][pos][num]!=0) return memo[sum][pos][num];

  int res = 0;
  for (int i = pos; sq[i] <= sum; i++)
    res += dfs(sum - sq[i], i, num + 1);

  //	return memo[sum][pos][num]=res;
  return res;
}

int main() {

  for (int i = 1; i * i < (1 << 15); i++)
    sq.push_back(i * i);

  long long n;
  while (cin >> n, n) {
    cout << dfs(n, 0, 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

vector<long long> sq;

// int memo[1<<15][200][4];

int dfs(long long sum, int pos, int num) {

  if (sum == 0)
    return 1;
  else if (num == 4 || sum < 0)
    return 0;

  //	if(memo[sum][pos][num]!=0) return memo[sum][pos][num];

  int res = 0;
  for (int i = pos; i < sq.size(); i++)
    res += dfs(sum - sq[i], i, num + 1);

  //	return memo[sum][pos][num]=res;
  return res;
}

int main() {

  for (int i = 1; i * i < (1 << 15); i++)
    sq.push_back(i * i);

  long long n;
  while (cin >> n, n) {
    cout << dfs(n, 0, 0) << endl;
  }

  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

const int N = (1 << 16);
int cnt[N];

int main() {
  rep(i, 190) rep(j, i + 1) rep(k, j + 1) rep(l, k + 1) {
    cnt[min(i * i + j * j + k * k + l * l, N - 1)]++;
  }

  ios::sync_with_stdio(false);

  int n;
  while (cin >> n, n) {
    cout << cnt[n] << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

const int N = (1 << 16);
int dp[N];

int main() {
  rep(i, 190) rep(j, i + 1) rep(k, j + 1) rep(l, k + 1) {
    dp[i * i + j * j + k * k + l * l]++;
  }

  ios::sync_with_stdio(false);

  int n;
  while (cin >> n, n) {
    cout << dp[n] << endl;
  }

  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> v;
int MAX = 1 << 15;
int cnt[(1 << 15) + 10], scnt[(1 << 15) + 10], tcnt[(1 << 15) + 10],
    fcnt[(1 << 15) + 10];

void sqrs() {
  int p = sqrt(MAX);
  for (int i = 1; i <= p; i++)
    v.push_back(i * i);

  memset(cnt, 0, sizeof(cnt));
  int vsize = (int)v.size();
  for (int i = 0; i < vsize; i++)
    cnt[v[i]]++;

  memset(scnt, 0, sizeof(scnt));
  for (int i = 0; i < vsize; i++) {
    for (int j = i; j < vsize; j++) {
      if (v[i] + v[j] <= MAX)
        scnt[v[i] + v[j]]++;
    }
  }

  memset(tcnt, 0, sizeof(tcnt));
  for (int i = 0; i < vsize; i++) {
    for (int j = i; j < vsize; j++) {
      for (int k = j; k < vsize; k++) {
        if (v[i] + v[j] + v[k] <= MAX)
          tcnt[v[i] + v[j] + v[k]]++;
      }
    }
  }

  memset(fcnt, 0, sizeof(fcnt));
  for (int i = 0; i < vsize; i++) {
    for (int j = i; j < vsize; j++) {
      for (int k = j; k < vsize; k++) {
        for (int l = k; l < vsize; l++) {
          if (v[i] + v[j] + v[k] + v[l] <= MAX)
            fcnt[v[i] + v[j] + v[k] + v[l]]++;
        }
      }
    }
  }
}

int main() {
  sqrs();

  while (cin >> n, n) {
    int ans = 0;
    ans += cnt[n];
    ans += scnt[n];
    ans += tcnt[n];
    ans += fcnt[n];
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n;
vector<int> v;
int MAX = 1 << 15;

void sqrs() {
  int p = sqrt(MAX);
  for (int i = 1; i <= p; i++)
    v.push_back(i * i);
}

int main() {
  sqrs();
  int ssize = (int)v.size();

  while (cin >> n, n) {
    int ans = 0;

    for (int i = 0; i < ssize; i++)
      if (v[i] == n)
        ans++;

    for (int i = 0; i < ssize; i++)
      for (int j = i; j < ssize; j++)
        if (v[i] + v[j] == n)
          ans++;

    for (int i = 0; i < ssize; i++)
      for (int j = i; j < ssize; j++)
        for (int k = j; k < ssize; k++)
          if (v[i] + v[j] + v[k] == n)
            ans++;

    for (int i = 0; i < ssize; i++)
      for (int j = i; j < ssize; j++)
        for (int k = j; k < ssize; k++)
          for (int l = k; l < ssize; l++)
            if (v[i] + v[j] + v[k] + v[l] == n)
              ans++;

    cout << ans << endl;
  }

  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int ans, sum, n, z;

void subsetsum(int x, int y) {
  for (int i = x; i <= z; i++) {
    if (y) {
      sum += i * i;
      if (sum > n) {
        sum -= i * i;
        return;
      } else if (sum == n) {
        ans++;
        sum -= i * i;
        return;
      }
      subsetsum(i, y - 1);
      sum -= i * i;
    } else {
      return;
    }
  }
}

int main() {
  while (1) {
    ans = sum = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    z = (int)sqrt(n);
    subsetsum(1, 4);
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

int ans, sum, n;

void subsetsum(int x, int y) {
  for (int i = x; i <= (int)sqrt(n); i++) {
    if (y) {
      sum += i * i;
      if (sum > n) {
        sum -= i * i;
        return;
      } else if (sum == n) {
        ans++;
        sum -= i * i;
        return;
      }
      subsetsum(i, y - 1);
      sum -= i * i;
    } else {
      return;
    }
  }
}

int main() {
  while (1) {
    ans = sum = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    subsetsum(1, 4);
    printf("%d\\n", ans);
  }
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef unsigned long long ull;
const int IINF = 1 << 28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  vi v;
  REP(i, IINF) {
    if (i * i > 1 << 15)
      break;
    v.pb(i * i);
  }
  int n;
  while (cin >> n, n) {
    int cnt = 0;
    REP(i, v.size()) {
      FOR(j, i, v.size()) {
        FOR(k, j, v.size()) {
          FOR(l, k, v.size()) {
            if (v[i] + v[j] + v[k] + v[l] > n)
              break;
            else if (v[i] + v[j] + v[k] + v[l] == n)
              cnt++;
          }
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef unsigned long long ull;
const int IINF = 1 << 28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  vi v;
  REP(i, IINF) {
    if (i * i > 1 << 15)
      break;
    v.pb(i * i);
  }
  int n;
  while (cin >> n) {
    int cnt = 0;
    REP(i, v.size()) {
      FOR(j, i, v.size()) {
        FOR(k, j, v.size()) {
          FOR(l, k, v.size()) {
            if (v[i] + v[j] + v[k] + v[l] == n) {
              cnt++;
            } else if (i + j + k + l > n)
              break;
          }
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define DEB 0
#define all(x) x.begin(), x.end()

// max:2192

unsigned short int dp[5][32770];

int main() {
  unsigned short int i, j, k;
  int n;
  for (i = 1; i * i < (1 << 15); i++) {
    dp[1][i * i] = 1;
    for (j = i * i + 1; j < (1 << 15); j++) {
      for (k = 1; k < 4; k++) {
        dp[k + 1][j] += dp[k][j - i * i];
      }
    }
  }
#if DEB
  int mx = 0;
  rep(i, 1 << 15) {
    rep(j, 5) { mx = max(mx, dp[j][i]); }
  }
  printf("max:%d\\n", mx);
#endif
  while (scanf("%d", &n), n) {
    printf("%d\\n", dp[1][n] + dp[2][n] + dp[3][n] + dp[4][n]);
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define DEB 0
#define all(x) x.begin(), x.end()

// max:2192

unsigned short int dp[5][32770];

int main() {
  unsigned short int i, j, k, n;
  for (i = 1; i * i < (1 << 15); i++) {
    dp[1][i * i] = 1;
    for (j = i * i + 1; j < (1 << 15); j++) {
      for (k = 1; k < 4; k++) {
        dp[k + 1][j] += dp[k][j - i * i];
      }
    }
  }
#if DEB
  int mx = 0;
  rep(i, 1 << 15) {
    rep(j, 5) { mx = max(mx, dp[j][i]); }
  }
  printf("max:%d\\n", mx);
#endif
  while (scanf("%d", &n), n) {
    printf("%d\\n", dp[1][n] + dp[2][n] + dp[3][n] + dp[4][n]);
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int n;
const int MAX = 32768;

int solve(int sum, int cnt, int num) {
  if (sum == n)
    return 1;
  if (sum + num * num > n || cnt == 4)
    return 0;
  int res = 0;
  res += solve(sum + num * num, cnt + 1, num);
  res += solve(sum, cnt, num + 1);
  return res;
}
int main() {

  while (cin >> n && n != 0) {
    cout << solve(0, 0, 1) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int n;
const int MAX = 32768;

int solve(int sum, int cnt, int num) {
  if (num > sum + sqrt(n) || (cnt == 4 && sum != n))
    return 0;
  if (sum == n)
    return 1;
  int res = 0;
  if (sum + pow(num, 2) > n)
    return 0;
  res += solve(sum + pow(num, 2), cnt + 1, num);
  res += solve(sum, cnt, num + 1);
  return res;
}
int main() {

  while (cin >> n && n != 0) {
    cout << solve(0, 0, 1) << endl;
  }

  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

int n;
ll dfs(ll sum, int p, int cnt) {
  if (sum == n) {
    return 1;
  }
  if (cnt == 4) {
    return 0;
  }
  ll ret = 0;
  for (ll i = p; i <= n; i++) {
    if (sum + i * i > n)
      break;
    ret += dfs(sum + i * i, i, cnt + 1);
  }

  return ret;
}
void solve() { cout << dfs(0, 1, 0) << endl; }
int main() {
  while (cin >> n) {
    if (n == 0)
      break;
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

bool ok(int n) {
  bool f = false;
  rep(i, n) if (i > 1) {
    if (n % i == 0) {
      if (n / i % i != 0)
        return false;
      f = true;
    }
  }
  return f;
}

int n;
int dfs(int sum, int p, int cnt) {
  if (cnt == 5) {
    return 0;
  }
  if (sum == n) {
    return 1;
  }
  /*
  if(cnt) {
      int d=n-sum;
      if(ok(d)) {
          int t=sqrt(d);
          printf("%d %d %d: %d %d\\n",sum,p,cnt,d,t);
          return p<=t;
      }
  }
  */
  int ret = 0;
  for (int i = p; i < n; i++) {
    if (sum + i * i > n)
      break;
    ret += dfs(sum + i * i, i, cnt + 1);
  }

  return ret;
}
void solve() { cout << dfs(0, 1, 0) << endl; }
int main() {
  while (cin >> n) {
    if (n == 0)
      break;
    solve();
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int n;
int ans;
void saiki(int kai, int sum, int i) {
  if (sum == n)
    ans++;
  else if (kai < 4 && sum < n)
    for (int j = i; sum + j * j <= n; j++)
      saiki(kai + 1, sum + j * j, j);
}

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n;
int ans;
void saiki(int kai, int sum, int i) {
  if (sum == n)
    ans++;
  else if (kai < 4 && sum < n)
    for (int j = i; j * j <= n; j++)
      saiki(kai + 1, sum + j * j, j);
}

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

#include <bits/stdc++.h>

using namespace std;

#define MAX (1 << 15)

int main() {
  int N;
  int dp[MAX + 1][5] = {};
  dp[0][0] = 1;
  for (int i = 1; i <= MAX; i++) {
    for (int j = i * i; j <= MAX; j++) {
      for (int k = 0; k < 4; k++) {
        dp[j][k + 1] += dp[j - i * i][k];
      }
    }
  }

  while (cin >> N, N) {
    int sum = 0;
    for (int i = 1; i <= 4; i++) {
      sum += dp[N][i];
    }
    cout << sum << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX (1 << 15)

int memo[300][MAX + 1][4];

int rec(int v, int rem, int p) {
  if (rem <= 0) {
    return (rem == 0);
  }

  if (memo[v][rem][p] >= 0) {
    return memo[v][rem][p];
  }

  int res = 0;
  if (p < 4) {
    for (int i = v;; i++) {
      if (rem - i * i < 0)
        break;
      res += rec(i, rem - i * i, p + 1);
    }
  }
  return (memo[v][rem][p] = res);
}

int main() {
  int N;
  memset(memo, -1, sizeof(memo));
  while (cin >> N, N) {
    cout << rec(1, N, 0) << endl;
  }
  return 0;
}