alexjercan/bugnet huggingface dataset:
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;

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

Error: TLE

Fixed submission:

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

int gcc(int a, int b, int c) {
  int o = 0;
  for (int r = 0; r < max(a, max(b, c)) + 1; r++) {
    if (r == 0)
      continue;
    if (a % r == 0 && b % r == 0 && c % r == 0) {
      o = r;
    }
  }
  return o;
}
int main() {
  int k;
  cin >> k;
  int ans = 0;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < k; j++) {
      for (int l = 0; l < k; l++) {
        ans += gcc(i + 1, j + 1, l + 1);
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? (-(a)) : (a))

typedef long long ll;

using namespace std;

int get_gcd(int a, int b) {
  while (b) {
    int temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int K;
  ll sum = 0;

  scanf("%d", &K);

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

  printf("%lld\\n", sum);

  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? (-(a)) : (a))

typedef long long ll;

using namespace std;

int get_gcd(int a, int b) {
  while (b) {
    int temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}

int main() {
  freopen("input.txt", "r", stdin);

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int K;
  ll sum = 0;

  scanf("%d", &K);

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

  printf("%lld\\n", sum);

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

#include <bits/stdc++.h>

using namespace std;

int gcd(int a, int b) {
  int tmp = max(a, b);
  for (int i = tmp; i != 1; i--) {
    if (a % i == 0 && b % i == 0) {
      return i;
    }
  }
  return 1;
}

int main() {
  int n;
  cin >> n;
  long long ans = 0;
  vector<int> v;
  for (int a = 1; a <= n; a++) {
    for (int b = 1; b <= n; b++) {
      // cout << gcd(a,b) << endl;
      int tmp = gcd(a, b);
      if (tmp == 1) {
        ans += n;
      } else {
        v.push_back(tmp);
      }
      // for(int c=1;c<=n;c++){
      //   //cout << a << " " << b << " " << c << " " << gcd(a, b, c) << endl;
      //   cout << gcd(a, b, c) << endl;
      //   ans += gcd(a, b, c);
      // }
    }
  }
  for (int c = 1; c <= n; c++) {
    for (int i = 0; i < v.size(); i++) {
      ans += gcd(c, v.at(i));
    }
  }
  // cout << v.size() << endl;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int gcd(int a, int b) {
  int tmp = max(a, b);
  for (int i = tmp; i != 1; i--) {
    if (a % i == 0 && b % i == 0) {
      return i;
    }
  }
  return 1;
}

int main() {
  int n;
  cin >> n;
  long long ans = 0;
  vector<int> v;
  for (int a = 1; a <= n; a++) {
    for (int b = 1; b <= n; b++) {
      // cout << gcd(a,b) << endl;
      v.push_back(gcd(a, b));
      // for(int c=1;c<=n;c++){
      //   //cout << a << " " << b << " " << c << " " << gcd(a, b, c) << endl;
      //   cout << gcd(a, b, c) << endl;
      //   ans += gcd(a, b, c);
      // }
    }
  }
  for (int c = 1; c <= n; c++) {
    for (int i = 0; i < v.size(); i++) {
      ans += gcd(c, v.at(i));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);

  int k;
  cin >> k;

  int sum = 0;
  vec a1, a2;

  RFOR(i, 1, k + 1) {
    RFOR(j, 1, k + 1) {
      int tmp = min(i, j);
      RFOR(l, 1, tmp + 1) {
        if (i % l == 0 && j % l == 0) {
          a1.push_back(l);
          break;
        }
      }
    }
  }

  REP(i, a1.size()) {
    RFOR(j, 1, k + 1) {
      int tmp = min(a1[i], j);
      RFOR(l, 1, tmp + 1) {

        if (a1[i] % l == 0 && j % l == 0) {
          sum += l;
          break;
        }
      }
    }
  }

  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
const double EPS = 1e-9;
#define INF (1LL << 60)
#define D double
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define SP << " " <<
typedef pair<int, int> P;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);

  int k;
  cin >> k;

  int sum = 0;

  FOR(i, 1, k + 1) {
    FOR(j, 1, k + 1) {
      FOR(l, 1, k + 1) {
        int tmp = 1;
        int m = min(i, j);
        m = min(m, l);
        RFOR(n, 1, m + 1) {
          if (i % n == 0 && j % n == 0 && l % n == 0) {
            tmp = n;
            break;
          }
        }
        sum += tmp;
      }
    }
  }

  cout << sum << endl;

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

#include <bits/stdc++.h>
#include <numeric>
using namespace std;
int gcd(int u, int v) {
  while (v != 0) {
    unsigned r = u % v;
    u = v;
    v = r;
  }
  return u;
}
int i, j, k, count1 = 0, count2 = 0;
void ABC161();
void ABC162();
void ABC163();
void ABC164();

int main() { ABC162(); }

void ABC162() {
  int k, l, m, sum = 0, max;
  cin >> k;
  for (i = 1; i <= k; i++) {
    for (j = 1; j <= k; j++) {
      for (l = 1; l <= k; l++) {
        sum += gcd(i, gcd(j, l));
      }
    }
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int i, j, k, count1 = 0, count2 = 0;
void ABC161();
void ABC162();
void ABC163();
void ABC164();

int main() { ABC162(); }

void ABC162() {
  int k, l, m, sum = 0, max;
  cin >> k;
  for (i = 1; i <= k; i++) {
    for (j = 1; j <= k; j++) {
      for (l = 1; l <= k; l++) {
        max = 1;
        for (m = 2; m <= i; m++) {
          if (i % m == 0 && j % m == 0 && l % m == 0) {
            max = m;
          }
        }
        sum += max;
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int gcd(int a, int b) {
  if (a >= b) {
    if (a % b == 0) {
      return (b);
    } else {
      return (gcd(b, a % b));
    }
  } else {
    if (b % a == 0) {
      return (a);
    } else {
      return (gcd(a, b % a));
    }
  }
}
int main() {
  short int K = 0;
  int result = 0;
  cin >> K;
  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {

        result += gcd(a, gcd(b, c));
      }
    }
  }
  cout << result;

  return 0;
}

Error: -8

Fixed submission:

#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() {
  short int K;
  int result;
  cin >> K;
  for (int a = 0; a < K; a++) {
    for (int b = 0; b < K; b++) {
      for (int c = 0; c < K; c++) {
        result += gcd(a, gcd(b, c));
      }
    }
  }
  cout << result;

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

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

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

int main() {

  int K;
  cin >> K;
  int 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 += solvegcd(solvegcd(a, b), c);
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {

  int K;
  cin >> K;
  int 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++) {
        int gcd = 1;
        int maxNum = max({a, b, c});
        for (int i = 1; i < maxNum + 1; i++) {
          if (i > gcd && a % i == 0 && b % i == 0 && c % i == 0) {
            gcd = i;
          }
        }
        ans += gcd;
      }
    }
  }

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

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

int my_gcd(int x, int y) {
  if (x % y == 0) {
    return y;
  }
  return my_gcd(y, x % y);
}

int main(void) {
  long double K;
  cin >> K;

  int64_t sum = 0;

  for (int i = 1; i < K + 1; i++) {
    for (int j = 1; j < K + 1; j++) {
      int gcd1 = my_gcd(i, j);
      for (int k = 1; k < K + 1; k++) {
        int gcd2 = my_gcd(gcd1, k);
        sum += gcd2;
      }
    }
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int K;
  cin >> K;
  int64_t sum = 0;
  for (int i = 1; i < K + 1; i++) {
    for (int j = 1; j < K + 1; j++) {
      for (int k = 1; k < K + 1; k++) {
        int x = max(i, max(j, k));
        int gcd = 1;
        for (int l = 1; l < x + 1; l++) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            gcd = max(gcd, l);
          }
        }
        sum += gcd;
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
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++) {
        sum += gcd(gcd(i, j), k);
      }
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int sum = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 1; k <= N; k++) {
        int s;
        for (int l = 1; l <= N; l++) {
          if (i % l == 0 && j % l == 0 && k % l == 0)
            s = l;
        }
        sum += s;
      }
    }
  }
  cout << sum << endl;

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

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int cal_gcd(int a, int b);
int main() {
  int k;
  ll 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 += cal_gcd(i, cal_gcd(j, l));
      }
    }
  }
  cout << sum << endl;
  return 0;
}

int cal_gcd(int a, int b) {
  int buf = 0;
  if (a < b) {
    buf = b;
    b = a;
    a = buf;
  }
  if (a % b == 0)
    return b;
  return cal_gcd(a % b, b);
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int cal_gcd(int a, int b);
int main() {
  int k;
  ll 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 += cal_gcd(i, cal_gcd(j, l));
      }
    }
  }
  cout << sum << endl;
  return 0;
}

int cal_gcd(int a, int b) {
  int buf = 0;
  if (a < b) {
    buf = b;
    b = a;
    a = buf;
  }
  if (a % b == 0)
    return b;
  cal_gcd(a % b, b);
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  int min;

  if (a < b) {
    min = a;
  } else {
    min = b;
  }
  if (min > c) {
    min = c;
  }

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

int main() {
  int K;

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

Error: TLE

Fixed submission:

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

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

  for (int i = 1;; i++) {
    if (i > a && i > b) {
      if (i > c) {
        break;
      }
    }
    if (a % i == 0 && b % i == 0) {
      if (c % i == 0) {
        g = i;
      }
    }
  }
  return g;
}

int main() {
  int K;

  cin >> K;
  int i, j, k;
  int sum = 0;
  for (i = 0; i < K; i++) {
    for (j = 0; j < K; j++) {
      for (k = 0; k < K; k++) {
        sum += gcd(i + 1, j + 1, k + 1);
        // cout << sum << endl;
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

int sum = 0;
void gcd(int a, int b, int c) {
  int x = min(min(a, b), c);
  for (int y = x; y > 0; y--) {
    if (a % y == 0 && b % y == 0 && c % y == 0) {
      sum = sum + y;
      break;
    }
  }
}

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

Error: 0

Fixed submission:

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

int K;
int sum = 0;
int gcd(int a, int b, int c) {
  int x = min(min(a, b), c);
  for (int y = x; y > 0; y--) {
    if (a % y == 0 && b % y == 0 && c % y == 0) {
      sum = sum + y;
      break;
    }
  }
}

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

#include <iostream>
using namespace std;

int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int main() {
  int n, g;
  long int sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        int g = gcd(i, gcd(j, k));
        sum = sum + g;
      }
    }
  }
  cout << sum << "\\n";
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, g;
  long int sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        for (int g = max(i, max(j, k)); g >= 1; g--) {
          if (i % g == 0 && j % g == 0 && k % g == 0) {
            sum = sum + g;
            break;
          }
        }
      }
    }
  }
  cout << sum << "\\n";
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define SORT(v) sort(v.begin(), v.end())
#define si(n) scanf("%d", &n)
#define sii(n, m) scanf("%d %d", &n, &m)
#define sl(n) scanf("%lld", &n)
#define sll(n, m) scanf("%lld %lld", &n, &m)
#define ss(cad) scanf("%s", cad)
#define PB push_back
#define fst first
#define scn second
#define DBG(x) cerr << #x << " = " << (x) << endl;
#define M 1000000007
#define N_MAX 100010

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef vector<pi> vp;

int main() {
  int k;
  si(k);
  ll ans = 0;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      for (int m = 1; m <= k; m++) {
        int a = __gcd(i, j);
        a = __gcd(a, m);
        ans += a;
      }
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define ll long long int
#define FOR(i, a, b) for (ll(i) = (a); i < (ll)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n)
#define outi(a) printf("%lld\\n", (a))
#define prial(a) cout << a << endl;
#define line cout << endl;
#define pb push_back
using namespace std;
inline ll read() {
  ll x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
  return x;
}
//****************************

int tt;
bool checkit(int a) {
  if (a < 0)
    return true;
  return false;
}

//****************************
int solve(int i);
int main() { solve(0); }
int solve(int qa) {
  int a = read();
  ll sum = 0;
  rep1(i, a + 1) rep1(j, a + 1) rep1(q, a + 1)
      // cout << i << j << q << endl,
      sum += __gcd(i, __gcd(j, q));
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

int gcd(int a, int b) {
  int c = 1;
  if (a < b) {
    c = a;
    a = b;
    b = c;
  }
  while (c != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}

int main() {

  int k, ans = 0;
  std::cin >> k;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      for (int n = 1; n <= k; n++) {
        ans += gcd(gcd(i, j), n);
      }
  std::cout << ans << std::endl;
}

Error: TLE

Fixed submission:

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

void swap(int *x, int *y) {
  int tmp;
  tmp = *x;
  *x = *y;
  *y = tmp;
}

int euclid(int small, int big) {
  int res = 0;
  if (small > big)
    swap(&small, &big);

  while (true) {
    res = big % small;
    if (res = 0)
      return small;
    big = small;
    small = res;
  }
  return 0;
}

int main() {
  int x;
  cin >> x;
  long long sm = 0;
  for (int i = 1; i <= x; i++) {
    for (int j = 1; j <= x; j++) {
      for (int k = 1; k <= x; k++) {
        sm = sm + euclid(euclid(i, j), k);
      }
    }
  }
  cout << sm << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

ll gcd(ll &a, ll &b, ll &c) {
  ll k = min(a, b);
  k = min(k, c);
  ll g;
  for (ll i = 1; i <= k; i++) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      g = i;
    }
  }
  return g;
}

int main() {
  ll K;
  cin >> K;
  ll ans = 0;
  for (ll i = 1; i <= K - 2; i++) {
    for (ll j = i + 1; j <= K - 1; j++) {
      for (ll h = j + 1; h <= K; h++) {
        ans += gcd(i, j, h);
      }
    }
  }

  ans *= 6;
  ll t = 0;
  for (ll i = 1; i <= K; i++) {
    for (ll j = 1; j <= K; j++) {
      if (i == j) {
        continue;
      }
      t += gcd(i, i, j);
    }
  }

  t *= 3;
  ans += t;
  for (ll i = 1; i <= K; i++) {
    ans += i;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

ll gcd(ll a, ll b, ll c) {
  ll k = min(a, b);
  k = min(k, c);
  ll g;
  for (ll i = 1; i <= k; i++) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      g = i;
    }
  }
  return g;
}

int main() {
  ll K;
  cin >> K;
  ll ans = 0;
  for (ll i = 1; i <= K; i++) {
    for (ll j = 1; j <= K; j++) {
      for (ll h = 1; h <= K; h++) {
        ans += gcd(i, j, h);
      }
    }
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */

int main() {
  int k;
  cin >> k;
  long long int sum = 0;
  int i, j, x;
  for (i = 1; i <= k; i++)
    for (j = 1; j <= k; j++)
      for (x = 1; x <= k; x++)
        sum += __gcd(__gcd(i, j), x);
  cout << sum;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */

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

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

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

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


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll dp[55][55][55];
ll gcd(ll x, ll y, ll z) {
  if (dp[x][y][z] != -1)
    return dp[x][y][z];
  ll d = 1;
  if (x != 1 or y != 1 or z != 1) {
    d = __gcd(__gcd(x, y), z);
  }
  dp[x][y][z] = dp[y][x][z] = dp[x][z][y] = dp[z][y][x] = dp[z][x][y] =
      dp[y][z][x] = d;
  return dp[x][y][z];
}

int main() {
  ll n, sum = 0;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (ll i = 1; i <= n; i++)
    for (ll j = 1; j <= n; j++)
      for (ll k = 1; k <= n; k++)
        sum += gcd(i, j, k);
  cout << sum;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int k;
  cin >> k;
  ll sum = 0;
  rep(i, k) {
    int a = i + 1;
    rep(j, k) {
      int b = j + 1;
      rep(l, k) {
        int c = l + 1;
        int p = min(a, min(b, c));
        while (p > 0) {
          if (a % p == 0) {
            if (b % p == 0) {
              if (c % p == 0) {
                sum += p;
                break;
              }
            }
          }
          p--;
        }
      }
    }
  }
  cout << sum << endl;
}

Error: -8

Fixed submission:

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

int x, y;

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

int main() {
  int k;
  cin >> k;
  int ans = 0;
  rep(a, k) {
    rep(b, k) {
      rep(c, k) { ans += gcd(a, gcd(b, c)); }
    }
  }
  cout << ans << endl;
}