Problem p02608 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define SpeeD ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define endl '\\n'
// pair<int, int> *p=new pair<int, int>[n];
// std::vector<pair<int,int>> vp;
// v.push_back(make_pair(x,y))
// pair<int, int> p[1000001];
// string *s=new string [n];
// int freq[e];
// freq[a[i]-'a']++; freq[a[i]]++;
using namespace std;
ll n, t, m, c1, c2, c, x, y, z, sum, mn = 999999999, avg, pos, dif, k, mx, xi,
                                     yi;
const int MXN = 1e5 + 7;
ll pre[MXN];
bool prime[MXN];

bool PE(ll x) {
  for (ll i = 2; i * i <= x; i += 1 + (i & 1))
    if (x % i == 0)
      return false;
  return true;
}
ll dist[MXN];
void sieve() {
  memset(prime, 1, sizeof prime);

  for (ll i = 2; i * i <= MXN; i += 1 + (i & 1))
    if (prime[i]) {
      for (ll j = i * i; j < MXN; j += i)
        prime[j] = false;
    }
}
bool vis[MXN], talked[MXN];

bool PR(string s) {
  int n = s.length();
  for (ll i = 0; i < n / 2; i++)
    if (s[i] != s[n - i - 1])
      return false;
  return true;
}
ll dsu[MXN];
int FindRoot(int i) {
  if (dsu[i] == i)
    return i;
  return dsu[i] = FindRoot(dsu[i]);
}
void fn(int i) {
  if (i > n)
    exit(0);
  ll c = 0;
  for (int x = 1; x <= i; x++) {
    if (x * x + 2 + 2 * x > i)
      break;
    for (int y = 1; y <= i; y++) {
      if (x * x + y * y + 1 + y * x + y + x > i)
        break;
      for (int z = 1; z <= i; z++) {
        if (z * z + x * x + y * y + y * x + y * z + x * z == i)
          c++;
        if (z * z + x * x + y * y + y * x + y * z + x * z > i)
          break;
      }
    }
  }
  cout << c << endl;

  i++;
  fn(i);
}
int main(void) {
  SpeeD;

  cin >> n;
  fn(1);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define SpeeD ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define endl '\\n'
// pair<int, int> *p=new pair<int, int>[n];
// std::vector<pair<int,int>> vp;
// v.push_back(make_pair(x,y))
// pair<int, int> p[1000001];
// string *s=new string [n];
// int freq[e];
// freq[a[i]-'a']++; freq[a[i]]++;
using namespace std;
ll n, t, m, c1, c2, c, x, y, z, sum, mn = 999999999, avg, pos, dif, k, mx, xi,
                                     yi;
const int MXN = 1e5 + 7;
ll pre[MXN];
bool prime[MXN];

bool PE(ll x) {
  for (ll i = 2; i * i <= x; i += 1 + (i & 1))
    if (x % i == 0)
      return false;
  return true;
}
ll dist[MXN];
void sieve() {
  memset(prime, 1, sizeof prime);

  for (ll i = 2; i * i <= MXN; i += 1 + (i & 1))
    if (prime[i]) {
      for (ll j = i * i; j < MXN; j += i)
        prime[j] = false;
    }
}
bool vis[MXN], talked[MXN];

bool PR(string s) {
  int n = s.length();
  for (ll i = 0; i < n / 2; i++)
    if (s[i] != s[n - i - 1])
      return false;
  return true;
}
ll dsu[MXN];
int FindRoot(int i) {
  if (dsu[i] == i)
    return i;
  return dsu[i] = FindRoot(dsu[i]);
}
ll fn(int i) {
  ll c = 0;
  for (int x = 1; x <= i; x++)
    for (int y = 1; y <= i; y++)
      for (int z = 1; z <= i; z++) {
        if (z * z + x * x + y * y + y * x + y * z + x * z == i)
          c++;
        if (z * z + x * x + y * y + y * x + y * z + x * z > i)
          break;
      }
  return c;
}
int main(void) {
  SpeeD;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cout << fn(i) << endl;
  }
}