prithivMLmods/Coder-Stat huggingface dataset:
Problem p02712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long ans = 0;
  int i;
  for (i = 1; 1 <= N; i++) {
    if (i % 3 != 0 && i % 5 != 0) {
      ans += i;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long ans = 0;
  int i;
  for (i = 1; i <= N; i++) {
    if (i % 3 != 0 && i % 5 != 0) {
      ans += i;
    }
  }
  cout << ans << endl;
}
Problem p02712 (Python)

Buggy submission (status: Runtime Error):
N = int(input().split())

print(sum([n + 1 for n in range(N) if (n + 1) % 3 != 0 and (n + 1) % 5 != 0]))


Fixed submission:
N = int(input())

print(sum([n + 1 for n in range(N) if (n + 1) % 3 != 0 and (n + 1) % 5 != 0]))
Problem p02712 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <unordered_set>
#include <utility>
#include <vector>
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define pf printf
#define ff first
#define ss second
#define sef second.first
#define ses second.second
#define ms(a, b) memset(a, b, sizeof(a))
#define loop(i, a) for (int i = 1; i <= a; i++)
#define pii pair<int, int>
#define sl(a) scanf("%I64d", &a)
#define sll(a, b) scanf("%I64d %I64d", &a, &b)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d %d", &a, &b)
using namespace std;
typedef long long ll;
const int maxx = 8e4 + 10;
// typedef pair < int , pii > node ;
// node tree[4*maxx],lazy[4*maxx];
int tree[4 * maxx], lazy[4 * maxx];
int main() {
  ll n, sum = 0, i = 1, k = 0;
  sl(n);
  loop(i, n) {
    if (i % 3 != 0 && i % 5 != 0) {
      sum += i;
    }
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <unordered_set>
#include <utility>
#include <vector>
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define pf printf
#define ff first
#define ss second
#define sef second.first
#define ses second.second
#define ms(a, b) memset(a, b, sizeof(a))
#define loop(i, a) for (int i = 1; i <= a; i++)
#define pii pair<int, int>
#define sl(a) scanf("%I64d", &a)
#define sll(a, b) scanf("%I64d %I64d", &a, &b)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d %d", &a, &b)
using namespace std;
typedef long long ll;
const int maxx = 8e4 + 10;
// typedef pair < int , pii > node ;
// node tree[4*maxx],lazy[4*maxx];
int tree[4 * maxx], lazy[4 * maxx];
int main() {
  long long sum = 0, n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    if (i % 3 != 0 && i % 5 != 0) {
      sum += i;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  long long sum = 0, s[100000];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    if (i % 3 == 0 || i % 5 == 0)
      s[i] = 0;
    else
      s[i] = i;
  }
  for (int i = 1; i <= n; i++)
    sum += s[i];
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, s[1000009];
  long long sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    if (i % 3 == 0 || i % 5 == 0)
      s[i] = 0;
    else
      s[i] = i;
  }
  for (int i = 1; i <= n; i++)
    sum += s[i];
  cout << sum << endl;
}
Problem p02712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

long sum(int n) { return (long)n * (n + 1) / 2; }

int main() {
  int n;
  scanf("%ld", &n);
  printf("%ld\n", sum(n) - sum(n / 3) * 3 - sum(n / 5) * 5 + sum(n / 15) * 15);
}


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

long sum(int n) { return (long)n * (n + 1) / 2; }

int main() {
  int n;
  scanf("%d", &n);
  printf("%ld\n", sum(n) - sum(n / 3) * 3 - sum(n / 5) * 5 + sum(n / 15) * 15);
}
Problem p02712 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long N;
  scanf("%ld", N);
  long ans = 0;
  for (long i = 1; i <= N; i++) {
    if (i % 3 != 0 && i % 5 != 0)
      ans += i;
  }
  printf("%ld", ans);
}


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

int main() {
  long N;
  scanf("%ld", &N);
  long ans = 0;
  for (long i = 1; i <= N; i++) {
    if (i % 3 != 0 && i % 5 != 0)
      ans += i;
  }
  printf("%ld", ans);
}
Problem p02713 (C++)

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

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

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

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

  return 0;
}


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

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

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

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

  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int gcd(int, int);
