alexjercan/bugnet huggingface dataset:
Problem p00815 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>

using namespace std;

void solve() {
  int m = 1, r;
  pair<int, int> next, now = make_pair(0, 100);
  vector<pair<int, int>> stack;
  vector<vector<int>> ans;
  vector<int> door(1, 100);
  ans.push_back(vector<int>());
  while (cin >> r, r) {
    //		cout << r;
    //		while(door[now.first] <= 0){
    //			now=stack.back();
    //			stack.pop_back();
    //		}
    if (r > 0) {
      next = make_pair(m++, r);
      ans[now.first].push_back(next.first);
      --door[now.first];
      ans.push_back(vector<int>());
      door.push_back(r - 1);
      //			if(now.first > 0){
      ans[next.first].push_back(now.first);
      //			}
      stack.push_back(now);
      now = next;
    } else {
      int tmp = stack[stack.size() + r].first;
      //			int tmp=stack.size()+r;
      ans[now.first].push_back(tmp);
      --door[now.first];
      ans[tmp].push_back(now.first);
      --door[tmp];
    }
    while (door[now.first] <= 0) {
      now = stack.back();
      stack.pop_back();
    }
    //		cout <<" now:"<<now.first<< endl;
  }
  for (auto it = ans.begin() + 1; it != ans.end(); ++it) {
    cout << (it - ans.begin());
    sort((*it).begin(), (*it).end());
    for (auto j : (*it))
      if (j > 0)
        cout << " " << j;
    cout << endl;
  }
}
int main() {
  int N;
  cin >> N;
  while (--N >= 0)
    solve();
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

void solve() {
  vector<int> doors(1, 0);
  vector<vector<int>> ans(1, vector<int>());
  int r, p = 0, d = 0;
  while (cin >> r, r) {
    if (r > 0) {
      doors.push_back(r);
      int q = p;
      p = doors.size() - 1;
      ans[q].push_back(p);
      ans.push_back(vector<int>());
      if (q > 0)
        ans[p].push_back(q);
      ++d;
    } else {
      int q = d + r;
      ans[p].push_back(q);
      ans[q].push_back(p);
      while (ans[p].size() >= doors[p]) {
        --p;
        --d;
      }
    }
    //		cout << "R:" << r << " P:" << p << endl;
  }
  for (int i = 1; i < ans.size(); ++i) {
    cout << i;
    sort(begin(ans[i]), end(ans[i]));
    for (auto j : ans[i])
      cout << " " << j;
    cout << endl;
  }
}
int main() {
  int N;
  cin >> N;
  while (--N >= 0)
    solve();
}
Problem p00815 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX_N 2000
int a[MAX_N], b[MAX_N], c[MAX_N], m, t, x;
vector<int> G[MAX_N];
void init() {
  t = x = 0;
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  memset(c, 0, sizeof(c));
  for (int i = 0; i < MAX_N; i++)
    G[i].clear();
}
void dfs(int d) {
  int v = t, p = ++x;
  b[d] = p;
  for (int i = 0; (int)G[p].size() < a[v]; i++) {
    t++;
    if (a[t] > 0) {
      G[p].push_back(x + 1);
      G[x + 1].push_back(p);
      dfs(d + 1);
    } else if (a[t] < 0) {
      G[p].push_back(b[d + a[t]]);
      G[b[d + a[t]]].push_back(p);
    } else
      break;
  }
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    init();
    for (int i = 0;; i++) {
      cin >> a[i];
      if (!a[i]) {
        m = i;
        break;
      }
    }
    dfs(0);
    for (int i = 1; i <= x; i++) {
      if (G[i].size() == 0)
        break;
      cout << i << " ";
      sort(G[i].begin(), G[i].end());
      for (int j = 0; j < (int)G[i].size(); j++)
        cout << G[i][j] << " \\n"[j == (int)G[i].size() - 1];
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int n;
  cin >> n;
  while (n--) {
    int a[200], m, t = 0, x = 0;
    for (int i = 0;; i++) {
      cin >> a[i];
      if (!a[i]) {
        m = i;
        break;
      }
    }
    vector<int> G[200], b(200, 0), c(200, 0);
    stack<int> s;
    for (int i = 0; i < m; i++) {
      s.push(++x);
      b[x] = a[i];
      c[s.size()] = x;
      while (a[i + 1] < 0 && (int)G[x].size() < b[x]) {
        // cout<<x<<" "<<s.size()<<" "<< a[i+1]<<"/"<<c[s.size()+a[i+1]]<<endl;
        G[x].push_back(c[s.size() + a[++i]]);
        G[c[s.size() + a[i]]].push_back(x);
      }
      for (int k;;) {
        k = s.top();
        if (b[k] == (int)G[k].size())
          s.pop();
        else
          break;
      }
      t = s.top();
      if (a[i + 1] > 0) {
        // cout<<t<<" "<<x+1<<endl;
        G[t].push_back(x + 1);
        G[x + 1].push_back(t);
      } else {
        while (a[i + 1] < 0 && (int)G[t].size() < b[t]) {
          // cout<<t<<" "<<s.size()<<" "<<
          // a[i+1]<<"/"<<c[s.size()+a[i+1]]<<endl;
          G[t].push_back(c[s.size() + a[++i]]);
          G[c[s.size() + a[i]]].push_back(t);
        }
      }
    }
    // for(int i=1;i<x;i++) cout<<i<<" "<<c[i]<<endl;
    for (int i = 1; i <= x; i++) {
      if (G[i].size() == 0)
        break;
      cout << i << " ";
      sort(G[i].begin(), G[i].end());
      for (int j = 0; j < (int)G[i].size(); j++)
        cout << G[i][j] << " \\n"[j == (int)G[i].size() - 1];
    }
  }
  return 0;
}
Problem p00816 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

using namespace std;

int dig(int n) {
  if (n < 10)
    return 1;
  else
    return dig(n / 10) + 1;
}

int main() {
  while (1) {
    int t, n;
    cin >> t >> n;
    if (!n)
      break;
    t = min(t, n);
    int s = dig(n);
    string ns = to_string(n);
    vector<vector<int>> dp(s + 1, vector<int>(n + 1));
    dp[0][0] = 1;
    REP(i, s + 1) {
      REP(j, i) {
        auto sub = ns.substr(j, i - j);
        int m = stoi(sub);
        REP(k, n - m + 1) {
          if (dp[i][k + m] == 0 && dp[j][k] > 0)
            dp[i][k + m] = j + 1;
          else if ((dp[i][k + m] != 0 && dp[j][k] != 0) || dp[j][k] < 0)
            dp[i][k + m] = -1;
        }
      }
    }
    bool ok = false;
    for (int u = t; u > 0; --u) {
      if (dp[s][u] > 0) {
        vector<int> res;
        int i = s;
        int j = u;
        do {
          int oldi = i;
          i = dp[i][j] - 1;
          auto sub = ns.substr(i, oldi - i);
          res.push_back(stoi(sub));
          j -= stoi(sub);
        } while (i > 0);
        reverse(begin(res), end(res));
        cout << u;
        for (int v : res)
          cout << ' ' << v;
        cout << endl;
        ok = true;
        break;
      } else if (dp[s][u] < 0) {
        cout << "rejected" << endl;
        ok = true;
        break;
      }
    }
    if (!ok)
      cout << "error" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

using namespace std;

int dig(int n) {
  if (n < 10)
    return 1;
  else
    return dig(n / 10) + 1;
}

int main() {
  while (1) {
    int t, n;
    cin >> t >> n;
    if (!n)
      break;
    int s = dig(n);
    string ns = to_string(n);
    vector<vector<int>> dp(s + 1, vector<int>(n + 1));
    dp[0][0] = 1;
    REP(i, s + 1) {
      REP(j, i) {
        auto sub = ns.substr(j, i - j);
        int m = stoi(sub);
        REP(k, n - m + 1) {
          if (dp[i][k + m] == 0 && dp[j][k] > 0)
            dp[i][k + m] = j + 1;
          else if ((dp[i][k + m] != 0 && dp[j][k] != 0) || dp[j][k] < 0)
            dp[i][k + m] = -1;
        }
      }
    }
    bool ok = false;
    for (int u = t; u > 0; --u) {
      if (dp[s][u] > 0) {
        vector<int> res;
        int i = s;
        int j = u;
        do {
          int oldi = i;
          i = dp[i][j] - 1;
          auto sub = ns.substr(i, oldi - i);
          res.push_back(stoi(sub));
          j -= stoi(sub);
        } while (i > 0);
        reverse(begin(res), end(res));
        cout << u;
        for (int v : res)
          cout << ' ' << v;
        cout << endl;
        ok = true;
        break;
      } else if (dp[s][u] < 0) {
        cout << "rejected" << endl;
        ok = true;
        break;
      }
    }
    if (!ok)
      cout << "error" << endl;
  }
  return 0;
}
Problem p00816 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;

unsigned short dp[7][1000000];
unsigned char pr[7][1000000];
int df[7][1000000];

int main() {
  while (true) {
    int n;
    string s;
    cin >> n >> s;
    if (n == 0 && s == "0") {
      break;
    }
    const int m = s.size();
    for (int i = 0; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        dp[i][j] = pr[i][j] = df[i][j] = 0;
      }
    }
    dp[0][0] = 1;
    for (int i = 0; i < m; ++i) {
      int x = 0;
      for (int j = i; j < m; ++j) {
        x = (x * 10) + s[j] - '0';
        if (x > n) {
          break;
        }
        for (int k = 0; k + x <= n; ++k) {
          if (dp[i][k] == 0) {
            continue;
          }
          dp[j + 1][k + x] += dp[i][k];
          pr[j + 1][k + x] = i;
          df[j + 1][k + x] = x;
        }
      }
    }
    int answer = -1;
    for (int i = n; answer < 0 && i >= 0; --i) {
      if (dp[m][i] != 0) {
        answer = i;
      }
    }
    if (answer < 0) {
      cout << "error" << endl;
    } else if (dp[m][answer] >= 2) {
      cout << "rejected" << endl;
    } else {
      cout << answer;
      vector<int> cut;
      int r = m, c = answer;
      do {
        const int d = df[r][c];
        cut.push_back(d);
        r = pr[r][c];
        c -= d;
      } while (r > 0);
      for (int i = cut.size() - 1; i >= 0; --i) {
        cout << " " << cut[i];
      }
      cout << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int dp[7][1000000];
int pr[7][1000000];
int df[7][1000000];

int main() {
  while (true) {
    int n;
    string s;
    cin >> n >> s;
    if (n == 0 && s == "0") {
      break;
    }
    const int m = s.size();
    for (int i = 0; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        dp[i][j] = pr[i][j] = df[i][j] = 0;
      }
    }
    dp[0][0] = 1;
    for (int i = 0; i < m; ++i) {
      int x = 0;
      for (int j = i; j < m; ++j) {
        x = (x * 10) + s[j] - '0';
        if (x > n) {
          break;
        }
        for (int k = 0; k + x <= n; ++k) {
          if (dp[i][k] == 0) {
            continue;
          }
          dp[j + 1][k + x] += dp[i][k];
          pr[j + 1][k + x] = i;
          df[j + 1][k + x] = x;
        }
      }
    }
    int answer = -1;
    for (int i = n; answer < 0 && i >= 0; --i) {
      if (dp[m][i] != 0) {
        answer = i;
      }
    }
    if (answer < 0) {
      cout << "error" << endl;
    } else if (dp[m][answer] >= 2) {
      cout << "rejected" << endl;
    } else {
      cout << answer;
      vector<int> cut;
      int r = m, c = answer;
      do {
        const int d = df[r][c];
        cut.push_back(d);
        r = pr[r][c];
        c -= d;
      } while (r > 0);
      for (int i = cut.size() - 1; i >= 0; --i) {
        cout << " " << cut[i];
      }
      cout << endl;
    }
  }
  return 0;
}
Problem p00819 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  string s, m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m >> s;
    for (int j = m.length() - 1; j >= 0; j--) {
      switch (m[j]) {
      case 'C':
        s = s.substr(1, s.length() - 1) + s[0];
        break;
      case 'J':
        s = s[s.length() - 1] + s.substr(0, s.length() - 1);
        break;
      case 'E': {
        int a = s.length() / 2, b = s.length() % 2;
        s = s.substr(a + b, a) + s.substr(a, b) + s.substr(0, a);
      } break;
      case 'A':
        reverse(s.begin(), s.end());
        break;
      case 'M':
        for (int k = 0; k < s.length(); k++)
          if (s[k] >= '0' && s[k] <= '9')
            s[k] = (s[k] - '0' + 1) % 10 + '0';
        break;
      case 'P':
        for (int k = 0; k < s.length(); k++)
          if (s[k] >= '0' && s[k] <= '9')
            s[k] = (s[k] - '0' + 9) % 10 + '0';
        break;
      }
    }
    cout << s << endl;
  }
}

Error: WA

Fixed submission:

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

int main() {
  int n;
  string s, m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m >> s;
    for (int j = m.length() - 1; j >= 0; j--) {
      switch (m[j]) {
      case 'C':
        s = s.substr(1, s.length() - 1) + s[0];
        break;
      case 'J':
        s = s[s.length() - 1] + s.substr(0, s.length() - 1);
        break;
      case 'E': {
        int a = s.length() / 2, b = s.length() % 2;
        s = s.substr(a + b, a) + (b ? s.substr(a + 1, 1) : "") + s.substr(0, a);
      } break;
      case 'A':
        // reverse(s.begin(), s.end());
        break;
      case 'M':
        for (int k = 0; k < s.length(); k++)
          if (s[k] >= '0' && s[k] <= '9')
            s[k] = (s[k] - '0' + 1) % 10 + '0';
        break;
      case 'P':
        for (int k = 0; k < s.length(); k++)
          if (s[k] >= '0' && s[k] <= '9')
            s[k] = (s[k] - '0' - 1) % 10 + '0';
        break;
      }
    }
    cout << s << endl;
  }
}
Problem p00819 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

string o, m;

void solve() {
  int len = (int)o.size();
  int sz = (int)m.size();

  for (int i = len - 1; i >= 0; i--) {
    switch (o[i]) {
    case 'J':
      m = m[sz - 1] + m.substr(0, sz - 1);
      break;
    case 'C':
      m = m.substr(1, sz - 1) + m[0];
      break;
    case 'E':
      if (sz % 2) {
        m = m.substr(sz / 2 + 1, sz / 2) + m[sz / 2] + m.substr(0, sz / 2);
      } else {
        m = m.substr(sz / 2, sz / 2) + m.substr(0, sz / 2);
      }
      break;
    case 'A':
      reverse(m.begin(), m.end());
      break;
    case 'P':
      for (int i = 0; i < sz; i++) {
        if ('1' <= m[i] && m[i] <= '9') {
          char c = m[i] - 1;
          m[i] = c;
        } else if (m[i] == '0')
          m[i] = '9';
      }
      break;
    case 'M':
      for (int i = 0; i < sz; i++) {
        if ('0' <= m[i] && m[i] <= '8') {
          char c = m[i] + 1;
          m[i] = c;
        } else if (m[i] == '9')
          m[i] = '0';
      }
      break;
    default:
      break;
    }
  }

  cout << m << endl;
}

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

  for (int i = 0; i < n; i++) {
    cin >> o;
    cin >> m;
    solve();
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

string o, m;

void solve() {
  int len = (int)o.size();
  int sz = (int)m.size();

  for (int i = len - 1; i >= 0; i--) {
    switch (o[i]) {
    case 'J':
      m = m[sz - 1] + m.substr(0, sz - 1);
      break;
    case 'C':
      m = m.substr(1, sz - 1) + m[0];
      break;
    case 'E':
      if (sz % 2) {
        m = m.substr(0, sz / 2) + m[sz / 1 + 1] + m.substr(sz / 2 + 2, sz / 2);
      } else {
        m = m.substr(0, sz / 2) + m.substr(sz / 2 + 1, sz / 2);
      }
      break;
    case 'A':
      reverse(m.begin(), m.end());
      break;
    case 'P':
      for (int i = 0; i < sz; i++) {
        if ('1' <= m[i] && m[i] <= '9') {
          char c = m[i] - 1;
          m[i] = c;
        } else if (m[i] == '0')
          m[i] = '9';
      }
      break;
    case 'M':
      for (int i = 0; i < sz; i++) {
        if ('0' <= m[i] && m[i] <= '8') {
          char c = m[i] + 1;
          m[i] = c;
        } else if (m[i] == '9')
          m[i] = '0';
      }
      break;
    default:
      break;
    }
  }

  cout << m << endl;
}

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

  for (int i = 0; i < n; i++) {
    cin >> o;
    cin >> m;
    solve();
  }

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

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

using namespace std;

#define M (2 << 15)

vector<int> sq;
void init() {
  for (int i = 0; i * i <= M; i++) {
    sq.push_back(i * i);
  }
}

bool is_sq(int n) { return binary_search(sq.begin(), sq.end(), n); }

int solve(int n) {
  int c = 0;
  int r;
  for (int i = 0; i < sq.size(); i++) {
    r = n - sq[i];
    if (r < sq[i]) {
      break;
    }
    for (int j = i; j < sq.size(); j++) {
      r = n - sq[i] - sq[j];
      if (r < sq[j]) {
        break;
      }
      for (int k = j; k < sq.size(); k++) {
        r = n - sq[i] - sq[j] - sq[k];
        if (r < sq[k]) {
          break;
        }
        // cout << "\\t" << r << endl;
        if (is_sq(r)) {
          c++;
        }
      }
    }
  }
end:
  return c;
}

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

Error: MLE

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>

using namespace std;

#define M ((2 << 15) + 1)
#define L (260)

int cache[M][L][5];
int solve(int n, int lim, int r) {
  // cout << n << ", " << lim << ", " << r << endl;
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (r == 0) {
    if (n == 0)
      return 1;
    else
      return 0;
  }
  if (cache[n][lim][r] >= 0)
    return cache[n][lim][r];
  int ret = 0;
  for (int i = 0; i <= lim; i++) {
    ret += solve(n - i * i, i, r - 1);
  }
  return cache[n][lim][r] = ret;
}

int main() {
  memset(cache, -1, sizeof(cache));
  int n;
  while (cin >> n, n) {
    cout << solve(n, sqrt(n) + 1, 4) << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n;
int dfs(int m, int d, int x) {
  int ans = 0;
  if (m == n)
    return 1;
  if (d == 4)
    return 0;
  if (x * x + m <= n)
    for (int i = x; i * i <= n; i++)
      ans += dfs(m + i * i, d + 1, i);
  return ans;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int cnt, n;
void dfs(int m, int d, int x) {
  if (m == n) {
    cnt++;
    return;
  }
  if (d == 4 || m > n)
    return;
  for (int i = x; i <= sqrt(n); i++)
    dfs(m + pow(i, 2), d + 1, i);
}
int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cnt = 0, dfs(0, 0, 1), cout << cnt << endl;
  }
  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int stoi(string x) {
  stringstream ss;
  ss << x;
  int tmp;
  ss >> tmp;
  return tmp;
}
string itos(int x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
int n;
int dfs(int h, int sum, int use) {
  // cout<<h<<" "<<sum<<" "<<use<<endl;
  if (use > 4)
    return 0;
  if (sum > n)
    return 0;
  if (sum == n)
    return 1;
  int ret = 0;
  for (int i = h; i * i <= n; i++) {
    if (sum + i * i > n || use == 4)
      break;
    ret += dfs(i, sum + i * i, use + 1);
  }
  return ret;
}
int main() {
  while (cin >> n, n != 0) {
    cout << dfs(1, 0, 0) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int stoi(string x) {
  stringstream ss;
  ss << x;
  int tmp;
  ss >> tmp;
  return tmp;
}
string itos(int x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
int n;
int dp[40003][201][4];
int dfs(int h, int sum, int use) {
  // cout<<h<<" "<<sum<<" "<<use<<endl;
  if (dp[sum][h][use] != -1)
    return dp[sum][h][use];
  if (use > 4)
    return dp[sum][h][use] = 0;
  if (sum > n)
    return dp[sum][h][use] = 0;
  if (sum == n)
    return dp[sum][h][use] = 1;
  int ret = 0;
  for (int i = h; i * i <= n; i++) {
    if (sum + i * i > n)
      break;
    ret += dfs(i, sum + i * i, use + 1);
  }
  return dp[sum][h][use] = ret;
}
int main() {
  while (cin >> n, n != 0) {
    for (int i = 0; i < 40003; i++)
      for (int j = 0; j < 210; j++)
        for (int k = 0; k < 5; k++)
          dp[i][j][k] = -1;
    cout << dfs(1, 0, 0) << endl;
  }

  return 0;
}
Problem p00820 (C++) — buggy submission, verdict: Memory 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;

int dfs(int cnt, int a, int sum) {
  // printf("%d %d %d\\n", cnt, a, sum);
  if (sum == n)
    return 1;
  if (cnt == 4)
    return 0;
  if (a * a > n)
    return 0;
  if (sum > n)
    return 0;
  int res = 0;

  for (int i = a; i * i <= n; i++) {
    if (sum + i * i > n)
      break;
    res += dfs(cnt + 1, i, sum + i * i);
  }

  return res;
}

void solve() {
  cout << dfs(0, 1, 0) << endl;
  ;
}

int main() {
  while (cin >> n) {
    if (n == 0)
      break;
    solve();
  }
  return 0;
}

Error: MLE

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;

int n;

int dp[4][1 << 8][1 << 16];

int dfs(int cnt, int a, int sum) {
  // printf("%d %d %d\\n", cnt, a, sum);
  if (sum == n)
    return 1;
  if (cnt == 4)
    return 0;
  if (a * a > n)
    return 0;
  if (sum > n)
    return 0;
  if (dp[cnt][a][sum] != -1)
    return dp[cnt][a][sum];
  int res = 0;

  for (int i = a; i * i <= n; i++) {
    res += dfs(cnt + 1, i, sum + i * i);
  }

  return dp[cnt][a][sum] = res;
}

void solve() {
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, 1, 0) << endl;
  ;
}

int main() {
  while (cin >> n) {
    if (n == 0)
      break;
    solve();
  }
  return 0;
}