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

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define printV(v)                                                              \\
  for (int i = 0; i < (int)v.size(); i++) {                                    \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define printRev(v)                                                            \\
  for (int i = (int)v.size() - 1; i >= 0; i--) {                               \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define sortV(v) sort(v.begin(), v.end())
#define revV(v) reverse(v.begin(), v.end())
#define FOR(i, from, n) for (int i = from; i < n; i++)
#define FOL(i, from, n) for (int i = from; i >= n; i--)

ll a, b, c, d, e, f, g, n, m, k, t, x, h, w, p, q, y;
string str = "";
ll mod = 1e9 + 7;

ll pow(ll base, ll q, ll mod) {
  ll answer = 1;
  while (q > 0) {
    if (q & 1LL)
      answer = (answer * base) % mod;

    q = q >> 1LL;
    base = (base * base) % mod;
  }
  return answer % mod;
}

void solve() {
  cin >> n;
  vector<int> squares;
  for (int i = 1; i <= sqrt(2 * n); i++) {
    squares.pb(i * i);
  }

  int sz = squares.size();
  for (int i = 1; i <= n; i++) {
    int cnt = 0;

    for (int i1 = 0; i1 < sz; i1++) {
      int val1 = squares[i1];
      if (val1 >= 2 * i)
        break;
      for (int i2 = i1; i2 < sz; i2++) {
        int val2 = squares[i2];
        if ((val2 + val1) >= 2 * i)
          break;

        int val3 = 1;
        int sum = val1 + val2;
        sum = 2 * i - sum;
        int t = sqrt(sum);
        if ((t * t) == sum)
          val3 = sum;
        else
          continue;
        if (val3 < val1 || val3 < val2)
          continue;
        // if(i == 20) cout << val1 << " " << val2 << " " << val3 << endl;
        int f1 = sqrt(val1);
        int f2 = sqrt(val2);
        int f3 = sqrt(val3);
        if ((f1 + f2 + f3) % 2)
          continue;
        sum = (f1 + f2 + f3) / 2;
        int x, y, z;

        x = sum - f3;
        y = sum - f2;
        z = sum - f1;
        // if(i == 20) cout << x << " " << y << " " << z << endl;
        if (x > 0 && y > 0 && z > 0 && x < i && z < i && y < i) {
          if (x == y && y == z)
            cnt++;
          else if (x == y || x == z || y == z)
            cnt += 3;
          else
            cnt += 6;
        }
      }
    }
    printf("%d\\n", cnt);
  }
}

int main() {
  ios_base::sync_with_stdio(false);

  int cases = 1;
  // cin >> cases;

  while (cases--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define printV(v)                                                              \\
  for (int i = 0; i < (int)v.size(); i++) {                                    \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define printRev(v)                                                            \\
  for (int i = (int)v.size() - 1; i >= 0; i--) {                               \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define sortV(v) sort(v.begin(), v.end())
#define revV(v) reverse(v.begin(), v.end())
#define FOR(i, from, n) for (int i = from; i < n; i++)
#define FOL(i, from, n) for (int i = from; i >= n; i--)

ll a, b, c, d, e, f, g, n, m, k, t, x, h, w, p, q, y;
string str = "";
ll mod = 1e9 + 7;

ll pow(ll base, ll q, ll mod) {
  ll answer = 1;
  while (q > 0) {
    if (q & 1LL)
      answer = (answer * base) % mod;

    q = q >> 1LL;
    base = (base * base) % mod;
  }
  return answer % mod;
}

void solve() {
  cin >> n;
  vector<int> squares;
  for (int i = 1; i <= sqrt(2 * n); i++) {
    squares.pb(i * i);
  }

  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    int sz = squares.size();
    for (int i1 = 0; i1 < sz; i1++) {
      int val1 = squares[i1];
      for (int i2 = i1; i2 < sz; i2++) {
        int val2 = squares[i2];
        for (int i3 = i2; i3 < sz; i3++) {
          int val3 = squares[i3];
          if (val1 + val2 + val3 == 2 * i) {
            int f1 = sqrt(val1);
            int f2 = sqrt(val2);
            int f3 = sqrt(val3);
            int sum = (f1 + f2 + f3) / 2;
            int x, y, z;

            if ((f1 + f2 + f3) % 2)
              continue;
            x = sum - f3;
            y = sum - f2;
            z = sum - f1;
            if (x > 0 && y > 0 && z > 0 && x < i && z < i && y < i) {
              if (x == y && y == z)
                cnt++;
              else if (x == y || x == z || y == z)
                cnt += 3;
              else
                cnt += 6;
            }
          }
        }
      }
    }
    cout << cnt << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);

  int cases = 1;
  // cin >> cases;

  while (cases--)
    solve();
  return 0;
}