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

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

#define froop1(a, n) for (long long i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)
int main() {
  long long N, sum = 0;
  cin >> N;
  froop1(1, N + 1) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        sum += gcd(i, gcd(j, k));
      }
    }
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

#define froop1(a, n) for (long long i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)
int main() {
  long long N, sum = 0;
  cin >> N;
  froop1(1, N + 1) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        int min = i, max = 0;
        if (min > j) {
          min = j;
        }
        if (min > k) {
          min = k;
        }
        for (int l = 1; l <= min; l++) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            max = l;
          }
        }
        sum += max;
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
// const long long MOD = ;
const long long INF_LL = 1e18 + 3;
const int INF_int = 1e9 + 3;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define All(a) a.begin(), a.end()
#define Debug_Output_ALL(a)                                                    \\
  for (auto itr = a.begin(); itr != a.end(); itr++)                            \\
    cout << *itr << " ";                                                       \\
  cout << endl;
// 最大公約数
long long gcd_(long long a, long long b) {
  if (a < b)
    swap(a, b);
  while (a % b != 0) {
    swap(a, b);
    b = b % a;
  }
  return b;
}
int main() {
  ll a, b, c;
  cin >> a;
  ll ans = 0;
  lrep(i, a) lrep(j, a) lrep(k, a) ans += gcd_(i + 1, gcd_(j + 1, k + 1));
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
// const long long MOD = ;
const long long INF_LL = 1e18 + 3;
const int INF_int = 1e9 + 3;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define All(a) a.begin(), a.end()
#define Debug_Output_ALL(a)                                                    \\
  for (auto itr = a.begin(); itr != a.end(); itr++)                            \\
    cout << *itr << " ";                                                       \\
  cout << endl;
// 最大公約数
long long gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  while (a % b != 0) {
    swap(a, b);
    b = b % a;
  }
  return b;
}
int main() {
  ll a, b, c;
  cin >> a >> b >> c;
  ll ans = 0;
  rep(i, a) rep(j, b) rep(k, c) ans += gcd(i + 1, gcd(j + 1, k + 1));
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  int K;
  cin >> K;
  vector<int> A(210);

  A[0] = 0;
  A[1] = 1;

  for (int k = 2; k <= K; k++) {
    int first = 0;
    for (int i = 1; i < k; i++) {
      for (int j = 1; j < k; j++) {
        first += find_gcd(i, j, k);
      }
    }

    int second = 0;
    for (int i = 1; i < k; i++) {
      second += find_gcd(i, k, k);
    }
    A[k] = A[k - 1] + 3 * first + 3 * second + k;
  }

  cout << A[K] << endl;
}

Error: TLE

Fixed submission:

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

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

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 k = 1; k <= K; k++) {
        ans += find_gcd(i, j, k);
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// 最大公約数 gcd
long long gcdTest(long long x, long long y) {
  if (y > x)
    swap(x, y);
  if (y == 0)
    return x;
  return gcdTest(y, x % y);
}

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

  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {
        ans += gcdTest(a, gcdTest(b, c));
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

// 最大公約数 gcd
int gcdTest(int x, int y) {
  if (y > x)
    swap(x, y);
  int surplus = x % y;
  if (surplus != 0) {
    gcdTest(y, surplus);
  } else {
    return y;
  }
}

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

  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {
        ans += gcdTest(a, gcdTest(b, c));
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
  int k;
  cin >> k;
  ll ans = 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++) {
        ans += gcd(a, gcd(b, c));
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int k;
  cin >> k;
  long long int kot = 0;
  int ans = 1;
  for (int a = 1; a < k + 1; a++) {
    for (int b = 1; b < k + 1; b++) {
      for (int c = 1; c < k + 1; c++) {
        for (int i = 1; i < max(a, max(b, c)) + 1; i++) {
          if (a % i == 0 && b % i == 0 && c % i == 0) {
            ans = i;
          }
        }

        kot += ans;
      }
    }
  }

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

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

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

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


Error: -8

Fixed submission:

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

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

int main() {
  int K;
  cin >> K;
  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(i, j), l);
      }
    }
  }
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * C - Sum of gcd of Tuples (Easy)
 * https://atcoder.jp/contests/abc162/tasks/abc162_c
 */
#include <bits/stdc++.h>
using namespace std;

// 最大公約数を求める
int gcd(int a, int b, int c) {
  int minNum = min(min(a, b), c);
  int maxDivisor = 1;
  for (int i = 2; i <= minNum; i++) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      maxDivisor = i;
    }
  }

  return maxDivisor;
}

int main() {
  // ∑a=1K∑b=1K∑c=1gcd(a,b,c) を求めてください。
  // ただし、gcd(a,b,c) は a,b,c の最大公約数を表します。
  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(a, b, c);
      }
    }
  }

  cout << sum << endl;
}


Error: TLE

Fixed submission:

/**
 * C - Sum of gcd of Tuples (Easy)
 * https://atcoder.jp/contests/abc162/tasks/abc162_c
 */
