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

#include <iostream>
#include <vector>
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;
  vector<int> A(100000);
  int 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++) {
        A.at(i) = gcd(i, j);
        sum += gcd(A.at(i), k);
      }
    }
  }
  cout << sum << endl;
}


Error: TLE

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() {
  int K;
  cin >> K;

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


Error: TLE

Fixed submission:

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

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

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

/*

Coded and Decoded by : Yash Kapoor

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

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll, ll>
#define vl vector<ll>
#define sl set<ll>
#define vll vector<pll>
#define ml map<ll, ll>
#define mll map<pll, ll>
#define all(a) a.begin(), a.end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define dl '\\n'
// #define why         (ll)1000000007
#define why (ll)998244353
#define lp(i, a, b) for (ll i = a; i < b; ++i)
#define lpr(i, a, b) for (ll i = a; i >= b; i--)
#define lpd(i, x) for (auto i : x)
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll inf = 1e18;
ld pi = 3.141592653589;
ll mod = why;
ll fast_power(ll base, ll power, ll mod) {
  ll result = 1;
  while (power) {
    if (power % 2)
      result = (result * base) % mod;
    base = (base * base) % mod;
    power /= 2;
  }
  return result;
}

ll inverse(ll base, ll mod) { return fast_power(base, mod - 2, mod); }

ll solve() {
  ll k, sum = 0;
  cin >> k;
  lp(i, 1, k + 1) {
    lp(j, 1, k + 1) {
      lp(l, 1, k + 1) { sum += __gcd(i, __gcd(j, l)); }
    }
  }
  cout << sum;
  return 0;
}

int main() {
  ios ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

/*

Coded and Decoded by : Yash Kapoor

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

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define mp make_pair
#define pll pair<ll, ll>
#define vl vector<ll>
#define sl set<ll>
#define vll vector<pll>
#define ml map<ll, ll>
#define mll map<pll, ll>
#define all(a) a.begin(), a.end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define dl '\\n'
// #define why         (ll)1000000007
#define why (ll)998244353
#define lp(i, a, b) for (ll i = a; i < b; ++i)
#define lpr(i, a, b) for (ll i = a; i >= b; i--)
#define lpd(i, x) for (auto i : x)
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll inf = 1e18;
ld pi = 3.141592653589;
ll mod = why;
ll fast_power(ll base, ll power, ll mod) {
  ll result = 1;
  while (power) {
    if (power % 2)
      result = (result * base) % mod;
    base = (base * base) % mod;
    power /= 2;
  }
  return result;
}

ll inverse(ll base, ll mod) { return fast_power(base, mod - 2, mod); }

ll solve() {
  ll k, sum = 0;
  cin >> k;
  lp(i, 1, k + 1) {
    lp(j, 1, k + 1) {
      lp(l, 1, k + 1) { sum += __gcd(i, __gcd(j, l)); }
    }
  }
  cout << sum;
}

int main() {
  ios ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

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


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int gcd(int x, int y) {
  for (int i = max(x, y); i > 0; i--) {
    if (x % i == 0 && y % i == 0)
      return i;
  }
}

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

#include <iostream>

using namespace std;

int euclidean_gcd(int a, int b) {
  if (a < b)
    return euclidean_gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return 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 += euclidean_gcd(euclidean_gcd(a, b), c);
      }
    }
  }

  cout << sum;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

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

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

  cout << sum;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#define il inline
#define re register
#define fo(i, j, k) for (register int i = j; i <= k; i++)
#define do(i, j, k) for (register int i = j; i >= k; i--)
#define inf 0x7fffffff
typedef long long ll;
using namespace std;
il ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return f * x;
}
/*int h[200010],v[200010],dis[200010];
struct edge
{
        int v,w,next;
}e[200010*2];
il void add(int u,int v,int w)
{
        e[++cnt].v=v;
        e[cnt].w=w;
        e[cnt].next=h[u];
        h[u]=cnt;
}*/
ll n, k, ans;
ll dp[100010];
const ll mod = 1e9 + 7;
ll cal(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = (ret * a) % mod;
    a = (a * a) % mod;
    b >>= 1LL;
  }
  return ret;
}
int main() {
  // easy
  n = 3, k = read();
  for (re ll i = k; i >= 1; i--) {
    ll num = k / i;
    dp[i] = cal(num, n);
    for (ll j = 2 * i; j <= k; j += i) {
      dp[i] = (dp[i] - dp[j] + mod) % mod;
    }
    ans = (ans + dp[i] * i) % mod;
  }
  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#define il inline
#define re register
#define fo(i, j, k) for (register int i = j; i <= k; i++)
#define do(i, j, k) for (register int i = j; i >= k; i--)
#define inf 0x7fffffff
typedef long long ll;
using namespace std;
il ll read() {
  ll x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return f * x;
}
/*int h[200010],v[200010],dis[200010];
struct edge
{
        int v,w,next;
}e[200010*2];
il void add(int u,int v,int w)
{
        e[++cnt].v=v;
        e[cnt].w=w;
        e[cnt].next=h[u];
        h[u]=cnt;
}*/
ll n, k, ans;
ll dp[100010];
const ll mod = 1e9 + 7;
ll cal(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = (ret * a) % mod;
    a = (a * a) % mod;
    b >>= 1LL;
  }
  return ret;
}
int main() {
  // easy
  n = read(), k = read();
  for (re ll i = k; i >= 1; i--) {
    ll num = k / i;
    dp[i] = cal(num, n);
    for (ll j = 2 * i; j <= k; j += i) {
      dp[i] = (dp[i] - dp[j] + mod) % mod;
    }
    ans = (ans + dp[i] * i) % mod;
  }
  printf("%lld\\n", ans);
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int K;

int gcd(int a, int b, int c) {
  for (int d = c; d > 0; d--) {
    if (a % d == 0 and b % d == 0 and c % d == 0) {
      return d;
    }
  }
}

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

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int K;

int gcd(int a, int b, int c) {
  for (int d = K; d > 0; d--) {
    if (a % d == 0 and b % d == 0 and c % d == 0) {
      return d;
    }
  }
}

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

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

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

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

int mygcd(int a, int b, int c) {
  if (0 == b % a && 0 == c % a) {
    return a;
  }

  for (int i = a / 2; i >= 1; i--) {
    if (0 == a % i && 0 == b % i && 0 == c % i) {
      return i;
    }
  }
}

int main(void) {
  // Your code here!"
  int sum = 0, k;
  cin >> k;
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; b++) {
      for (int c = 1; c <= k; c++) {
        sum += mygcd(a, b, c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int mygcd(int a, int b, int c) {
  int mx = 1;
  for (int i = 2; i <= 200; i++) {
    if (0 == a % i && 0 == b % i && 0 == c % i) {
      mx = i;
    }
  }
  return mx;
}

int main(void) {
  // Your code here!"
  int sum = 0, k;
  cin >> k;
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; b++) {
      for (int c = 1; c <= k; c++) {
        sum += mygcd(a, b, c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef long double ld;

int MOD = 1000000007;

ll gcd(ll N, ll M, ll L) {
  while (M != N)
    if (M > N)
      M = M - N;
    else
      N = N - M;

  while (M != L)
    if (M > L)
      M = M - L;
    else
      L = L - M;

  return L;
}

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


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;

int MOD = 1000000007;

ll gcd(ll N, ll M, ll L) {
  while (M != N)
    if (M > N)
      M = M - N;
    else
      N = N - M;

  while (M != L)
    if (M > L)
      M = M - L;
    else
      L = L - M;
}

int main() {
  ll K, sum = 0;
  cin >> K;
  for (ll i = 1; i <= K; i++) {
    for (ll j = 1; j <= K; j++) {
      for (ll k = 1; k <= K; k++) {
        sum += 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 (b % a == 0) {
    return a;
  } else {
    return gcd(b % a, a);
  }
}

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int gcd(int a, int b) {
  if (a < b) {
    a = a + b;
    b = a - b;
    a = a - b;
  }
  if (b == a) {
    return b;
  } else if (b == 1) {
    return 1;
  } else {
    return gcd(a - b, b);
  }
}

int main() {
  int k;
  long long sum = 0;
  for (int a = 1; a <= k; a++) {
    sum += a; // gcd(a,a,a)
    for (int d = 1; d <= k - a; d++) {
      sum += 3 * gcd(a, d);
      for (int f = 1; f <= k - a - d; f++) {
        sum += 6 * gcd(a, gcd(d, f));
      }
    }
  }
  cout << sum << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

int main() {
  int n;
  cin >> n;
  map<multiset<int>, pair<int, int>> M;
  multiset<int> mset;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        mset.clear();
        mset.insert(i);
        mset.insert(j);
        mset.insert(k);
        if (M.find(mset) == M.end()) {
          // Nuevo
          M[mset] = make_pair(gcd(i, j, k), gcd(i, j, k));
        } else {
          M[mset].second += M[mset].first;
        }
      }
    }
  }
  int res = 0;
  for (auto i : M)
    res += (i.second).second;
  cout << res;

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

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

using namespace std;
int main() {
  long long a;
  cin >> a;

  int ans = 0;
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= a; j++) {
      for (int k = 1; k <= a; k++) {
        ans += gcd(gcd(i, j), gcd(j, k));
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;
int main() {
  long long a;
  cin >> a;

  int b, c, d;
  int ans = 0;

  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= a; j++) {
      for (int k = 1; k <= a; k++) {
        b = i;
        c = j;
        d = k;
        int tmp = max({b, c, d});
        for (int f = tmp; f > 0; f--) {
          if (b % f == 0 && c % f == 0 && d % f == 0) {
            ans += f;
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int k, min_;
  int sum = 0, mul = 1;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        int i1 = i;
        int j1 = j;
        int l1 = l;
        mul = 1;

        for (int m = 2; m <= max(max(i1, j1), l1);) {
          if (i1 % m == 0 && j1 % m == 0 && l1 % m == 0) {
            mul = mul * m;
            i1 = i1 / m;
            j1 = j1 / m;
            l1 = l1 / m;
          } else
            m++;
          if (i1 < m || j1 < m || l1 < m)
            break; // この工夫がないとACされない
        }
        sum += mul;
      }
    }
  }

  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int k, min_;
  int sum = 0, mul = 1;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        int i1 = i;
        int j1 = j;
        int l1 = l;
        mul = 1;

        for (int m = 2; m <= max(max(i1, j1), l1);) {
          if (i1 % m == 0 && j1 % m == 0 && l1 % m == 0) {
            mul = mul * m;
            i1 = i1 / m;
            j1 = j1 / m;
            l1 = l1 / m;
          } else
            m++;
          // if( i1 < m || j1 < m || l1 < m ) break; //
          // この工夫がないとACされない
        }
        sum += mul;
      }
    }
  }

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

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int k, min_;
  int sum = 0, mul = 1;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        int i1 = i;
        int j1 = j;
        int l1 = l;
        mul = 1;

        for (int m = 2; m <= k;) {
          if (i1 % m == 0 && j1 % m == 0 && l1 % m == 0) {
            mul = mul * m;
            i1 = i1 / m;
            j1 = j1 / m;
            l1 = l1 / m;
          } else
            m++;
          if (i1 < m)
            break;
        }
        sum += mul;
      }
    }
  }

  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int k, min_;
  int sum = 0, mul = 1;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        int i1 = i;
        int j1 = j;
        int l1 = l;
        mul = 1;

        for (int m = 2; m <= k;) {
          if (i1 % m == 0 && j1 % m == 0 && l1 % m == 0) {
            mul = mul * m;
            i1 = i1 / m;
            j1 = j1 / m;
            l1 = l1 / m;
          } else
            m++;
          // if( i1 < m ) break;
        }
        sum += mul;
      }
    }
  }

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

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int k, min_;
  int sum = 0, mul = 1;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        int i1 = i;
        int j1 = j;
        int l1 = l;
        mul = 1;

        for (int m = 2; m <= k;) {
          if (i1 % m == 0 && j1 % m == 0 && l1 % m == 0) {
            mul = mul * m;
            i1 = i1 / m;
            j1 = j1 / m;
            l1 = l1 / m;
            // cout << i1 << j1 << l1 << min1;
          } else
            m++;
          if (i1 < m)
            break;
        }
        sum += mul;
      }
    }
  }

  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int k, min_;
  long long int sum = 0, mul = 1;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        int i1 = i;
        int j1 = j;
        int l1 = l;
        mul = 1;

        for (int m = 2; m <= k;) {
          if (i1 % m == 0 && j1 % m == 0 && l1 % m == 0) {
            mul = mul * m;
            i1 = i1 / m;
            j1 = j1 / m;
            l1 = l1 / m;
            // cout << i1 << j1 << l1 << min1;
          } else
            m++;
        }
        sum += mul;
      }
    }
  }

  cout << sum << 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;
  } else {
    return gcd(b, a % b);
  }
}

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int max(int a, int b, int c) {
  int max = 0;
  max = a;
  if (max < b) {
    max = b;
  }
  if (max < c) {
    max = c;
  }
  return max;
}

