alexjercan/bugnet huggingface dataset:
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b, int c) {
  if (a < b)
    swap(a, b);
  int r = 0, ans = 0;
  while (1) {
    r = a % b;
    if (r == 0) {
      ans = b;
      break;
    } else {
      a = b;
      b = r;
    }
  }
  if (ans > c)
    swap(ans, c);
  while (1) {
    r = ans % c;
    if (r == 0) {
      return c;
    } else {
      ans = c;
      c = r;
    }
  }
}
int main() {
  int k;
  cin >> k;
  long long sum = 0;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      for (int k1 = 1; k1 <= k; k1++) {
        sum += gcd(i, j, k1);
      }
  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b, int c) {
  for (int i = 200; i >= 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0)
      return i;
  }
}
int main() {
  int k;
  cin >> k;
  long long sum = 0;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      for (int k1 = 1; k1 <= k; k1++) {
        sum += gcd(i, j, k1);
      }
  cout << sum;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a, b, c, k, i, big, gcd, flag;
  cin >> k;
  int64_t sum = 0;
  for (a = 1; a <= k; a++) {
    for (b = 1; b <= k; b++) {
      for (c = 1; c <= k; c++) {
        big = 200;
        flag = 0;
        if (big > a)
          big = a;
        if (big > b)
          big = b;
        if (big > c)
          big = c;
        for (i = big; i > 0; i--) {
          if (a % i == 0 && b % i == 0) {
            if (c % i == 0) {
              gcd = i;
              flag = 1;
            }
          }
          if (flag == 1)
            break;
        }
        sum += gcd;
      }
    }
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int a, b, c, k, i, big, gcd, flag;
  cin >> k;
  int64_t sum = 0;
  for (a = 1; a <= k; a++) {
    for (b = 1; b <= k; b++) {
      for (c = 1; c <= k; c++) {
        big = 0;
        flag = 0;
        if (big < a)
          big = a;
        if (big < b)
          big = b;
        if (big < c)
          big = c;
        for (i = big; i > 0; i--) {
          if (a % i == 0 && b % i == 0) {
            if (c % i == 0) {
              gcd = i;
              flag = 1;
            }
          }
          if (flag == 1)
            break;
        }
        sum += gcd;
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
int gcd(int x, int y, int z) {
  int m = max(x, y);
  m = max(m, z);
  for (int i = m; i >= 1; i--) {
    if (x % i == 0 && y % i == 0 && z % i == 0)
      return i;
  }
}
int gcd_(int a, int b) {
  int temp = a;
  while (a % b != 0) {
    a = b;
    b = temp % b;
    temp = a;
  }
  return b;
}
int main() {
  int k;
  cin >> k;
  long long int sum = 0;
  for (int a = 1; a <= k; a++) {
    for (int b = a + 1; b <= k; b++) {
      for (int c = b + 1; c <= k; c++) {
        sum += 6 * gcd(a, b, c);
      }
    }
  }

  for (int a = 1; a <= k; a++) {
    for (int b = a + 1; b != a && b <= k; b++) {
      sum += 6 * gcd_(a, b);
    }
  }

  for (int a = 1; a <= k; a++) {
    sum += a;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
int gcd(int x, int y, int z) {
  int m = max(x, y);
  m = max(m, z);
  for (int i = m; i >= 1; i--) {
    if (x % i == 0 && y % i == 0 && z % i == 0)
      return i;
  }
}
int gcd_(int a, int b) {
  int temp = a;
  while (a % b != 0) {
    a = b;
    b = temp % b;
    temp = a;
  }
  return b;
}
int main() {
  int k;
  cin >> k;
  long long int sum = 0;
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; b++) {
      for (int c = 1; c <= k; c++) {
        sum += gcd(a, b, c);
      }
    }
  }

  /*for (int a = 1; a <= k; a++)
  {
          for (int b = a + 1; b != a && b <= k; b++) {
                  sum += 6 * gcd_(a, b);
          }
  }

  for (int a = 1; a <= k; a++)
  {
          sum += a;
  }*/
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI 3.14159265358979323846264338327950
#define INF 1e18

int gcd(int x, int y) {
  if (x % y == 0) {
    return y;
  }
  int k = x % y;
  return gcd(y, k);
}
int main() {
  int k;
  cin >> k;
  ll ans = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int s = 1; s <= k; s++) {
        int f = gcd(i, j);
        int t = gcd(s, f);
        ans += t;
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI 3.14159265358979323846264338327950
#define INF 1e18

int gcd(int x, int y) {
  if (x % y == 0) {
    return y;
  }
  int k = x % y;
  gcd(y, k);
}
int main() {
  int k;
  cin >> k;
  ll ans = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int s = 1; s <= k; s++) {
        int f = gcd(i, j);
        int t = gcd(s, f);
        ans += t;
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

int gcd(int a, int b) {
  if (b == 1)
    return (1);
  else if (b == 0)
    return (a);
  else
    return gcd(b, a % b);
}

int main() {
  int K;
  cin >> K;
  long long ans = 0;

  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        ans += gcd(gcd(i, j), k);
      }
    }
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

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

int gcd(int a, int b) {
  if (a == 1 || b == 1)
    return (1);
  else if (a == 0)
    return (0);
  else
    return gcd(b, a % b);
}

int main() {
  int K;
  cin >> K;
  long long ans = 0;

  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      for (int k = 0; k < K; k++) {
        ans += gcd(gcd(i, j), k);
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int K;
  cin >> K;
  int A = 0;

  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        int B = min(i, j);
        B = min(B, k);
        for (int l = B; l >= 1; l--) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            A += l;
            break;
          }
        }
      }
    }
  }
  cout << A << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int K;
  cin >> K;
  int A = 0;

  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        for (int l = K; l >= 1; l--) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            A += l;
            break;
          }
        }
      }
    }
  }
  cout << A << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
