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

#include <bits/stdc++.h>

using namespace std;

int main() {

  long int n, ans = 0, num = 0;
  long long int sum = 0;
  scanf("%ld", &n);
  long int i, j, k;
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      for (k = 1; k <= n; ++k) {
        num = __gcd(i, j);
        ans = __gcd(k, num);
        sum += ans;
      }
    }
  }

  cout << sum;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  long int n, ans = 0, num = 0;
  long long int sum = 0;
  scanf("%d", &n);
  long int i, j, k;
  for (i = 1; i <= n; ++i) {

    for (j = 1; j <= n; ++j) {
      for (k = 1; k <= n; ++k) {
        if (i == 1 || j == 1 || k == 1)
          sum += 1;

        else {
          num = __gcd(i, j);
          ans = __gcd(k, num);
          sum += ans;
        }
      }
    }
  }

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

#include <bits/stdc++.h>
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;
  int sum = 0;
  cin >> K;
  for (int a = 1; a <= K; a++) {
    for (int b = 1; b <= K; b++) {
      for (int c = 1; c <= K; c++) {
        sum += gcd(a, gcd(b, c));
      }
    }
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b, int c) { // これさえいければいける
  int N = 1;
  int K = max(a, max(b, c));
  for (int i = K; K >= 1; K--) {
    if (a % K == 0 && b % K == 0 && c % K == 0) {
      N = K;
      break;
    }
  }
  return N;
}
int main() {
  int K;
  int sum = 0;
  cin >> K;
  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: Runtime Error

#include <iostream>

using namespace std;
const int N = 210;
int dp[N][N];

int gcd(int a, int b) {
  if (dp[a][b])
    return dp[a][b];
  if (dp[b][a]) {
    dp[a][b] = dp[b][a];
    return dp[a][b];
  } else {
    if (a % b == 0) {
      dp[a][b] = b;
      dp[b][a] = b;
      return b;
    } else {
      int aaaa = gcd(b, a - (a / b) * b);
      dp[a][b] = aaaa;
      dp[b][a] = aaaa;
      return aaaa;
    }
  }
}

int main() {
  int k;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= i; j++) {
      gcd(i, j);
    }
  }
  long long res = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int m = 1; m <= k; m++) {
        res += dp[dp[i][j]][dp[j][m]];
      }
    }
  }
  cout << res;
}

Error: -8

Fixed submission:

#include <iostream>

using namespace std;
const int N = 210;
int dp[N][N];

int gcd(int a, int b) {
  if (dp[a][b])
    return dp[a][b];
  if (dp[b][a]) {
    dp[a][b] = dp[b][a];
    return dp[a][b];
  } else {
    int aaaa = gcd(b, a - a / b * b);
    dp[a][b] = aaaa;
    dp[b][a] = aaaa;
    return aaaa;
  }
}

int main() {
  int k;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= i; j++) {
      gcd(i, j);
    }
  }
  long long res = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int m = 1; m <= k; m++) {
        res += dp[dp[i][j]][dp[j][m]];
      }
    }
  }
  cout << res;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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


Error: TLE

Fixed submission:

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

