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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

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

int main() {
  long long int n;
  cin >> n;
  cout << sum(n) + 15 * sum(n / 15) - 3 * sum(n / 3) - 5 * sum(n / 5);
  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>

int main() {
  int N;
  int ctr = 0;
  scanf("%lld", &N);
  for (int i = 1; i <= N; i++) {
    if (i % 3 && i % 5 == 0) {
      continue;
    } else if (i % 5 == 0) {
      continue;
    } else if (i % 3 == 0) {
      continue;
    } else {
      ctr += i;
    }
  }
  printf("%lld", ctr);
  return 0;
}
Problem p02712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main() {
  long long int N;
  long long int sum = 0;
  scanf("%lld", &N);

  for (int i = 0; i <= N; i++) {
    if (i % 3 == 0 || i % 5 == 0) {
      continue;
    } else {
      sum += i;
    }
  }

  printf("%lld", sum);

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  // input angka
  // angka diurai dr 1 ampe angka
  // klo ada angka yang bisa dibagi 3 dan 5. fizzbuzz
  // ada angka yang dibagi 3, Fizz
  // angka yg dibagi 5 buzz
  // yg gk bisa 33 nya, jd angka
  // itung yg bukan fizz,buzz, fizzbuzz
  int n, nothing = 0, i, sum = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    if (i % 3 == 0) {
      i = nothing;
    } else if (i % 5 == 0) {
      i = nothing;
    } else if (i % 15 == 0) {
      i = nothing;
    }
    sum += i;
  }

  printf("%d", sum);
  return 0;
}
Problem p02712 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

int main() {
  long long int n, total = 0;
  scanf("%lld", &n);

  for (int i = 1; i <= n; i++) {
    if (i % 3 != 0 && i % 5 != 0) {
      total = total + i;
    }
  }

  printf("%lld", total);

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int main() {
  int N;
  int ctr = 0;
  scanf("%lld", &N);
  for (int i = 1; i <= N; i++) {
    if (i % 3 && i % 5 != 0) {
      ctr += i;
    }
  }
  printf("%lld\\n", ctr);
  return 0;
}
Problem p02712 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

using namespace std;
typedef long long ll;

int main() {
  int n;
  ll sum = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    if (i % 3 != 0 && i % 5 != 0)
      sum += i;
  }
  printf("%lld", sum);
}

Error: WA

Fixed submission:

#include <cstdio>

using namespace std;
typedef long long ll;

int main() {
  int n;
  ll sum = 0;
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    if (i % 3 != 0 && i % 5 != 0)
      sum += i;
  }
  printf("%lld", sum);
}
Problem p02712 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

  for (int num = 1; num <= n; num++) {
    if (num % 5 == 0 && num % 3 == 0)
      continue;
    else if (num % 5 == 0)
      continue;
    else if (num % 3 == 0)
      continue;
    else
      sum += num;
  }
  cout << sum;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  while (c != n) {
    if (num % 5 == 0 && num % 3 == 0)
      continue;
    else if (num % 5 == 0)
      continue;
    else if (num % 3 == 0)
      continue;
    else {
      sum += num;
      num++;
      c++;
    }
  }

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

//---------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
//---------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define SIZE 100005
#define INF 1e18
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define vec vector
using pint = pair<int, int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
//---------------------------------------------------------------
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;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const double pi = acos(-1);
//---------------------------------------------------------------

// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

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

  ll n;
  cin >> n;
  ll ans = 0;
  rep1(i, n) {
    if (i % 3 == 0 || i % 5 == 0)
      continue;
    ans += i;
  }
  cout << ans << endl;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 15) >= this->size() (which is 15)


Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> vec(N, 0);

  for (int i = 1; i <= N; i++) {

    if (i % 3 == 0 || i % 5 == 0) {

    } else {
      vec.erase(vec.begin() + i);
      vec.insert(vec.begin() + i, i);
    }
  }
  int sum = 0;
  for (int j = 0; j <= N; j++) {
    sum += vec.at(j);
  }
  cout << sum << endl;
}
Problem p02712 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  int64_t ans = 0;

  for (int i = 0; i < N; i++) {
    if (!((i + 1) % 3 == 0 || (i + 1) % 5 == 0)) {
      ans += (i + 1);
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  int ans = 0;
  int i = 1;

  while (i <= N) {
    if (!(i % 3 == 0 || i % 5 == 0)) {
      ans += i;
      i++;
    }
  }
  cout << ans << endl;
}
Problem p02712 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  long long sm = 0;

  for (int i = 1; i <= n; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
    } else if (i % 3 == 0) {
    } else if (i % 5 == 0) {
    } else {
      sm = sm + i;
    }
  }
  cout << sm << endl;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 15) >= this->size() (which is 15)


