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


#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define TRAV(a, x) for (const auto &a : x)
#define ALL(x) x.begin(), x.end()
#define NEXT_PERM(x) while (next_permutation(ALL(x))) // 순열 알고리즘(오름)
#define PREV_PERM(x) while (prev_permutation(ALL(x))) // 순열 알고리즘(내림)
#define PRINT(x) cout << x
#define PRINT_LN(x) cout << x << endl;
#define DUMP(a, n) REP(_i, n) printf("%d%c", a[_i], _i + 1 == n ? '\\n' : ' ')
#define DUMP2D(a, n, m)                                                        \\
  REP(_i, n) REP(_j, m) printf("%d%c", a[_i][_j], _j + 1 == m ? '\\n' : ' ');   \\
  puts("")

typedef long long int ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<char> vc;

int gcd(int a, int b);
int lcm(int a, int b);

void solve() {
  int n;
  cin >> n;
  int result = 0;
  REP(i, n) {
    REP(j, n) {
      REP(k, n) { result += gcd(i + 1, gcd(j + 1, k + 1)); }
    }
  }
  cout << result;
}

int main() {
  solve();
  return 0;
}

int gcd(int a, int b) {
  while (b != 0) {
    int r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int lcm(int a, int b) { return a * b / gcd(a, b); }


Error: TLE

Fixed submission:


#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define TRAV(a, x) for (const auto &a : x)
#define ALL(x) x.begin(), x.end()
#define NEXT_PERM(x) while (next_permutation(ALL(x))) // 순열 알고리즘(오름)
#define PREV_PERM(x) while (prev_permutation(ALL(x))) // 순열 알고리즘(내림)
#define PRINT(x) cout << x
#define PRINT_LN(x) cout << x << endl;
#define DUMP(a, n) REP(_i, n) printf("%d%c", a[_i], _i + 1 == n ? '\\n' : ' ')
#define DUMP2D(a, n, m)                                                        \\
  REP(_i, n) REP(_j, m) printf("%d%c", a[_i][_j], _j + 1 == m ? '\\n' : ' ');   \\
  puts("")

typedef long long int ll;
typedef vector<int> vi;
typedef vector<string> vs;

int gcd(int a, int b);
int lcm(int a, int b);

int result = 0;
int t[3];

int min(int a, int b) { return (a > b) ? a : b; }

int min3(int a, int b, int c) { return min(a, b) ? min(a, c) : min(b, c); }

int gdc3(int a, int b, int c) {
  int i, min = min3(a, b, c);

  for (i = min; i > 1; i--)
    if (a % i == 0 && b % i == 0 && c % i == 0)
      return i;

  return 1;
}

void DFS(int Cnt, int max) {
  if (Cnt == 3) {
    if (t[0] == t[1] && t[1] == t[2] && t[0] == t[2])
      result += t[0];
    else
      result += gdc3(t[0], t[1], t[2]);
    return;
  }
  for (int i = 0; i < max; i++) {
    t[Cnt] = i + 1;
    DFS(Cnt + 1, max);
  }
}

void solve() {
  int n;
  cin >> n;
  DFS(0, n);
  PRINT(result);
}

int main() {
  solve();
  return 0;
}

int gcd(int a, int b) {
  while (b != 0) {
    int r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int lcm(int a, int b) { return a * b / gcd(a, b); }
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) (v).begin(), (v).end()
#define out(a) cout << (a) << endl
using namespace std;
using ll = long long;

int main() {
  int k;
  cin >> k;
  ll sum = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        sum += __gcd(__gcd(i, j), l);
      }
    }
  }
  out(sum);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) (v).begin(), (v).end()
#define out(a) cout << (a) << endl
using namespace std;
using ll = long long;