inline ll max(ll a, ll b) { return a > b ? a : b; }
inline ll min(ll a, ll b) { return a > b ? b : a; }
ll my_gcd(ll a, ll b) {
  ll x = max(a, b), y = min(a, b);
  if (y == 0) {
    return x;
  } else {
    return my_gcd(y, x % y);
  }
}

int main() {
  ll K;
  cin >> K;
  ll sum = 0;
  for (int i = 1; i <= K; i++) {
    sum += i;
  }
  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      if (i == j)
        continue;
      sum += 3 * my_gcd(i, j);
    }
  }
  for (int i = 1; i <= K; i++) {
    for (int j = i + 1; j <= K; j++) {
      for (int k = j + 1; k <= K; k++) {
        sum += 6 * my_gcd(k, my_gcd(i, j));
      }
    }
  }
  cout << sum << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
inline ll max(ll a, ll b) { return a > b ? a : b; }
inline ll min(ll a, ll b) { return a > b ? b : a; }
ll my_gcd(ll a, ll b) {
  if (a == b)
    return a;
  ll x = max(a, b), y = min(a, b);
  if (x % y == 0) {
    return y;
  }
  my_gcd(y, x % y);
}

int main() {
  ll K;
  cin >> K;
  ll sum = 0;
  for (int i = 1; i <= K; i++) {
    sum += i;
  }
  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      if (i == j)
        continue;
      sum += 3 * my_gcd(i, j);
    }
  }
  for (int i = 1; i <= K; i++) {
    for (int j = i + 1; j <= K; j++) {
      for (int k = j + 1; k <= K; k++) {
        sum += 6 * my_gcd(k, my_gcd(i, j));
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

signed main() {
  int k;
  cin >> k;
  int sum = 0;
  for (int a = 1; a <= k; a++)
    for (int b = 1; b <= k; b++)
      for (int c = 1; c <= k; c++) {
        sum += gcd(gcd(a, b), c);
      }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int x = 0;
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      for (int k = 1; k < n + 1; k++) {
        int y = 1;
        for (int l = 2; l < k + 1; l++) {
          int ma = i % l;
          int mi = j % l;
          int mu = k % l;
          if (ma == 0) {
            if (mi == 0) {
              if (mu == 0) {
                y = l;
              } else {
                continue;
              }
            } else {
              continue;
            }
          } else {
            continue;
          }
        }
        x += y;
      }
    }
  }
  cout << x << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int K, sum = 0, ans = 0;
  cin >> K;

  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {

        ans = gcd(a, gcd(b, c));

        sum += ans;
      }
    }
  }
  cout << sum << endl;
}

