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

// Romene

#include <bits/stdc++.h>

#include <string>

#include <sstream>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define f first
#define s second
#define all(c) c.begin(), c.end()
#define ulli unsigned long long int
#define lli long long int
#define llv vector<ll>
#define llc vector<char>
#define lls vector<string>
#define llb vector<bool>
#define mp make_pair
#define endl "\\n"
#define debg cout << "HI" << endl;
#define ins insert
#define foo(i, n) for (ll i = 0; i < n; i++)
#define foa(i, a) for (ll i = 0; i < a.size(); i++)
#define foe(i, a, b) for (ll i = a; i <= b; i++)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define INF 2147483647
#define len length
#define sp setprecision(16)
#define mod 998244353

lli powr(ll a, ll b) { /*power(a,b) by modular exponentiation*/
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 0) {
    return powr(((a % mod) * (a % mod)) % mod, (b / 2) % mod);
  } else {
    if (b == 1) {
      return a;
    } else {
      return (a * powr(((a % mod) * (a % mod) % mod), ((b - 1) / 2) % mod)) %
             mod;
    }
  }
}
ll gcd1(ll a, ll b) {
  if (a == 0)
    return b;
  else {
    return gcd1(b % a, a);
  }
}
ll lcm(ll a, ll b) { return (a * b) / gcd1(a, b); }
ll mod1(ll a) {
  if (a < 0)
    return -1 * a;
  else {
    return a;
  }
}

bool isPrime(int num) {

  for (int i = 2; i * i <= (num); i++) {
    if (num % i == 0) {
      return false;
      break;
    }
  }

  return true;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.in", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int f1;
  f1 = 1;

  for (int y2 = 0; y2 < f1; y2++) {

    ll n, count = 0;
    cin >> n;
    for (ll t = 1; t <= n; t++) {
      count = 0;
      int flag = 0;
      for (ll i = 1; i <= t; i++) {
        if (t - 3 - 2 * i - i * i < 0)
          break;
        for (ll j = 1; j <= t; j++) {
          if (t - 1 - i - j - i * j - i * i - j * j < 0)
            break;
          ll temp = i + j;

          ll c = t - i * i - j * j - i * j;
          if (temp * temp + 4 * c >= 0) {
            if ((ll)sqrt(temp * temp + 4 * c) * (ll)sqrt(temp * temp + 4 * c) ==
                temp * temp + 4 * c) {
              if ((ll)sqrt(temp * temp + 4 * c) - temp > 0 &&
                  ((ll)sqrt(temp * temp + 4 * c) - temp) % 2 == 0) {
                count++;
              }
            }
          }
        }
      }

      cout << count << endl;
    }
  }

  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}


Error: TLE

Fixed submission:

// Romene

#include <bits/stdc++.h>

#include <string>

#include <sstream>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define f first
#define s second
#define all(c) c.begin(), c.end()
#define ulli unsigned long long int
#define lli long long int
#define llv vector<ll>
#define llc vector<char>
#define lls vector<string>
#define llb vector<bool>
#define mp make_pair
#define endl "\\n"
#define debg cout << "HI" << endl;
#define ins insert
#define foo(i, n) for (ll i = 0; i < n; i++)
#define foa(i, a) for (ll i = 0; i < a.size(); i++)
#define foe(i, a, b) for (ll i = a; i <= b; i++)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define INF 2147483647
#define len length
#define sp setprecision(16)
#define mod 998244353

lli powr(ll a, ll b) { /*power(a,b) by modular exponentiation*/
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 0) {
    return powr(((a % mod) * (a % mod)) % mod, (b / 2) % mod);
  } else {
    if (b == 1) {
      return a;
    } else {
      return (a * powr(((a % mod) * (a % mod) % mod), ((b - 1) / 2) % mod)) %
             mod;
    }
  }
}
ll gcd1(ll a, ll b) {
  if (a == 0)
    return b;
  else {
    return gcd1(b % a, a);
  }
}
ll lcm(ll a, ll b) { return (a * b) / gcd1(a, b); }
ll mod1(ll a) {
  if (a < 0)
    return -1 * a;
  else {
    return a;
  }
}

bool isPrime(int num) {

  for (int i = 2; i * i <= (num); i++) {
    if (num % i == 0) {
      return false;
      break;
    }
  }

  return true;
}

int main() {
  /* #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.in", "w", stdout);
    #endif*/
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int f1;
  f1 = 1;

  for (int y2 = 0; y2 < f1; y2++) {

    ll n, count = 0;
    cin >> n;
    for (ll t = 1; t <= n; t++) {
      count = 0;
      for (ll i = 1; i <= t; i++) {
        for (ll j = 1; j <= t; j++) {
          ll temp = i + j;

          ll c = t - i * i - j * j - i * j;
          if (temp * temp + 4 * c >= 0) {
            if ((ll)sqrt(temp * temp + 4 * c) * (ll)sqrt(temp * temp + 4 * c) ==
                temp * temp + 4 * c) {
              if ((ll)sqrt(temp * temp + 4 * c) - temp > 0 &&
                  ((ll)sqrt(temp * temp + 4 * c) - temp) % 2 == 0) {

                count++;
                // cout << i<<" "<<j<<endl;
              }
            }
          } else {
            continue;
          }
        }
      }
      cout << count << endl;
    }
  }

  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}