alexjercan/bugnet huggingface dataset:
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  cout << sum << endl;
}


Error: 0

Fixed submission:

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

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

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

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

// omajinai c++ atcoder
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define itn int
typedef long long ll;
constexpr auto mod = 1000000007;
using namespace std;

int gcd(int p, int q) {
  if (p % q == 0)
    return q;
  return gcd(q, p % q);
}
int main() {
  long int k;
  cin >> k;
  long int sum = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      long int gcd1 = gcd(i, j);
      for (int l = 1; l <= k; l++) {
        sum += gcd(gcd1, l);
      }
    }
  }
  cout << sum;
}


Error: -8

Fixed submission:

// omajinai c++ atcoder
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define itn int
typedef long long ll;
constexpr auto mod = 1000000007;
using namespace std;

int gcd(int p, int q) {
  if (p % q == 0)
    return q;
  return gcd(q, p % q);
}
int main() {
  long int k;
  cin >> k;
  long int sum = 0;
  for (int i = 0; i <= k; i++) {
    for (int j = 0; j <= k; j++) {
      long int gcd1 = gcd(i, j);
      for (int l = 0; l <= k; l++) {
        sum += gcd(gcd1, l);
      }
    }
  }
  cout << sum;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

Error: -8

Fixed submission:

#include <iostream>
using namespace std;
int gcd(int b, int c) {
  while (b % c == 0) {
    int a = b % c;
    b = c;
    c = a;
  }
  return c;
}
int main() {
  int n;
  cin >> n;
  long long int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        ans += gcd(i, gcd(j, k));
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {

  // freopen ("input.txt", "r", stdin);

  int n;

  cin >> n;

  long long 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 temp = __gcd(i, j);
        sum += __gcd(temp, k);
      }
    }
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  freopen("input.txt", "r", stdin);

  int n;

  cin >> n;

  long long 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 temp = __gcd(i, j);
        sum += __gcd(temp, k);
      }
    }
  }

  cout << sum << endl;

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

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

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

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

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 l = 1; l <= k; ++l) {
        sum += gcd(i, j, l);
      }
    }
  }
  cout << sum;
}


Error: TLE

Fixed submission:

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

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

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

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 l = 1; l <= k; ++l) {
        sum += gcd(i, j, l);
      }
    }
  }
  cout << sum;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

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

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


Error: 0

Fixed submission:

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

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

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

#include <iostream>

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

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 l = 1; l <= k; ++l) {
        ans += gcd(i, gcd(j, l));
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int k;
  cin >> k;
  int ans = 0;
  for (int i = 1; i <= k; ++i) {
    for (int j = 1; j <= k; ++i) {
      for (int l = 1; l <= k; ++l) {
        ans += gcd(i, gcd(j, k));
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
#define ll long long
ll gcd(ll a, ll b) {
  while (b)
    b ^= a ^= b ^= a %= b;
  return a;
}
int main() {
  ll n, ans = 0;
  scanf("%lld", &n);
  ll c[n + 1];
  fill(c, c + n + 1, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      c[gcd(i, j)]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      ans += gcd(i, j) * c[j];
    }
  }
  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
#define ll long long
ll gcd(ll a, ll b) {
  while (b)
    b ^= a ^= b ^= a %= b;
}
int main() {
  ll n, ans = 0;
  scanf("%lld", &n);
  ll c[n + 1];
  fill(c, c + n + 1, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      c[gcd(i, j)]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      ans += gcd(i, j) * c[j];
    }
  }
  printf("%lld\\n", ans);
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

long long gcd(long long a, long long b) {
  long long c;
  while (b > 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}

int main() {
  long long sum = 0;
  long long n = 200;
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++)
      for (long long k = 1; k <= n; k++) {
        sum += gcd(gcd(i, j), k);
      }
  printf("%lld", sum);
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

// long long gcd(long long a, long long b)
//{
//     if(a < b)
//     {
//         long long tmp = a;
//         a = b;
//         b = tmp;
//     }
//     while(a != b || b == 1)
//     {
//         a = a % b;
//         b = a;
//     }
//     return max(b, (long long)1);
// }

int main() {
  long long sum = 0;
  long long n;
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++)
      for (long long k = 1; k <= n; k++) {
        for (long long l = min(min(i, j), k); l >= 1; l--)
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            sum += l;
            break;
          }
      }
  printf("%lld", sum);
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <math.h>

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

int main() {

  int k;
  std::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 += std::__gcd(a, std::__gcd(b, c));
      }
    }
  }
  std::cout << sum;

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <math.h>

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

int main() {

  int k;
  std::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 += gcd(a, b, c);
      }
    }
  }
  std::cout << sum;

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

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

