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

#include <bits/stdc++.h>

#define irep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep2(i, a, n) for (int i = (int)(a); i <= (int)(n); i++)
#define irrep(i, n) for (int i = (int)(n - 1); i > -1; i--)
#define irrep2(i, n, a) for (int i = (int)(n); i >= (int)(a); i--)
#define fi first
#define se second

using ll = long long;
using v_int = std::vector<int>;
using v2_int = std::vector<v_int>;
using v_ll = std::vector<ll>;
using v2_ll = std::vector<v_ll>;
using v_string = std::vector<std::string>;
using v_bool = std::vector<bool>;
using v2_bool = std::vector<v_bool>;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using mii = std::map<int, int>;
using mll = std::map<ll, ll>;

const double PI = 3.1415926535897932;
const int INF = 2 * (int)1e9;
const ll LINF = (ll)1e18;
const ll MOD = 1000000007;
const int dc[4] = {1, -1, 0, 0};
const int dr[4] = {0, 0, 1, -1};
// const int dc[8] = {1, -1, 0,  0, 1, -1,  1, -1};
// const int dr[8] = {0,  0, 1, -1, 1,  1, -1, -1};

using namespace std;

template <typename T, typename U>
inline ostream &operator<<(ostream &o, pair<T, U> &p) {
  return o << "{" << p.first << "," << p.second << "}";
}

template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
  int len = (int)v.size();
  for (int i = 0; i < len; i++) {
    is >> v[i];
  }
  return is;
}

template <typename T> inline ostream &operator<<(ostream &os, vector<T> &v) {
  int len = (int)v.size();
  for (int i = 0; i < len; i++) {
    os << v[i];
    if (i != len - 1) {
      os << ",";
    }
  }
  return os;
}

template <typename T> inline void chmin(T &a, T b) { a = min(a, b); }

template <typename T> inline void chmax(T &a, T b) { a = max(a, b); }

int solve(int n) {
  int cnt = 0;
  for (int x = 1; x * x <= n; x++) {
    for (int y = 1; y * y <= n; y++) {
      int a = 1;
      int b = x + y;
      int c = x * x + y * y + x * y - n;
      int d = b * b - 4 * a * c;
      if (d < 0) {
        continue;
      }
      double d2 = sqrt(d);
      int d3 = sqrt(d);
      if (fabs(d2 * d2 - d3 * d3) < 1e-10) {
        int z = (-b + sqrt(d)) / 2;
        if (z >= 1) {
          cnt++;
          // printf("%d,%d,%d,%d\\n",x,y,z,d);
        }
      }
    }
  }
  return cnt;
}

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

  irep2(i, 1, n) { cout << solve(i) << endl; }

  return 0;
}
/*atcoder*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define irep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep2(i, a, n) for (int i = (int)(a); i <= (int)(n); i++)
#define irrep(i, n) for (int i = (int)(n - 1); i > -1; i--)
#define irrep2(i, n, a) for (int i = (int)(n); i >= (int)(a); i--)
#define fi first
#define se second

using ll = long long;
using v_int = std::vector<int>;
using v2_int = std::vector<v_int>;
using v_ll = std::vector<ll>;
using v2_ll = std::vector<v_ll>;
using v_string = std::vector<std::string>;
using v_bool = std::vector<bool>;
using v2_bool = std::vector<v_bool>;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using mii = std::map<int, int>;
using mll = std::map<ll, ll>;

const double PI = 3.1415926535897932;
const int INF = 2 * (int)1e9;
const ll LINF = (ll)1e18;
const ll MOD = 1000000007;
const int dc[4] = {1, -1, 0, 0};
const int dr[4] = {0, 0, 1, -1};
// const int dc[8] = {1, -1, 0,  0, 1, -1,  1, -1};
// const int dr[8] = {0,  0, 1, -1, 1,  1, -1, -1};

using namespace std;

template <typename T, typename U>
inline ostream &operator<<(ostream &o, pair<T, U> &p) {
  return o << "{" << p.first << "," << p.second << "}";
}

template <typename T> inline istream &operator>>(istream &is, vector<T> &v) {
  int len = (int)v.size();
  for (int i = 0; i < len; i++) {
    is >> v[i];
  }
  return is;
}

template <typename T> inline ostream &operator<<(ostream &os, vector<T> &v) {
  int len = (int)v.size();
  for (int i = 0; i < len; i++) {
    os << v[i];
    if (i != len - 1) {
      os << ",";
    }
  }
  return os;
}

template <typename T> inline void chmin(T &a, T b) { a = min(a, b); }

template <typename T> inline void chmax(T &a, T b) { a = max(a, b); }

int solve(int n) {
  int cnt = 0;
  for (int x = 1; x * x <= n; x++) {
    for (int y = 1; y * y <= n - x * x; y++) {
      for (int z = 1; z * z <= n - x * x - y * y; z++) {
        int d = x * x + y * y + z * z + x * y + y * z + x * z;
        if (d == n) {
          cnt++;
          // printf("%d,%d,%d\\n", x,y,z);
        }
      }
    }
  }
  return cnt;
}

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

  irep2(i, 1, n) { cout << solve(i) << endl; }

  return 0;
}
/*atcoder*/