int gcd(int a, int b, int c) {
  int lim = 0, ans = 0, cnt = 0, i;
  lim = max(a, b, c);
  for (i = lim; i >= 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0) {
      ans = i;
      cnt++;
    }
    if (cnt > 0) {
      break;
    }
  }
  return ans;
}

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

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.141592653589793
// vector < vector<int>>a(0, vector<int>(0));二次元配列宣言

int main() {
  long long int sum = 0;
  int i, j, h, g;
  int k;
  cin >> k;
  for (i = 1; i <= k; i++) {
    for (j = 1; j <= k; j++) {
      for (h = 1; h <= k; h++) {
        for (g = min(min(i, j), min(j, h)); g >= 1; g--) {
          if (i % g == 0 && j % g == 0 && h % g == 0) {
            sum += g;
            break;
          }
        }
      }
    }
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.141592653589793
// vector < vector<int>>a(0, vector<int>(0));二次元配列宣言

int main() {
  long long int sum = 0;
  int i, j, h, g;
  int k;
  cin >> k;
  for (i = 1; i <= k; i++) {
    for (j = 1; j <= k; j++) {
      for (h = 1; h <= k; h++) {
        for (g = max(max(i, j), max(j, h)); g >= 1; g--) {
          if (i % g == 0 && j % g == 0 && h % g == 0) {
            sum += g;
            break;
          }
        }
      }
    }
  }
  cout << sum << endl;
}