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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define PI 3.141592653589
#define MOD 1000000007

using namespace std;

template <typename T> T gcd(T a, T b) { return (a % b) ? gcd(b, a % b) : b; }
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

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

  /*
  REP(i, 200){
      REP(j, 200){
          REP(k, 200){
              int tmp = (x + y) * (x + y) + (y + z) * (y + z) + (z + x) * (z +
  x); if(tmp == 2 * n) ctr++;
          }
      }
  }
  */

  for (int i = 1; i <= n; i++) {
    int ctr = 0;

    for (int x = 2; x * x <= n; x++) {
      for (int y = x; y * y <= n; y++) {
        int z = sqrt(2 * i - x * x - y * y);

        if (z * z == 2 * i - x * x - y * y && z >= y) {
          // cout << i << ":" << x << " " << y << " " << z << endl;

          set<int> st;
          st.insert(x);
          st.insert(y);
          st.insert(z);
          /*
          if(st.size() == 1) ctr++;
          else if(st.size() == 2) ctr += 3;
          else ctr += 6;
          */

          int a = (x - y + z) / 2, b = (x + y - z) / 2, c = (-x + y + z) / 2;
          if (a >= 1 && b >= 1 && c >= 1) {
            if (st.size() == 1)
              ctr++;
            else if (st.size() == 2)
              ctr += 3;
            else
              ctr += 6;
          }
        }
      }
    }
    cout << ctr << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define PI 3.141592653589
#define MOD 1000000007

using namespace std;

template <typename T> T gcd(T a, T b) { return (a % b) ? gcd(b, a % b) : b; }
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

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

  int x, y, z;

  /*
  REP(i, 200){
      REP(j, 200){
          REP(k, 200){
              int tmp = (x + y) * (x + y) + (y + z) * (y + z) + (z + x) * (z +
  x); if(tmp == 2 * n) ctr++;
          }
      }
  }
  */

  vector<int> heihou;
  int i = 2;
  while (1) {
    heihou.push_back(i * i);
    i++;
    if (i > n)
      break;
  }

  int m = heihou.size(); // higher 100

  for (int i = 1; i <= n; i++) {
    int ctr = 0;

    for (int x = 2; x <= n; x++) {
      for (int y = x; y <= n; y++) {
        int z = sqrt(2 * i - x * x - y * y);

        if (z * z == 2 * i - x * x - y * y && z >= y) {
          // cout << i << ":" << x << " " << y << " " << z << endl;

          set<int> st;
          st.insert(x);
          st.insert(y);
          st.insert(z);
          /*
          if(st.size() == 1) ctr++;
          else if(st.size() == 2) ctr += 3;
          else ctr += 6;
          */

          int a = (x - y + z) / 2, b = (x + y - z) / 2, c = (-x + y + z) / 2;
          if (a >= 1 && b >= 1 && c >= 1) {
            if (st.size() == 1)
              ctr++;
            else if (st.size() == 2)
              ctr += 3;
            else
              ctr += 6;
          }
        }
      }
    }
    cout << ctr << endl;
  }

  return 0;
}