#include <bits/stdc++.h>
using namespace std;

// 最大公約数を求める
int gcd(int a, int b, int c) {
  int maxNum = max(max(a, b), c);
  int maxDivisor = 1;
  for (int i = 2; i <= maxNum; i++) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      maxDivisor = i;
    }
  }

  return maxDivisor;
}

int main() {
  // ∑a=1K∑b=1K∑c=1gcd(a,b,c) を求めてください。
  // ただし、gcd(a,b,c) は a,b,c の最大公約数を表します。
  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(a, b, c);
      }
    }
  }

  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, int c) {
  if (a == 1 and b == 1 and c == 1) {
    return 1;
  }
  int minimum = min(min(a, b), c);
  int ans;
  for (int i = 1; i <= minimum; i++) {
    if (a % i == 0 and b % i == 0 and c % i == 0) {
      ans = i;
    }
  }
  return ans;
}

int sum_gcd(int x) {
  if (x == 1) {
    return 1;
  }
  long long ans = 0;
  for (int i = 1; i < x; i++) {
    ans += 3 * gcd(i, x, x);
    for (int j = 1; j < x; j++) {
      ans += 3 * gcd(i, j, x);
    }
  }
  ans += x;
  return ans;
}

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

  vector<long long> A(K);
  A[0] = 1;

  if (K == 1) {
    cout << 1 << endl;
    exit(0);
  }

  for (int i = 1; i < K; i++) {
    A[i] = A[i - 1] + sum_gcd(i + 1);
  }

  cout << A[K - 1] << endl;
}

Error: TLE

Fixed submission:

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

int gcd(int a, int b, int c) {
  int maximum = max(max(a, b), c);
  int ans;
  for (int i = 1; i <= maximum; i++) {
    if (a % i == 0 and b % i == 0 and c % i == 0) {
      ans = i;
    }
  }
  return ans;
}

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(i, j, k);
      }
    }
  }

  cout << ans << 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) {
    int z = x;
    x = y;
    y = z;
  }
  if (x % y == 0) {
    return y;
  } else {
    return gcd(y, x % y);
  }
}

int main() {
  int k;
  int sum = 0;
  int j;
  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++) {
        j = gcd(a, gcd(b, c));
        sum = sum + j;
      }
    }
  }

  cout << sum << endl;
}


Error: TLE

Fixed submission:

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

int gcd(int x, int y, int z) {
  int d = 1;
  for (int i = 0; i < max(x, max(y, z)); i++) {
    if ((x % (i + 1) == 0) && (y % (i + 1) == 0) && (z % (i + 1) == 0)) {
      d = i + 1;
    }
  }
  return d;
}

int main() {
  int k;
  int sum = 0;
  int j;
  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++) {
        j = gcd(a, b, c);
        sum = sum + j;
      }
    }
  }

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

#include "bits/stdc++.h"
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
using namespace std;
namespace adaptor = boost::adaptors;

void Main() {
  int N;

  cin >> N;

  int min;
  int S[N * N * N];
  int count = 0;
  int sum = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {

        // 最小値判別
        if (i <= j) {
          if (i <= k) {
            min = i;
          } else {
            min = k;
          }
        } else {
          if (j <= k) {
            min = j;
          } else {
            min = k;
          }
        }

        // 最大公約数判定
        for (int l = 1; l <= min; l++) {
          if (i % l == 0 && j % l == 0 && k % l == 0)
            S[count] = l;
        }

        // カウントアップ
        count++;
      }
    }
  }

  for (int i = 0; i < N * N * N; i++) {
    sum += S[i];
  }

  printf("%d", sum);
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

Error: -8

Fixed submission:

#include "bits/stdc++.h"
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
using namespace std;
namespace adaptor = boost::adaptors;

void Main() {
  int N;

  cin >> N;

  int min;
  int S[N * N * N];
  int count = 0;
  int sum = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {

        // 最小値判別
        if (i <= j) {
          if (i <= k) {
            min = i;
          } else {
            min = k;
          }
        } else {
          if (j <= k) {
            min = j;
          } else {
            min = k;
          }
        }

        // 最大公約数判定
        for (int l = 0; l < min; l++) {
          if (i % l == 0 && j % l == 0 && k % l == 0)
            S[count] = l;
        }

        // カウントアップ
        count++;
      }
    }
  }

  for (int i = 0; i < N * N * N; i++) {
    sum += S[i];
  }

  printf("%d", sum);
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int gcd2(int a, int b) {
  int c, max = 0;
  max = b;
  if (max < a) {
    max = a;
    a = b;
    b = max;
  }
  c = b % a;
  if (c == 0) {
    return a;
  } else {
    return gcd2(a, c);
  }
}

int gcd3(int a, int b, int c) {
  int x, y;
  x = gcd2(a, b);
  y = gcd2(x, c);
  return y;
}