vector<int> waru(vector<int>);

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

int gcd(int a, int b) {
  vector<int> vec = {a, b};
  while (vec.at(1) != 0) {
    vec = waru(vec);
  }
  return vec.at(0);
}

vector<int> waru(vector<int> i) {
  vector<int> vec(2);
  int syou;
  vec.at(0) = i.at(1);
  syou = i.at(0) / i.at(1);
  vec.at(1) = i.at(0) - syou * i.at(1);
  return vec;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gcd(int, int);
vector<int> waru(vector<int>);

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

int gcd(int a, int b) {
  vector<int> vec = {a, b};
  if (a < b)
    swap(vec.at(0), vec.at(1));
  while (vec.at(1) != 0) {
    vec = waru(vec);
  }
  return vec.at(0);
}

vector<int> waru(vector<int> i) {
  vector<int> vec(2);
  int syou;
  vec.at(0) = i.at(1);
  syou = i.at(0) / i.at(1);
  vec.at(1) = i.at(0) - syou * i.at(1);
  return vec;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

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

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


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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int k;
  cin >> k;
  int x = 0, s = 0;
  for (int l = 1; l <= k; l++) {
    for (int m = 1; m <= k; m++) {
      for (int n = 1; n <= k; n++) {
        for (int i = 1; i <= max(l, max(m, n)); i++) {
          if (l % i == 0 && m % i == 0 && n % i == 0) {
            x = i;
          }
        }
        s += x;
        x = 0;
      }
    }
  }
  cout << s << endl;
}

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

int main() {
  int k;
  cin >> k;
  int x = 0, s = 0;
  for (int l = 1; l <= k; l++) {
    for (int m = 1; m <= k; m++) {
      for (int n = 1; n <= k; n++) {
        for (int i = 1; i <= min(l, min(m, n)); i++) {
          if (l % i == 0 && m % i == 0 && n % i == 0) {
            x = i;
          }
        }
        s += x;
        x = 0;
      }
    }
  }
  cout << s << endl;
}
Problem p02713 (C++)

Buggy submission (status: 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;
}

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;
}


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;

  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;
}
Problem p02713 (C++)

Buggy submission (status: 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 = 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);
}

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 = 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);
}
Problem p02713 (C++)

Buggy submission (status: 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;
}

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 (status: 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 = 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;
}

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 = 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;
}
Problem p02713 (C++)

Buggy submission (status: 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 = 1; b <= k; b++) {
      for (c = 1; c <= k; c++) {
        sum += gcd(a, b, c);
      }
    }
  }

  cout << sum;
  return 0;
}

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 = 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;
}
Problem p02713 (C++)

Buggy submission (status: 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 + 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;
}


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; 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;
}
Problem p02713 (C++)

Buggy submission (status: 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 < c + 1; i++) {
          if (a % i == 0 && b % i == 0 && c % i == 0) {
            gcd = i;
          }
        }
        sum = sum + gcd;
      }
    }
  }
  cout << sum << endl;
  return 0;
}

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 < 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;
}
Problem p02713 (C++)

Buggy submission (status: 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 {
    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;
}

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 {
    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;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

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

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

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

  cin >> k;

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

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

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

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

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

  cin >> k;

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

  return 0;
}
Problem p02713 (C++)

Buggy submission (status: 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;
}

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 (status: Time Limit Exceeded):
#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;
}

Fixed submission:
#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;
}
Problem p02713 (C++)

Buggy submission (status: 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;
}

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 (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> P;
const int INF = 100000000;

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

  int min_num = min(a, min(b, c));
  int base;
  for (int i = 1; min_num + 1; i++) {
    if (min_num % i == 0) {
      base = min_num / i;
    }
    if ((a % base == 0) && (b % base == 0) && (c % base == 0)) {
      return base;
    }
  }
  return 0;
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> P;
const int INF = 100000000;

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

  int min_num = min(a, min(b, c));
  int base;
  for (int i = 1; min_num + 1; i++) {
    if (min_num % i == 0) {
      base = min_num / i;
    }
    if ((a % base == 0) && (b % base == 0) && (c % base == 0)) {
      return base;
    }
  }
  return 0;
}