Error: -8

Fixed submission:

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

int gcd(int a, int b) {

  for (int i = 0; true; i++) {
    b %= a % b;

    if (a % b == 0) {
      return b;
    }
  }
}

int main() {
  int K, sum = 0, ans = 0;
  cin >> K;

  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {

        int ans = gcd(a, gcd(b, c));

        sum += ans;
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int n, t, kq, s;
int gcd(int a, int b) {
  int tmp;
  while (b != 0) {
    tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        t = gcd(i, j);
        kq = gcd(k, t);
        s += kq;
      }
    }
  }
  cout << s;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, t, kq;
unsigned long long s;
int UCLN(int a, int b) {
  if (a == b)
    return a;
  if (a > b)
    UCLN(a - b, b);
  else
    UCLN(a, b - a);
}
int main() {
  // freopen("output.out","w",stdout);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        t = UCLN(i, j);
        kq = UCLN(k, t);
        // cout<<i<<" "<<j<<" "<<k<<" "<<kq<<endl;
        s += kq;
      }
    }
  }
  cout << s;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i < (n + 1); ++i)
using namespace std;
using ll = long long;
using p = pair<int, int>;
int n;

ll gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  ll k, a, b, sum(0);
  cin >> k;
  rep(i, k) {
    rep(j, k) {
      rep(l, k) {
        a = gcd(i, j);
        b = gcd(a, l);
        sum += b;
      }
    }
  }
  ll Ans;
  Ans = sum;
  cout << Ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i < (n + 1); ++i)
#define rep(j, n) for (int j = 1; j < (n + 1); ++j)
#define rep(l, n) for (int l = 1; k < (n + 1); ++l)
using namespace std;
using ll = long long;
using p = pair<int, int>;
int n;

ll gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

int main() {
  ll k, a, b, sum(0);
  cin >> k;
  rep(i, k) {
    rep(j, k) {
      rep(l, k) {
        a = gcd(i, j);
        b = gcd(a, k);
        sum += b;
      }
    }
  }
  ll Ans;
  Ans = sum;
  cout << Ans;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define mod 1000000007
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
using ll = long long;
using pii = pair<int, int>;

// 最大公約数
ll gcd(ll a, ll b) { return b ? gcd(b, a %= b) : a; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      for (ll k = 1; k <= n; k++) {
        ans += gcd(gcd(i, j), k);
      }
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define mod 1000000007
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
using ll = long long;
using pii = pair<int, int>;

// 最大公約数
ll gcd(ll a, ll b) { return b ? gcd(b, a) : a; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      for (ll k = 1; k <= n; k++) {
        ans += gcd(gcd(i, j), k);
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

int gcd(int a, int b, int c) {

  for (int i = std::min(a, std::min(b, c)); i > 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0)
      return i;
  }

  return 1;
}

int main() {

  int n;

  long long res = 0;

  std::cin >> n;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        res += gcd(i, j, k);
      }
    }
  }

  std::cout << res << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

int gcd(int a, int b, int c) {

  for (int i = std::max(a, std::max(b, c)); i > 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0)
      return i;
  }

  return 1;
}