int gcd(int a, int b) {
  if (a < b)
    return gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

int main() {
  int k;
  cin >> k;
  vector<vector<int>> vec(3, vector<int>(k));
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < 3; j++) {
      vec[i][j] = i + 1;
    }
  }

  int sum = 0;

  for (int i = 0; i < k; i++) {
    for (int j = 0; j < k; j++) {
      for (int l = 0; l < k; l++) {
        sum += gcd(gcd(vec[i][0], vec[j][1]), vec[l][2]);
      }
    }
  }
  out(sum);
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define ll long long
#define kl k << 1
#define kr k << 1 | 1
using namespace std;
inline ll read() {
  ll s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
void put1() { puts("YES"); }
void put2() { puts("NO"); }
void put3() { puts("-1"); }
ll qp(ll a, ll b, ll p) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % p;
      --b;
    }
    a = (a * a) % p;
    b >>= 1;
  }
  return ans % p;
}

const int manx = 1e5 + 5;
const int mo = 998244353;

map<pair<ll, ll>, ll> gc;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  ll n = read(), ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        ll x = gcd(i, j);
        ll y = gcd(x, k);
        ans += y;
      }
    }
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define mp make_pair
#define pb push_back
#define ll long long
#define kl k << 1
#define kr k << 1 | 1
using namespace std;
inline ll read() {
  ll s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
void put1() { puts("YES"); }
void put2() { puts("NO"); }
void put3() { puts("-1"); }
ll qp(ll a, ll b, ll p) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % p;
      --b;
    }
    a = (a * a) % p;
    b >>= 1;
  }
  return ans % p;
}

const int manx = 1e5 + 5;
const int mo = 998244353;

map<pair<ll, ll>, ll> gc;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  ll n = read(), ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        ll x, y;
        if (gc[mp(i, j)])
          x = gc[mp(i, j)];
        else
          x = gc[mp(i, j)] = gcd(i, j);
        if (gc[mp(x, k)])
          y = gc[mp(x, k)];
        else
          y = gc[mp(x, k)] = gcd(x, k);
        ans += y;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

/*int g(int a,int b){
  int r=a%b;
  if(r<1)return b;
  g(b,r);
}*/
int main() {
  int k;
  cin >> k;
  int ans = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        ans += gcd(gcd(i, j), l);
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int g(int a, int b) {
  int r = a % b;
  if (r < 1)
    return b;
  g(b, r);
}
int main() {
  int k;
  cin >> k;
  int ans = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        ans += g(g(i, j), l);
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;

using namespace std;

int gcd(int a, int b) {
  if (b > a) {
    std::swap(a, b);
  }

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

int main() {

  int k;
  cin >> k;

  ll sum = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int n = 1; n <= k; n++) {
        int t = gcd(i, j);
        sum += gcd(t, n);
      }
    }
  }

  cout << sum << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef long long ll;

using namespace std;

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

int main() {

  int k;
  cin >> k;

  ll sum = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int m = 1; m <= k; m++) {
        int prm = gcd(i, j);
        sum += gcd(prm, m);
      }
    }
  }

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

#include <iostream>
#include <vector>

using namespace std;

int gcd(int x, int y) {
  if (x < y) {
    swap(x, y);
  }

  while (y > 0) {
    int r = x % y;
    x = y;
    y = r;
  }

  return x;
}

int gcd_cache(int x, int y, int z, int ***cache) {
  if (y < x) {
    swap(x, y);
  }
  if (y < z) {
    swap(y, z);
    if (y < x) {
      swap(x, y);
    }
  }

  if (cache[x][y][z] == -1) {
    int n = gcd(x, y);
    cache[x][y][z] = gcd(n, z);
  }
  return cache[x][y][z];
}

int main() {
  int n;
  cin >> n;
  int ***cache = new int **[n + 1];
  for (int i = 0; i <= n; i++) {
    cache[i] = new int *[n + 1];
    for (int j = 0; j <= n; j++) {
      cache[i][j] = new int[n + 1];
      for (int k = 0; k <= n; k++) {
        cache[i][j][k] = -1;
      }
    }
  }

  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        sum += gcd_cache(i, j, k, cache);
      }
    }
  }
  cout << sum << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int gcd(int x, int y) {
  if (x < y) {
    swap(x, y);
  }

  while (y > 0) {
    int r = x % y;
    x = y;
    y = r;
  }

  return x;
}