int kansuu(int a, int b) {
  int tempa = max(a, b);
  int tempb = min(a, b);
  if (tempa % tempb == 0) {
    return tempb;
  } else {
    return kansuu(tempb, tempa % tempb);
  }
}

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

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

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int kansuu(int a, int b) {
  int tempa = max(a, b);
  int tempb = min(a, b);
  if (tempa % tempb == 0) {
    return tempb;
  } else {
    kansuu(tempb, tempa % tempb);
  }
}

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

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

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

#include <iostream>
using namespace std;
int main() {
  long K;
  cin >> K;
  long 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++) {
        int gcd = 0;
        for (int i = 1; i < a + 1 && i < b + 1 && i < c + 1; i++) {
          if (a % i == 0 && b % i == 0 && c % i == 0) {
            gcd = i;
          }
        }
        sum = sum + gcd;
      }
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long K;
  cin >> K;
  long 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++) {
        int gcd = 0;
        for (int i = 1; i < c + 1; i++) {
          if (a % i == 0 && b % i == 0 && c % i == 0) {
            gcd = i;
          }
        }
        sum = sum + gcd;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

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

int main() {

  int n;
  scanf("%d", &n);

  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) {
        int cur = gcd(i, j);
        sum += gcd(cur, k);
      }
    }
  }
  printf("%lld", sum);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {

  int n;
  scanf("%d", &n);

  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) {
        for (int l = n; l >= 1; --l) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            sum += l;
            // printf("%d ", l);
            break;
          }
        }
      }
    }
  }
  printf("%lld", sum);
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int compute_gcd(int i, int j) {
  int divisor = i, dividend = j, remainder;
  remainder = dividend % divisor;
  while (remainder) {
    dividend = divisor;
    divisor = remainder;
    remainder = dividend % divisor;
  }
  return divisor;
}

int main() {
  int n, i, j, k;
  long long int sum = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = i + 1; j <= n; j++) {
      for (k = j + 1; k <= n; k++) {
        sum += 6 * compute_gcd(compute_gcd(i, j), k);
      }
    }
  }
  sum += (n * (n + 1) / 2);
  for (i = 1; i <= n; i++) {
    for (j = i + 1; j <= n; j++) {
      sum += 6 * compute_gcd(i, j);
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int compute_gcd(int i, int j, int k) {
  int a, least = min(min(i, j), k);
  if (j % least == 0 && k % least == 0) {
    return least;
  }
  for (a = least / 2; a > 1; a--) {
    if (least % a == 0) {
      if (j % a == 0 && k % a == 0) {
        return a;
      }
    }
  }
  return 1;
}

int main() {
  int n, i, j, k, ans;
  long long int sum = 0;
  map<vector<int>, int> gcd;
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      for (k = 1; k <= n; k++) {
        vector<int> v{i, j, k};
        sort(v.begin(), v.end());
        if (gcd.find(v) != gcd.end()) {
          sum += gcd[v];
        } else {
          ans = compute_gcd(i, j, k);
          gcd[v] = ans;
          sum += ans;
        }
      }
    }
  }
  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;
  ans = 0;
  for (int a = 1; a <= N; a++) {
    for (int b = a; b <= N; b++) {
      for (int c = b; c <= N; c++) {
        for (int i = N; i >= 1; i--) {
          if (a % i == 0 && b % i == 0 && c % i == 0) {
            if (a != b && b != c && c != a) {
              ans += 6 * i;
            } else if (a != b || b != c) {
              ans += 3 * i;
            } else {
              ans += i;
            }
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define fix(n) cout << fixed << setprecision((int)n)

void absll(ll &x) {
  if (x >= 0)
    return;
  else
    x = -x;
  return;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// const ll INF = 1LL << 60;

int N;

// DPテーブル
// vector<ll> dp(100010);

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define fix(n) cout << fixed << setprecision((int)n)

void absll(ll &x) {
  if (x >= 0)
    return;
  else
    x = -x;
  return;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// const ll INF = 1LL << 60;

int N;

// DPテーブル
// vector<ll> dp(100010);

ll gcd(ll a, ll b, ll c) {
  ll mx = max(a, max(b, c));
  for (ll i = mx; i >= 1; i--)
    if (a % i == 0 && b % i == 0 && c % i == 0)
      return i;
  return 1;
}

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

#include <iostream>
#include <numeric>

int main() {

  int K = 0;
  std::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 += std::gcd(std::gcd(i, j), k);
      }
    }
  }

  std::cout << sum << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int max_divisor(const int i, const int j, const int k) {
  int divisor = std::max(std::max(i, j), j);

  for (int cnt = divisor; 0 < cnt; cnt--) {
    if (i % cnt == 0) {
      if (j % cnt == 0) {
        if (k % cnt == 0) {
          return cnt;
        }
      }
    }
  }

  return 0;
}

int main() {

  int K = 0;
  std::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 += max_divisor(i, j, k);
      }
    }
  }

  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 x, int y) {
  int a = min(x, y);
  int b = 0;
  for (int i = 0; i < a; i++) {
    if (x % (i + 1) == 0 && y % (i + 1) == 0) {
      b = i + 1;
    }
  }
  return b;
}

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 k = 0; k < K; k++) {
        ans += gcd(gcd(i + 1, j + 1), k + 1);
      }
    }
  }
  cout << ans << endl;
}