Fixed submission:

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

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

  vector<int> FizzBuzz(N);
  for (int i = 1; i < N; i++) {
    cin >> FizzBuzz.at(i);
  }

  int answer = 0;

  for (int i = 1; i < N; i++) {
    if (FizzBuzz.at(N) % 15 == 0) {
      answer++;
    }
  }

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

#include <stdio.h>

int main() {
  long long int number;
  scanf("%lld", &number);
  long long int total = 0;
  for (int i = 1; i <= number; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
      continue;
    } else if (i % 3 == 0) {
      continue;
    } else if (i % 5 == 0) {
      continue;
    } else {
      total += i;
    }
  }
  printf("%lld", total);
  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>

int main() {
  int test;
  scanf("%lld", &test);
  int fizzbuzz = 0;
  long long int jumlah = 0;
  for (int i = 0; i < test; i++) {
    if (i % 3 == 0 && i % 5 == 0) {
      fizzbuzz += i;
    } else if (i % 3 == 0 || i % 5 == 0) {
      fizzbuzz += i;
    } else {
      jumlah += i;
    }
  }
  printf("%lld", jumlah);
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: 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; ++h) {
        ans += gcd(gcd(i, j), h);
      }
    }
  }

  cout << ans;

  return 0;
}

Error: TLE

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

  cout << ans;

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

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

int gcd(int a, int b, int c) {
  int min = max(a, max(b, c));
  int n = 1;
  for (int i = 0; i < min; i++) {
    if (a % (i + 1) == 0 && b % (i + 1) == 0 && c % (i + 1) == 0)
      n = i + 1;
  }
  return n;
}
int main() {
  int K;
  cin >> K;
  int count = 0;
  for (int k = 0; k < K; k++) {
    for (int j = 0; j < k; j++) {
      for (int i = 0; i < j; i++) {
        count += gcd(i + 1, j + 1, k + 1);
      }
    }
  }
  count *= 6;
  int count_1 = 0;
  for (int k = 0; k < K; k++) {
    for (int j = 0; j < k; j++) {
      count_1 += gcd(j + 1, j + 1, k + 1);
    }
  }
  for (int k = 0; k < K; k++) {
    for (int j = 0; j < k; j++) {
      count_1 += gcd(j + 1, k + 1, k + 1);
    }
  }
  count_1 *= 3;
  count += count_1;
  int count_2 = 0;
  for (int k = 0; k < K; k++) {
    count_2 += gcd(k + 1, k + 1, k + 1);
  }
  count += count_2;
  cout << count << endl;
}

Error: TLE

Fixed submission:

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

int gcd(int a, int b, int c) {
  int min = max(a, max(b, c));
  int n = 1;
  for (int i = 0; i < min; i++) {
    if (a % (i + 1) == 0 && b % (i + 1) == 0 && c % (i + 1) == 0)
      n = i + 1;
  }
  return n;
}
int main() {
  int K;
  cin >> K;
  int count = 0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      for (int k = 0; k < K; k++) {
        count += gcd(i + 1, j + 1, k + 1);
      }
    }
  }
  cout << count << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Runtime Error

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

int64_t beki(int64_t n, int64_t x) {
  int64_t s, ans;
  s = (n * n) % 1000000007;
  ans = (s * n) % 1000000007;
  return ans;
}

int main() {
  int64_t n, k, a;
  a = 0;
  n = 3;
  cin >> k;
  vector<int64_t> ans(k);
  ans.at(k - 1) = 1;
  for (int64_t i = k - 2; i >= 0; i--) {
    ans.at(i) = beki(k / (i + 1), 3);
    for (int64_t j = 1; (i + 1) * j <= k; j++) {
      ans.at(i) = ans.at(i) - ans.at((i + 1) * j - 1);
    }
    ans.at(i) = ans.at(i) + beki(k / (i + 1), 3);
  }
  for (int64_t i = 0; i < k; i++) {
    a = a + (i + 1) * ans.at(i);
    while (a >= 1000000007) {
      a = a - 1000000007;
    }
  }
  cout << a << endl;
}

Error: 0

Fixed submission:

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