int gcd_cache(int x, int y, int z, int ***cache) {
  if (y < x) {
    swap(x, y);
  }
  if (y < z) {
    swap(y, z);
    if (y < x) {
      swap(x, y);
    }
  }

  if (cache[x][y][z] == -1) {
    int n = gcd(x, y);
    cache[x][y][z] = gcd(n, z);
  }
  return cache[x][y][z];
}

int main() {
  int n;
  cin >> n;
  if (n > 200) {
    return 0;
  }

  int ***cache = new int **[n + 1];
  for (int i = 0; i <= n; i++) {
    cache[i] = new int *[n];
    for (int j = 0; j <= n; j++) {
      cache[i][j] = new int[n];
      for (int k = 0; k <= n; k++) {
        cache[i][j][k] = -1;
      }
    }
  }

  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        sum += gcd_cache(i, j, k, cache);
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int gcd(int x, int y) {
  if (x < y) {
    swap(x, y);
  }
  if (x % y == 0) {
    return y;
  } else {
    return gcd(y, x % y);
  }
}

int main() {
  int k;
  cin >> k;

  int gcd_sum = 0;

  for (int a = 1; a < k + 1; a++) {
    for (int b = 1; b < k + 1; b++) {
      for (int c = 1; c < k + 1; c++) {
        gcd_sum += gcd(gcd(a, b), c);
      }
    }
  }

  cout << gcd_sum << endl;
}

Error: TLE

Fixed submission:

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

int gcd(int x, int y) {
  if (x < y) {
    swap(x, y);
  }
  if (x % y == 0) {
    return y;
  } else {
    return gcd(x, x % y);
  }
}

int main() {
  int k;
  cin >> k;

  int gcd = 0;
  int gcd_sum = k * k + (k - 1) * k;

  for (int a = 2; a < k + 1; a++) {
    for (int b = 2; b < k + 1; b++) {
      for (int c = 1; c < k + 1; c++) {
        for (int i = 1; i < c + 1; i++) {
          if (a % i == 0 && b % i == 0 && c % i == 0) {
            gcd = i;
          }
        }
        gcd_sum += gcd;
      }
    }
  }

  cout << gcd_sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
using ll = long long;

int main() {
  int K;
  cin >> K;
  int sum = 0;
  int m = 0;
  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {
        m = min({a, b, c});
        int mm = 1;
        for (int k = m; 1 <= k; k--) {
          if (a % k == 0 && b % k == 0 && c % k == 0) {
            mm = k;
            break;
          }
        }
        sum += mm;
      }
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;
using ll = long long;

int main() {
  int K;
  cin >> K;
  int sum = 0;
  int m = 0;
  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {
        m = max({a, b, c});
        int mm = 1;
        for (int k = 2; k <= m; k++) {
          if (a % k == 0 && b % k == 0 && c % k == 0) {
            mm = k;
          }
        }
        sum += mm;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

int main(int argc, char **argv) {
  int num, sum = 0, buf;
  std::cin >> num;
  for (int i = 1; i <= num; i++) {
    for (int j = i; j <= num; j++) {
      for (int k = j; k <= num; k++) {
        if (i == j && j == k) {
          sum += i;
          break;
        } else if ((i == j || i == k || j == k) ||
                   (i != j && i != k && j != k)) {
          buf = i < j ? j : i;
          buf = buf < k ? k : buf;
          for (int l = buf; l > 0; l--) {
            if (i % l == 0 && j % l == 0 && k % l == 0) {
              sum += l * 6;
              break;
            }
          }
        }
      }
    }
  }
  std::cout << sum << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int main(int argc, char **argv) {
  int num, sum = 0, buf;
  std::cin >> num;
  for (int i = 1; i <= num; i++) {
    for (int j = 1; j <= num; j++) {
      for (int k = 1; k <= num; k++) {
        buf = i < j ? j : i;
        buf = buf < k ? k : buf;
        for (int l = buf; l > 0; l--) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            sum += l;
            break;
          }
        }
      }
    }
  }
  std::cout << sum << std::endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a < b) {
    int r = b;
    b = a;
    a = r;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
int gcd2(int a, int b, int c) { return gcd(a, gcd(b, c)); }
int main() {
  long int k, ans = 0;
  cin >> k;
  for (int a = 0; a < k; a++) {
    for (int b = 0; b < k; b++) {
      for (int c = 0; c < k; c++) {
        ans += gcd2(a, b, c);
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int gcd(int x, int y) {
  if (x < y) {
    swap(x, y); // 小さい方をyとする
  }
  int r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}

int gcd3(int a, int b, int c) {
  int g = gcd(a, b);
  return gcd(g, c);
}

int main() {
  int K;
  cin >> K;
  long long sum = 0;
  for (int a = 1; a <= K; ++a) {
    for (int b = 1; b <= K; ++b) {
      for (int c = 1; c <= K; ++c) {
        sum += gcd3(a, b, c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

int gcd3(int a, int b, int c) {
  int g = gcd(a, b);
  return gcd(g, c);
}

int main() {
  int K;
  cin >> K;
  long long sum = 0;
  for (int a = 1; a <= K; ++a) {
    for (int b = 1; b <= K; ++b) {
      for (int c = 1; c <= K; ++c) {
        sum += gcd3(a, b, c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rrep(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++)
#define mrep(i, n) for (ll i = (ll)(n - 1); i >= 0; i--)
#define be(v) (v).begin(), (v).end()
#define dcout cout << fixed << setprecision(20)
ll INF = 1LL << 60;
ll mod = 1e9 + 7;

int gcd(int a, int b) {

  if (a > b)
    swap(a, b);

  if (a % b == 0)
    return b; // 最大公約数

  else
    return gcd(a, b);
}

int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  rrep(i, 1, n) rrep(j, 1, n) rrep(k, 1, n) { ans += gcd(i, gcd(j, k)); }
  cout << ans << endl;
}

Error: -8

Fixed submission:

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

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(int x, int y, int z) {
  int maxx = max(max(x, y), z);
  for (int i = maxx; i >= 1; i--)
    if (x % i == 0 && y % i == 0 && z % i == 0)
      return i;
  return 1;
}

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(int x, int y, int z) {
  int maxx = max(max(x, y), z);
  for (int i = maxx; i >= 1; i--)
    if (x % i == 0 && y % i == 0 && z % i == 0)
      return i;
  return 1;
}
int main() {
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        sum += gcd(i, j, k);
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#include <iostream>      // cout, endl, cin
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP1(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

int gcd(int a, int b, int c) {
  int ma, ret;
  ma = min(a, min(b, c));
  for (int i = ma; i > 0; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      ret = i;
      break;
    }
  }
  return ret;
}

int main(void) {
  int K, i, j, k, g;
  long long int sum = 0;
  cin >> K;
  REP1(i, 1, K) {
    REP1(j, 1, K) {
      REP1(k, 1, K) {
        g = gcd(i, j, k);
        sum += g;
      }
    }
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#include <iostream>      // cout, endl, cin
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP1(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

int gcd(int a, int b, int c) {
  int ma, ret;
  ma = max(a, max(b, c));
  for (int i = ma; i > 0; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      ret = i;
      break;
    }
  }
  return ret;
}

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
int gcd2(int a, int b) {
  int ans = 1;
  for (int i = a; i > 1; i--) {
    if (a % i == 0 && b % i == 0) {
      ans = i;
      break;
    }
  }
  return ans;
}
int gcd(int a, int b, int c) {
  int ans = 1;
  for (int i = a; i > 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      ans = i;
      break;
    }
  }
  return ans;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
int gcd(int a, int b) {
  int ans = 1;
  for (int i = a; i > 1; i--) {
    if (a % i == 0) {
      if (b % i == 0) {
        ans = i;
        break;
      }
    }
  }
  return ans;
}

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