int main() {
  int K;
  cin >> K;
  int ans = 0;
  for (int i = 1; i <= K; i++) {
    for (int j = i; j <= K; j++) {
      for (int k = j; k <= K; k++) {
        if (i == j && j == k) {
          ans += gcd(i, j, k);
        } else if (i != j && j != k && k != i) {
          ans += gcd(i, j, k) * 6;
        } else {
          ans += gcd(i, j, k) * 3;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>

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

  for (int i = std::max(a, std::max(b, c)); i > 1; i--) {
    if (a % i == 0 && b % i == 0 && c % i == 0)
      return i;
  }

  return 1;
}

int main() {

  int n;

  long long res = 0;

  std::cin >> n;

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

  std::cout << res << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

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

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

  return 1;
}

int main() {

  int n;

  long long res = 0;

  std::cin >> n;

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

  std::cout << res << std::endl;

  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
// Sum of gcd of Tuples (Easy)
#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

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

  cout << sum << endl;

  return 0;
}

Fixed submission:
// Sum of gcd of Tuples (Easy)
#include <algorithm>
#include <complex>
#include <deque>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

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

  cout << sum << endl;

  return 0;
}
Problem p02713 (C++)

Buggy submission (status: 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(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();
  }
}

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(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();
  }
}
Problem p02713 (C++)

Buggy submission (status: 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 {
    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;
}

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 {
    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;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; i <= n; j++) {
      int gcd = __gcd(i, j);
      for (int k = 1; k <= n; k++) {
        sum += __gcd(gcd, k);
      }
    }
  }
  cout << sum;
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int gcd = __gcd(i, j);
      for (int k = 1; k <= n; k++) {
        sum += __gcd(gcd, k);
      }
    }
  }
  cout << sum;
}
Problem p02713 (C++)

Buggy submission (status: 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;
}

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 (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <stdio.h>
#define debug(x) cout << '>' << #x << ':' << x << endl;

#define nitro ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
const int INF = 1e9 + 5;
using namespace std;
using ll = unsigned long long int;
const ll mod = 998244353;
int main() {
  // your code goes here

  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  int k;
  cin >> k;
  ll total = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        int temp = __gcd(j, l);
        total += __gcd(i, temp);
      }
    }
  }
  cout << total;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
#define debug(x) cout << '>' << #x << ':' << x << endl;

#define nitro ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
const int INF = 1e9 + 5;
using namespace std;
using ll = unsigned long long int;
const ll mod = 998244353;
int main() {
  // your code goes here

  // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  int k;
  cin >> k;
  ll total = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int l = 1; l <= k; l++) {
        int temp = __gcd(j, l);
        total += __gcd(i, temp);
      }
    }
  }
  cout << total;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
  freopen("input.txt", "r", stdin);
  // freopen("output.txt","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;

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

  cout << ans << endl;
}


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

typedef long long ll;

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;

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

  cout << ans << endl;
}
Problem p02713 (C++)

Buggy submission (status: 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 = 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;
}


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;
  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 (status: 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() {
  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;
}

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() {
  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 (status: 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;
}
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);
}

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;
  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);
}
Problem p02713 (C++)

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

  cout << sum << endl;
}


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

  cout << sum << endl;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

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

int main() {
  int K;
  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 += gcd3(a, b, c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int gcd(int x, int y) {
  if (x < y) {
    swap(x, y); // 小さい方をyとする
  }
  int r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}

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

int main() {
  int K;
  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 += gcd3(a, b, c);
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define PI 3.141592653589
#define ll long long
using namespace std;

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

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

  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define PI 3.141592653589
#define ll long long
using namespace std;

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

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

  cout << ans;

  return 0;
}
Problem p02713 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02713 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

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