int64_t beki(int64_t n) {
  int64_t s, ans;
  s = (n * n) % 1000000007;
  ans = (s * n) % 1000000007;

  return ans;
}
int main() {
  int64_t k, a;
  a = 0;
  cin >> k;
  vector<int64_t> ans(k);
  ans.at(k - 1) = 1;
  for (int64_t i = 1; i >= 0; i--) {
    ans.at(i) = beki(k / (i + 1));
    for (int64_t j = 1; (i + 1) * j <= k; j++) {
      ans.at(i) = ans.at(i) - ans.at((i + 1) * j - 1);
      if (ans.at(i) < 0) {
        ans.at(i) = ans.at(i) + 1000000007;
      }
    }
    ans.at(i) = ans.at(i) + beki(k / (i + 1));
  }
  for (int64_t i = 0; i < k; i++) {
    a = a + (i + 1) * ans.at(i);
    while (a >= 1000000007) {
      a = a - 1000000007;
    }
  }
  cout << a << endl;
}
Problem p02713 (C++) — buggy submission, verdict: 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;
}


Error: TLE

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, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, a, b) for (auto(i) = a; i < b; i++)
#define list(i, N) for (auto(i) = 0; i < N; i++)

typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
#define mp make_pair
#define pb push_back
#define eb emplace_back

#define int ll
#define INF 1e18 + 5
#define mod 1000000007

template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); }

int k;

int gcd(int i, int j, int k) { return __gcd(i, __gcd(j, k)); }

#define K 205

int dp[K + 1][K + 1][K + 1];

int32_t main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  cin >> k;
  int ans = 0;

  rep(i, 1, k + 1) {
    rep(j, 1, k + 1) { rep(m, 1, k + 1) dp[i][j][m] = -1; }
  }

  rep(i, 1, k + 1) {
    rep(j, 1, k + 1) {
      rep(m, 1, k + 1) {
        // find the number of permutations this can take??
        if (dp[i][j][m] != -1)
          ans += dp[i][j][m];
        else {
          int cur = gcd(i, j, m);
          dp[i][j][m] = cur;
          dp[m][j][i] = cur;
          dp[i][m][j] = cur;
          dp[m][i][j] = cur;
          dp[j][i][m] = cur;
          dp[j][m][i] = cur;
          ans += dp[i][j][m];
        }
      }
    }
  }

  cout << ans << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, a, b) for (auto(i) = a; i < b; i++)
#define list(i, N) for (auto(i) = 0; i < N; i++)

typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
#define mp make_pair
#define pb push_back
#define eb emplace_back

#define int ll
#define INF 1e18 + 5
#define mod 1000000007

template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); }

int k;

int gcd(int i, int j, int k) { return __gcd(i, __gcd(j, k)); }

int32_t main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  cin >> k;
  int ans = 0;

  rep(i, 1, k + 1) {
    rep(j, 1, k + 1) {
      rep(k, 1, k + 1) {
        // find the number of permutations this can take??
        ans += gcd(i, j, k);
      }
    }
  }

  cout << ans << "\\n";

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

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 1; i < (long long)(n); i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

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

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

  rep(i, n + 1) {
    rep(j, n + 1) {
      rep(k, n + 1) { sum += e_gcd(e_gcd(i, j), k); }
    }
  }
  cout << sum;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 1; i < (long long)(n); i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

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

int main() {
  ll n;
  ll sum = 0;
  cin >> n;
  map<T, ll> a;

  rep(i, n + 1) {
    rep(j, n + 1) {
      rep(k, n + 1) {
        vector<ll> b = {i, j, k};
        sort(b.begin(), b.end());
        if (a[make_tuple(b[0], b[1], b[2])] == 0) {
          a[make_tuple(b[0], b[1], b[2])] = e_gcd(e_gcd(b[0], b[1]), b[2]);
        }
        sum += a[make_tuple(b[0], b[1], b[2])];
      }
    }
  }
  cout << sum;
}
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;
  long long ans = 0;
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; b++) {
      for (int c = 1; c <= k; c++) {
        long long A = a, B = b, C = c;
        int os = gcd(A, B);
        ans += gcd(os, C);
      }
    }
  }
  cout << ans;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;
  long long ans = 0;
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; b++) {
      for (int c = 1; c <= k; c++) {
        long long A = a, B = b, C = c;
        while (B % A != 0) {
          if (A <= B)
            swap(A, B);
          A %= B;
        }
        while (C % A != 0) {
          if (A <= C)
            swap(A, C);
          A %= C;
        }
        ans += A;
      }
    }
  }
  cout << ans;
}
Problem p02713 (C++) — buggy submission, verdict: 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;
}