int main() {

  int n;

  long long res = 0;

  std::cin >> n;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        res += gcd(i, j, k);
      }
    }
  }

  std::cout << res << std::endl;

  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int main() {
  long int a, b = 0;
  cin >> a;
  rep(i, a) {
    rep(o, a) {
      rep(j, a) { b += gcd(i + 1, gcd(o + 1, j + 1)); }
    }
  }
  cout << b;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
int main() {
  long int a, b = 0;
  cin >> a;
  rep(i, a++) {
    rep(o, a++) { rep(j, a++) b += gcd(i, gcd(o, j)); }
  }
  cout << b;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)

int gcd(int p, int q) { // ユークリッドの互除法
  int r;
  while (q != 0) {
    r = p % q;
    p = q;
    q = r;
  }
  return p;
}

int main() {
  int k;
  cin >> k;
  k += 1;
  int ans = 0;
  int ma;
  rep1(i, k) {
    rep1(j, k) {
      rep1(l, k) {
        ma = gcd(i, j);
        ma = gcd(ma, l);
        ans += ma;
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)

int main() {
  int k;
  cin >> k;
  k += 1;
  int ans = 0;
  rep1(i, k) {
    rep1(j, k) {
      rep1(l, k) {
        int ma = 0;
        rep1(m, k) {
          if (i % m == 0 && j % m == 0 && l % m == 0) {
            ma = m;
          }
        }
        ans += ma;
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

// Sum of gcd of Tuples (Easy)
#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int gcd(int p, int q) {
  if (p % q == 0)
    return q;
  return gcd(q, p % q);
}

int main() {
  int K;
  cin >> K;
  int sum = 0;
  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        int tmp = gcd(j, k);
        sum += gcd(i, tmp);
      }
    }
  }

  cout << sum << endl;

  return 0;
}

Error: -8

Fixed submission:

// Sum of gcd of Tuples (Easy)
#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int gcd(int p, int q) {
  if (p % q == 0)
    return q;
  return gcd(q, p % q);
}

int main() {
  int K;
  cin >> K;
  int sum = 0;
  for (int i = 1; i <= K; i++) {
    for (int j = 0; j <= K; j++) {
      for (int k = 0; k <= K; k++) {
        int tmp = gcd(j, k);
        sum += gcd(i, tmp);
      }
    }
  }

  cout << sum << endl;

  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> P;
const int INF = 100000000;

int gcd(int a, int b, int c) {

  int min_num = min(a, min(b, c));
  int base;
  for (int i = 1; min_num + 1; i++) {
    if (min_num % i == 0) {
      base = min_num / i;
    }
    if ((a % base == 0) && (b % base == 0) && (c % base == 0)) {
      return base;
    }
  }
  return 0;
}

int main() {
  int K;
  cin >> K;
  int ans = 0;
  for (int i = 1; i <= K; i++) {
    for (int j = i; j <= K; j++) {
      for (int k = j; k <= K; k++) {
        if (i == j && j == k) {
          ans += gcd(i, j, k);
        } else if (i != j && j != k && k != i) {
          ans += gcd(i, j, k) * 6;
        } else {
          ans += gcd(i, j, k) * 3;
        }
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> P;
const int INF = 100000000;

int gcd(int a, int b, int c) {

  int min_num = min(a, min(b, c));
  int base;
  for (int i = 1; min_num + 1; i++) {
    if (min_num % i == 0) {
      base = min_num / i;
    }
    if ((a % base == 0) && (b % base == 0) && (c % base == 0)) {
      return base;
    }
  }
  return 0;
}

int main() {
  int K;
  cin >> K;
  int ans = 0;
  rep(i, K) {
    rep(j, K) {
      rep(k, K) { ans += gcd(i + 1, j + 1, k + 1); }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int gcd = __gcd(i, j);
      for (int k = 1; k <= n; k++) {
        sum += __gcd(gcd, k);
      }
    }
  }
  cout << sum;
}

Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; i <= n; j++) {
      int gcd = __gcd(i, j);
      for (int k = 1; k <= n; k++) {
        sum += __gcd(gcd, k);
      }
    }
  }
  cout << sum;
}