Error: -8

Fixed submission:

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

int gcd(int x, int y) {
  int a = min(x, y);
  int b = 0;
  for (int i = 0; i < a + 1; i++) {
    if (x % i == 0 && y % i == 0) {
      b = i;
    }
  }
  return b;
}

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 k = 0; k < K; k++) {
        ans += gcd(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;
int s = 0, a, b, c;
long long sum = 0;
int gcd(int a, int b, int c) {
  for (int i = a; i >= 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      s = i;
      break;
    }
  }
  return s;
}
int main() {
  int k;
  cin >> k;
  for (a = 1; a <= k; a++) {
    for (b = a; b <= k; b++) {
      for (c = b; c <= k; c++) {
        if (a == b && b == c)
          sum += gcd(a, b, c);
        else if (a == b || b == c || c == a)
          sum += 3 * gcd(a, b, c);
        else
          sum += 6 * gcd(a, b, c);
      }
    }
  }

  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int s = 0, a, b, c;
long long sum = 0;
int gcd(int a, int b, int c) {
  for (int i = a; i >= 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      s = i;
      break;
    }
  }
  return s;
}
int main() {
  int k;
  cin >> k;
  for (a = 1; a <= k; a++) {
    for (b = 1; b <= k; b++) {
      for (c = 1; c <= k; c++) {
        sum += gcd(a, b, c);
      }
    }
  }

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

#include <iostream>

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

int gcd3(int a, int b, int c) {
  int d = gcd2(a, b);
  int e = gcd2(c, d);
  return e;
}

int main() {

  int K = 0;
  std::cin >> K;

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

  std::cout << sum << std::endl;

  return 0;
}

Error: -8

Fixed submission:

#include <iostream>

int gcd2(int a, int b) {
  int d = a % b;
  while (d != 0) {
    d = a % b;
    a = b;
    b = d;
  }
  return d;
}

int gcd3(int a, int b, int c) {
  int d = gcd2(a, b);
  int e = gcd2(c, d);
  return e;
}

int main() {

  int K = 0;
  std::cin >> K;

  long Sum = 0;
  for (int i = 1; i <= K; i++) {
    for (int j = 1; j <= K; j++) {
      for (int k = 1; k <= K; k++) {
        Sum += gcd3(i, j, k);
      }
    }
  }

  std::cout << Sum << std::endl;

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

#include <iostream>

using namespace std;

int gcd(int a, int b) {

  if (a % b == 0) {

    return b;
  }

  return gcd(b, a % b);
}

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

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  int kuso = 1;

  for (int p = 1; p < 201; p++) {

    if (a % p == 0 && b % p == 0 && c % p == 0) {

      kuso = p;
    }
  }

  return kuso;
}

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

  cout << sum << endl;

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

#include <iostream>
using namespace std;

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++) {
        for (int d = (min(min(a, b), c)); d >= 1; d--) {
          if (a % d == 0 && b % d == 0 && c % d == 0) {
            sum += d;
            break;
          }
        }
      }
    }
  }

  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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++) {
        for (int d = (max(max(a, b), c)); d >= 1; d--) {
          if (a % d == 0 && b % d == 0 && c % d == 0) {
            sum += d;
            break;
          }
        }
      }
    }
  }

  cout << sum << endl;
}