int main(void) {
  int k, ans = 0;

  cin >> k;

  for (int i = 1; i <= k; i++) {
    for (int s = 1; s <= k; s++) {
      for (int t = 1; t <= k; t++) {
        ans = ans + gcd3(i, s, t);
      }
    }
  }
  cout << ans << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int gcd2(int a, int b) {
  int c, max = 0;
  max = b;
  if (max < a) {
    max = a;
    a = b;
    b = max;
  }
  c = b % a;
  if (c == 0) {
    return a;
  } else {
    gcd2(a, c);
  }
}

int gcd3(int a, int b, int c) {
  int x, y;
  x = gcd2(a, b);
  y = gcd2(x, c);
  return y;
}

int main(void) {
  int k, ans = 0;

  cin >> k;

  for (int i = 1; i <= k; i++) {
    for (int s = 1; s <= k; s++) {
      for (int t = 1; t <= k; t++) {
        ans = ans + gcd3(i, s, t);
      }
    }
  }
  cout << ans << "\\n";

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

#include <algorithm>
#include <iostream>
#include <numeric>

using namespace std;

int main(void) {
  long long int K;
  long long int answer = 0;

  scanf("%Ld", &K);
  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {
        answer += __gcd(__gcd(a, b), c);
      }
    }
  }
  printf("%Ld\\n", answer);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <numeric>
#include <set>

using namespace std;

int main(void) {
  long long int K;
  set<multiset<int>> set_calc;
  multiset<int> cache;
  long long int answer = 0;

  scanf("%Ld", &K);
  for (int a = 1; a <= K; a++) {
    cache.clear();
    cache.insert(a);
    for (int b = 1; b <= K; b++) {
      auto it_b = cache.insert(b);
      for (int c = 1; c <= K; c++) {
        auto it_c = cache.insert(c);
        set_calc.insert(cache);
        cache.erase(it_c);
      }
      cache.erase(it_b);
    }
  }
  /*
          for(auto a: set_calc) {
                  for(auto b: a) {
                          cout << b << ",";
                  }
                  cout << endl;
          }
  */
  set<int> count;
  int mul;
  for (auto a : set_calc) {
    count.clear();
    for (int b : a)
      count.insert(b);
    mul = (count.size() == 1 ? 1 : count.size() == 2 ? 3 : 6);
    auto itr = a.begin();
    answer += mul * __gcd(*itr, __gcd(*next(itr, 1), *next(itr, 2)));
  }
  printf("%Ld\\n", answer);
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int k;
ll ans;
int gcd(int x, int y) {
  int r = x % y;
  while (r) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}
int main() {
  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(gcd(a, b), c);
      }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

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

#include <iostream>
#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 i, int j) {
  int num, r, shori[3];

  if (i > j) {
    shori[0] = i;
    shori[1] = j;
    shori[2] = i % j;
  } else {
    shori[0] = j;
    shori[1] = i;
    shori[2] = j % i;
  }
  while (shori[2]) {
    shori[0] = shori[1];
    shori[1] = shori[2];
    shori[2] = shori[0] % shori[1];
  }

  return shori[1];
}

int main() {
  int k, num = 0;

  cin >> k;

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

  cout << num << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#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 i, int j, int k) {
  int num, max;

  max = i;
  if (max < j)
    max = j;
  if (max < k)
    max = k;

  for (num = max; num > 0; num--) {
    if (i % num == 0 && j % num == 0 && k % num == 0)
      break;
  }

  return num;
}

int main() {
  int k, num = 0;

  cin >> k;

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

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

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

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

int 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(a, b, c);
      }
    }
  }
  cout << sum << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

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

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

int 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(a, b, c);
      }
    }
  }
  cout << sum << endl;

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

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

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

  int ans = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = i; j <= N; j++) {
      for (int k = j; k <= N; k++) {
        int gcd = 0;
        for (int l = j; l > 0; l--) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            if (i == j && j == k) {
              ans += l;
            } else if (i == j || j == k) {
              ans += l * 3;
            } else {
              ans += l * 6;
            }
            l = 0;
          }
        }
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

  int ans = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      int m = max(i, j);
      for (int k = 1; k <= N; k++) {
        m = max(m, k);
        int gcd = 0;
        for (int l = m; l > 0; l--) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            ans += l;
            l = 0;
          }
        }
      }
    }
  }

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

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

int K;

int main() {
  cin >> K;
  long ans = 0;
  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      int codiv;
      for (int k = 1; k <= min(i, j); k++) {
        if (i % k == 0 && j % k == 0)
          codiv = k;
      }
      for (int k = 1; k <= K; k++) {
        int codiv2;
        for (int l = 1; l <= min(i, j); l++) {
          if (k % l == 0 && codiv % l == 0)
            codiv2 = l;
        }
        ans += codiv2;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int K;

int main() {
  cin >> K;
  long ans = 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) {
            ans += l;
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

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

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

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int gcd(int a, int b, int c) {
  int kouyaku;
  for (int i = a; i > 0; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      kouyaku = i;
      break;
    }
  }
  return kouyaku;
}

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

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

  cout << sum << endl;
}