Error: TLE

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, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<vi> vivi;
typedef vector<ll> vll;
typedef vector<vll> vllvll;
typedef pair<int, int> Pi;
typedef queue<Pi> QPi;
typedef pair<ll, ll> Pll;
typedef queue<Pll> QPll;
using Graph = vector<vector<int>>;
const int mod = 1000000007;
// 小数点　<< fixed << setprecision(0) <<
// sort降順　sort(ALL(),greater<int>());
// 文字判定　isupper islower
// 順列　do {} while(next_permutation(ALL(X)));
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct edge {
  int from; // 出発点
  int to;   // 到達点
  int cost; // 移動コスト
};
typedef struct edge se;
typedef vector<edge> ve;
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, temp;
  cin >> k;
  ll ans = 0;
  vi ab(k + 1, 0);
  for (int a = 1; a <= k; a++) {
    for (int b = 1; b <= k; b++) {
      temp = euclidean_gcd(a, b);
      ab[temp]++;
    }
  }
  for (int c = 1; c <= k; c++) {
    for (int d = 1; d <= k; d++) {
      ans += euclidean_gcd(c, d) * ab[d];
    }
  }
  cout << ans << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<vi> vivi;
typedef vector<ll> vll;
typedef vector<vll> vllvll;
typedef pair<int, int> Pi;
typedef queue<Pi> QPi;
typedef pair<ll, ll> Pll;
typedef queue<Pll> QPll;
using Graph = vector<vector<int>>;
const int mod = 1000000007;
// 小数点　<< fixed << setprecision(0) <<
// sort降順　sort(ALL(),greater<int>());
// 文字判定　isupper islower
// 順列　do {} while(next_permutation(ALL(X)));
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct edge {
  int from; // 出発点
  int to;   // 到達点
  int cost; // 移動コスト
};
typedef struct edge se;
typedef vector<edge> ve;
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;
  cin >> k;
  ll ans = 0;
  vi ab(k + 1, 0);
  for (int a = 1; a <= k; a++) {
    for (int b = 0; b <= k; b++) {
      ab[euclidean_gcd(a, b)]++;
    }
  }
  for (int c = 0; c <= k; c++) {
    REP(i, k) { ans += euclidean_gcd(c, ab[i + 1]); }
  }
  cout << ans << endl;
  return 0;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int gcd(int a, int b) {

  if (a % b == 0)
    return b;

  return gcd(b, a % b);
}

int main() {
  int k;
  cin >> k;
  int tmp, tmp2;
  int ans = 0;

  rep(i, k) {
    rep(j, k) {

      tmp = gcd(i + 1, j + 1);

      rep(l, k) {
        tmp2 = gcd(tmp, l + 1);
        ans += tmp2;
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

// 最大公約数を返す関数
int div(int a, int b, int c) {
  vector<int> arr_a;
  vector<int> arr_b;
  vector<int> arr_c;
  for (int i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      arr_a.push_back(i);
      if (i * i != a) {
        arr_a.push_back(a / i);
      }
    }
  }
  for (int i = 1; i * i <= b; i++) {
    if (b % i == 0) {
      arr_b.push_back(i);
      if (i * i != b) {
        arr_b.push_back(b / i);
      }
    }
  }
  for (int i = 1; i * i <= c; i++) {
    if (c % i == 0) {
      arr_c.push_back(i);
      if (i * i != c) {
        arr_c.push_back(c / i);
      }
    }
  }

  int max_div = 1;

  rep(i, arr_a.size()) {

    rep(j, arr_b.size()) {

      if (arr_a.at(i) == arr_b.at(j)) {
        rep(k, arr_c.size()) {
          if (arr_a.at(i) == arr_c.at(k)) {
            max_div = max(max_div, arr_a.at(i));
          } else {
            continue;
          }
        }
      } else {
        continue;
      }
    }
  }

  return max_div;
}

int main() {
  int k;
  cin >> k;
  vector<int> gcd(pow(k, 3));
  int cnt = 0;
  rep(i, k) {
    rep(j, k) {
      rep(l, k) {
        gcd.at(cnt) = div(i + 1, j + 1, l + 1);
        cnt++;
      }
    }
  }

  int ans = 0;
  arep(x, gcd) { ans += x; }
  cout << ans << endl;
}
Problem p02713 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int fn(int a, int b) { return b == 0 ? a : (fn(b, a % b)); }

int main() {
  int K;
  cin >> K;
  long long sum = 0;
  for (int i = 1; i < K + 1; i++)
    for (int j = 1; j < K + 1; j++)
      for (int t = 1; t < K + 1; t++)
        sum = sum + fn(fn(i, j), t);

  cout << sum;
}


Error: TLE

Fixed submission:

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

int f(int a, int b) { return b == 0 ? a : (f(b, a % b)); }

int main() {
  int t;
  cin >> t;
  long long sum = 0;
  for (int i = 1; i < t + 1; i++)
    for (int j = 1; j < t + 1; j++)
      for (int v = 1; v < v + 1; v++)
        sum = sum + f(f(i, j), v);

  cout << sum;
}