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

#include <iostream>
#include <math.h>

using namespace std;
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define ll long long int
#define REPI(a, b, i, inc) for (ll i = a; i < b; i = i + inc)
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define F first
#define S second
#define pb push_back
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define testi                                                                  \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  REP(tc, 1, t + 1)
#define vec vector
#define veci vector<ll>
#define pi pair<ll, ll>
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uset unordered_set
#define umap unordered_map

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

int solve(int n) {
  int c = 0;
  REP(i, 1, sqrt(n)) {
    if (i * i + 5 > n)
      break;
    REP(j, 1, sqrt(n)) {
      if (i * i + j * j + i * j + j + i + 1 > n)
        break;
      REP(k, 1, sqrt(n)) {
        if (i * i + j * j + k * k + k * j + i * j + i * k == n)
          c++;
        if (i * i + j * j + k * k + k * j + i * j + i * k > n)
          break;
      }
    }
  }

  return c;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  REP(i, 1, n + 1) { cout << solve(i) << endl; }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>

using namespace std;
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define ll long long int
#define REPI(a, b, i, inc) for (ll i = a; i < b; i = i + inc)
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define F first
#define S second
#define pb push_back
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define testi                                                                  \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  REP(tc, 1, t + 1)
#define vec vector
#define veci vector<ll>
#define pi pair<ll, ll>
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uset unordered_set
#define umap unordered_map

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

int solve(int n) {
  int c = 0;
  REP(i, 1, sqrt(n))
  REP(j, 1, sqrt(n))
  REP(k, 1, sqrt(n)) {
    if (i * i + j * j + k * k + k * j + i * j + i * k == n)
      c++;
    if (i * i + j * j + k * k + k * j + i * j + i * k > n)
      break;
  }

  return c;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  REP(i, 1, n + 1) { cout << solve(i) << endl; }

  return 0;
}