int main() {
  int M, K;
  int count = 0;
  cin >> K;
  K++;
  for (int i = 1; i < K; i++) {
    for (int j = 1; j < K; j++) {
      for (int k = 1; k < K; k++) {
        M = max(i, max(j, k));
        for (int l = M; l > 0; l--) {
          if (i % l == 0 && j % l == 0 && k % l == 0) {
            count += l;
            break;
          }
        }
      }
    }
  }
  cout << count << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int gcd(int a, int b, int c) {
  while (a % b != 0) {
    int a_hold = a;
    a = b;
    b = a_hold % b;
  }
  while (b % c != 0) {
    int b_hold = b;
    b = c;
    c = b_hold % c;
  }
  return 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;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

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


Error: -8

Fixed submission:

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

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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MOD = 1e9 + 7;

ll get(ll x, ll y) {
  ll res = 1ll;
  while (y) {
    if (y & 1) {
      res = (res * x) % MOD;
    }
    y >>= 1;
    x = (x * x) % MOD;
  }
  return res;
}

void solve() {
  int n, k;
  n = 3;
  cin >> k;
  vector<ll> cur(k + 1);
  for (int i = 1; i <= k; i++) {
    cur[i] = get((k / i), n);
  }
  for (int i = k; i >= 1; i--) {
    for (int j = i + i; j <= k; j += i) {
      cur[i] -= cur[j];
      cur[i] %= MOD;
      if (cur[i] < 0)
        cur[i] += MOD;
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= k; i++) {
    // cout << cur[i] << '\\n';
    ans = (ans + i * cur[i]) % MOD;
  }
  cout << ans << '\\n';
}

int main() {
  int t;
  t = 1;
  for (int cs = 1; cs <= t; cs++) {
    solve();
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MOD = 1e9 + 7;

ll get(ll x, ll y) {
  ll res = 1ll;
  while (y) {
    if (y & 1) {
      res = (res * x) % MOD;
    }
    y >>= 1;
    x = (x * x) % MOD;
  }
  return res;
}

void solve() {
  int n, k;
  n = 3;
  cin >> k;
  vector<ll> cur(n + 1);
  for (int i = 1; i <= k; i++) {
    cur[i] = get((k / i), n);
  }
  for (int i = k; i >= 1; i--) {
    for (int j = i + i; j <= k; j += i) {
      cur[i] -= cur[j];
      cur[i] %= MOD;
      if (cur[i] < 0)
        cur[i] += MOD;
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= k; i++) {
    // cout << cur[i] << '\\n';
    ans = (ans + i * cur[i]) % MOD;
  }
  cout << ans << '\\n';
}

int main() {
  int t;
  t = 1;
  for (int cs = 1; cs <= t; cs++) {
    solve();
  }
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <iostream>  // cout, endl, cin
#include <math.h>    // sqrt
#include <string>    // string, to_string, stoi
#include <vector>    // vector
using namespace std;

int gcd(int i, int j, int k) {
  int div;
  while (true) {
    if (i == j && j == k) {
      break;
    }
    div = min({i, j, k});
    if (i != div) {
      i = i % div + div;
      if (div < i) {
        i -= div;
      }
    }
    if (j != div) {
      j = j % div + div;
      if (div < j) {
        j -= div;
      }
    }
    if (k != div) {
      k = k % div + div;
      if (div < k) {
        k -= div;
      }
    }
  }
  return i;
}

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

Error: TLE

Fixed submission:

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

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

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

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

int main() {
  ll k;
  cin >> k;
  ll answer = 0;
  for (int a = 1; a <= k; a++) {
    for (int b = a; b <= k; b++) {
      for (int c = b; c <= k; c++) {
        for (int d = a; d >= 1; d--) {
          if (a % d == 0 && b % d == 0 && c % d == 0) {
            if (a == b && b == c) {
              answer += d;
            } else if (a != b && b != c && c != a) {
              answer += d * 6;
            } else {
              answer += d * 3;
            }
            break;
          }
        }
      }
    }
  }
  cout << answer << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll k;
  cin >> k;
  ll answer = 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 = a; d >= 1; d--) {
          if (a % d == 0 && b % d == 0 && c % d == 0) {
            answer += d;
            break;
          }
        }
      }
    }
  }
  cout << answer << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int a, b, c, result;
  long sum = 0;
  cin >> a;
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= a; j++) {
      for (int k = 1; k <= a; k++) {
        sum += gcd(gcd(i, j), k);
      }
    }
  }
  cout << sum;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int greatest_num(int a, int b, int c) {
  if (a >= b && a >= c) {
    return a;
  }

  else if (b >= a && b >= c) {
    return b;
  }

  else if (c >= a && c >= b) {
    return c;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int a, b, c, result;
  long sum = 0;
  cin >> a;
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= a; j++) {
      for (int k = 1; k <= a; k++) {
        for (result = greatest_num(i, j, k); result >= 1; result--) {
          if (i % result == 0 && j % result == 0 && k % result == 0) {
            sum += result;
            break;
          }
        }
      }
    }
  }
  cout << sum;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int matriz[201][201][201];
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

long long int bksum(int a, int b, int c, int k) {
  if (a > k || b > k || c > k)
    return 0;
  if (matriz[a][b][c])
    return 0;
  matriz[a][b][c]++;
  return gcd(a, gcd(b, c)) + bksum(a + 1, b, c, k) + bksum(a, b + 1, c, k) +
         bksum(a, b, c + 1, k);
}

int main() {
  long long int sum = 0;
  int k;
  cin >> k;
  sum = bksum(1, 1, 1, k);
  printf("%lld\\n", sum);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
set<pair<pair<int, int>, int>> checked;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

long long int bksum(int a, int b, int c, int k) {
  if (a > k || b > k || c > k)
    return 0;
  pair<pair<int, int>, int> p;
  p = make_pair(make_pair(a, b), c);
  if (checked.count(p))
    return 0;
  checked.insert(p);
  return gcd(a, gcd(b, c)) + bksum(a + 1, b, c, k) + bksum(a, b + 1, c, k) +
         bksum(a, b, c + 1, k);
}

int main() {
  long long int sum = 0;
  int k;
  cin >> k;
  sum = bksum(1, 1, 1, k);
  printf("%lld\\n", sum);
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

Error: -8

Fixed submission:

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

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

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

  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, j), k);
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll gcd(ll a, ll b, ll c) {
  ll i = 0;
  ll n = 0;
  n = min({a, b, c});
  rep(i, n) {
    if (a % (n - i) == 0 && b % (n - i) == 0 && c % (n - i) == 0) {
      return n - i;
    }
  }
  return 1;
}
int main() {
  ll n, i, j, k;
  cin >> n;
  ll s1 = 0;
  rep(i, n) rep(j, n) rep(k, n) {
    if (i == j && j == k)
      s1 += i + 1;
    if (i > j && j > k)
      s1 += 6 * gcd(i + 1, j + 1, k + 1);
    if (i > j && j == k)
      s1 += 3 * gcd(i + 1, j + 1, k + 1);
    if (i == j && j > k)
      s1 += 3 * gcd(i + 1, j + 1, k + 1);
  }
  cout << s1;

  return 0;
}

Error: TLE

Fixed submission:

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

ll gcd(ll a, ll b, ll c) {
  ll i = 0;
  ll n = 0;
  n = min({a, b, c});
  rep(i, n) {
    if (a % (n - i) == 0 && b % (n - i) == 0 && c % (n - i) == 0) {
      return n - i;
    }
  }
  return 1;
}
int main() {
  ll n, i, j, k;
  cin >> n;
  ll s1 = 0;
  rep(i, n) rep(j, n) rep(k, n) s1 += gcd(i + 1, j + 1, k + 1);
  cout << s1;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return res;
}
ll inv(ll x) { return power(x, mod - 2); }
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
int main() {
  ll a, b, c, i, j, k;
  cin >> a;
  ll sum = 0;
  for (i = 1; i <= a; i++) {
    for (j = 1; j <= a; j++) {
      for (k = 1; k <= a; k++) {
        sum += gcd(gcd(i, j), k);
      }
    }
  }
  cout << sum;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return res;
}
ll inv(ll x) { return power(x, mod - 2); }
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
int main() {
  ll a, b, c, i, j, k;
  cin >> a >> b >> c;
  ll sum = 0;
  for (i = 1; i <= a; i++) {
    for (j = 1; j <= b; j++) {
      for (k = 1; k <= c; k++) {
        sum += gcd(gcd(i, j), k);
      }
    }
  }
  cout << sum;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vpi = vector<pii>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpl = vector<pll>;
using vs = vector<string>;

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

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vpi = vector<pii>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpl = vector<pll>;
using vs = vector<string>;

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

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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

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

using namespace std;

#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e9 + 7;

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

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

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

using namespace std;

#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e9 + 7;

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

int main() {
  int k;
  cin >> k;
  ll sum = 0;
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; k++) {
      // int g = gcd(a,b);
      for (int c = 1; c <= k; c++) {
        sum += gcd(gcd(a, b), c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

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

  long int sum;
  int a, b, c;

  sum = 0;
  for (int i = 1; i <= K; ++i) {
    for (int j = 1; j <= K; ++j) {
      for (int k = 1; k <= K; ++k) {
        c = 1;
        a = i;
        b = j;
        while (c != 0) {
          c = a % b;
          a = b;
          b = c;
          // cout<<"c1="<<c<<endl;
        }
        c = 1;
        b = a;
        a = k;
        while (c != 0) {
          c = a % b;
          a = b;
          b = c;
          // cout<<"c2="<<c<<endl;
        }
        // cout<<i<<" "<<j<<" "<<k<<" b="<<b<<endl;
        sum = sum + a;
      }
    }
  }

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

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

  long int sum;
  int a, b, c;

  sum = 0;
  for (int i = 1; i <= K; ++i) {
    for (int j = 1; j <= K; ++j) {
      for (int k = 1; k <= K; ++k) {
        c = 1;
        while (c != 0) {
          c = a % b;
          a = b;
          b = c;
        }
        c = 1;
        while (c != 0) {
          c = a % b;
          if (c == 0)
            break;
          a = b;
          b = c;
        }
        // cout<<i<<" "<<j<<" "<<k<<" b="<<b<<endl;
        sum = sum + b;
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 1; i < (int)(n + 1); i++)
const int INF = 1e9;
const int MOD = 1e9 + 7;

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

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


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1e9;
const int MOD = 1e9 + 7;

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

int main() {
  int K;
  int ans = 0;
  cin >> K;
  rep(a, K) {
    rep(b, K) {
      rep(c, K) { ans += gcd(gcd(a, b), c); }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  int ans = 0;
  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 << endl;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
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, ans;
  cin >> k;

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