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

/***
 ** "Learn to forgive the ones who deserve it?and?the ones who don?t."
 **
 ***/

/**
 *  Author: dhruv_gheewala
 *  Problem:
 *  Date:   13.04.2020
 *  Time:   18:55:07
 **/

#if __has_include("debug.h")
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;

#define p(str) 7
#define o(str) 42
#define fundri 108
#define d(...) 1729
#endif

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const bool testcases = 0;
#define ff first
#define ss second
#define endl '\\n'
#define all(s) s.begin(), s.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define test                                                                   \\
  int T = 1;                                                                   \\
  if (testcases)                                                               \\
    cin >> T;                                                                  \\
  for (int tc = 1; tc <= T; tc++)
#define make_unique(vec)                                                       \\
  sort(all(vec));                                                              \\
  vec.erase(unique(all(vec)), vec.end())
#define sort_by(cont, expr)                                                    \\
  sort(all(cont), [&](const auto &lhs, const auto &rhs) { return expr; })

ll seed;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {
  return uniform_int_distribution<int>(l, r)(rng);
}

template <typename T> bool isPrime(T n) {
  for (T i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}
template <typename T> inline bool in_range(T a, T l, T r) {
  return (a >= l && a <= r);
}

template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T, U> &a) {
  in >> a.ff >> a.ss;
  return in;
}
template <typename T> inline istream &operator>>(istream &in, vector<T> &a) {
  for (auto &x : a)
    in >> x;
  return in;
}
template <typename TAIL> inline void inp(TAIL &T) { cin >> T; }
template <typename HEAD, typename... TAIL>
inline void inp(HEAD &H, TAIL &...T) {
  cin >> H;
  inp(T...);
}

int32_t main(int32_t argc, char **argv) {
  std::chrono::time_point<std::chrono::high_resolution_clock> start_time,
      end_time;
  start_time = std::chrono::high_resolution_clock::now();

#ifdef DHRUV_GHEEWALA
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
  freopen("DEBUG.txt", "a", stderr);
#endif

  fast;
  int k, ans = 0;
  test {
    cin >> k;
    for (int i = 1; i <= k; i++) {
      if (i == 1) {
        ans += k * k;
        continue;
      }

      for (int j = 1; j <= k; j++) {
        if (j == 1) {
          ans += k;
          continue;
        }

        int q = __gcd(i, j);
        if (q == 1) {
          ans += k;
          continue;
        }

        for (int p = 1; p <= k; p++)
          ans += __gcd(p, q);
      }
    }

    cout << ans << endl;

    fundri;
  }

  end_time = std::chrono::high_resolution_clock::now();
  ll elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(
                        end_time - start_time)
                        .count();
  o("\\n\\n");
  if (elapsed_time < 10000)
    o("Elapsed Time: " << elapsed_time << " ms\\n");
  else
    o("Elapsed Time: " << elapsed_time / 1000 << " s\\n");

  return 0;
}

Error: TLE

Fixed submission:

/***
 ** "Chaos isn't a pit. Chaos is a ladder."
 **     -- Little Finger
 ***/

/**
 *  Author: dhruv_gheewala
 *  Problem: __________
 *  Date:   13.04.2020
 *  Time:   07:05:54
 **/

#if __has_include("debug.h")
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;

#define p(str) 7
#define o(str) 42
#define fundri 108
#define d(...) 1729
#endif

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const bool testcases = 1;
#define ff first
#define ss second
#define endl '\\n'
#define all(s) s.begin(), s.end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define test                                                                   \\
  int T = 1;                                                                   \\
  if (testcases)                                                               \\
    cin >> T;                                                                  \\
  for (int tc = 1; tc <= T; tc++)
#define make_unique(vec)                                                       \\
  sort(all(vec));                                                              \\
  vec.erase(unique(all(vec)), vec.end())
#define sort_by(cont, expr)                                                    \\
  sort(all(cont), [&](const auto &lhs, const auto &rhs) { return expr; })

ll seed;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {
  return uniform_int_distribution<int>(l, r)(rng);
}

template <typename T> bool isPrime(T n) {
  for (T i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}
template <typename T> inline bool in_range(T a, T l, T r) {
  return (a >= l && a <= r);
}

template <typename T, typename U>
inline istream &operator>>(istream &in, pair<T, U> &a) {
  in >> a.ff >> a.ss;
  return in;
}
template <typename T> inline istream &operator>>(istream &in, vector<T> &a) {
  for (auto &x : a)
    in >> x;
  return in;
}
template <typename TAIL> inline void inp(TAIL &T) { cin >> T; }
template <typename HEAD, typename... TAIL>
inline void inp(HEAD &H, TAIL &...T) {
  cin >> H;
  inp(T...);
}

int32_t main(int32_t argc, char **argv) {
  std::chrono::time_point<std::chrono::high_resolution_clock> start_time,
      end_time;
  start_time = std::chrono::high_resolution_clock::now();

#ifdef DHRUV_GHEEWALA
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
  freopen("DEBUG.txt", "a", stderr);
#endif

  fast;
  int k, ans;
  test {
    cin >> k;
    ans = 0;
    for (int i = 1; i <= k; i++) {
      if (i == 1) {
        ans += k * k;
        continue;
      }

      for (int j = 1; j <= k; j++) {
        if (j == 1) {
          ans += k;
          continue;
        }

        int q = __gcd(i, j);
        for (int p = 1; p <= k; p++) {
          if (p == 1) {
            ans++;
            continue;
          }

          ans += __gcd(q, p);
        }
      }
    }

    cout << ans << endl;
    fundri;
  }

  end_time = std::chrono::high_resolution_clock::now();
  ll elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(
                        end_time - start_time)
                        .count();
  o("\\n\\n");
  if (elapsed_time < 10000)
    o("Elapsed Time: " << elapsed_time << " ms\\n");
  else
    o("Elapsed Time: " << elapsed_time / 1000 << " s\\n");

  return 0;
}