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

Buggy submission (status: Runtime Error):
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
const int maxn = 5050, mod = 1e9 + 7;
ll solve(ll n, ll x) {
  if (x == 0 || x == n)
    return 0;
  ll l = n - x;
  ll cnt = x / l;
  ll ans = 3 * l * cnt;
  x %= l;
  if (x)
    ans += l + 2 * x;
  return ans + solve(n - x, x) - ((l - x) % l);
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  ll n, x;
  cin >> n >> x;
  cout << solve(n, x);
}


Fixed submission:
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
const int maxn = 5050, mod = 1e9 + 7;
ll solve(ll n, ll x) {
  if (x == 0 || x == n)
    return 0;
  ll l = n - x;
  ll cnt = x / l;
  ll ans = 3 * l * cnt;
  x %= l;
  if (x)
    ans += l + 2 * x;
  return ans + solve(l, (l - x) % l) - ((l - x) % l);
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  ll n, x;
  cin >> n >> x;
  cout << solve(n, x);
}
Problem p04048 (C++)

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

using namespace std;

using ll = long long;
using db = double;
using vi = vector<int>;
#define pb push_back
#define fir first
#define sec second

ll n, x;

ll solve(ll n, ll x) {
  if (x * 2 == n)
    return 2 * x;
  ll t = n - x;
  ll s = x / t;
  if (s == 0)
    return solve(n - x, n - 2 * x) + n;
  else
    return solve(n - s * t, x - s * t) + 2 * s * t;
}

int main() {
  cin >> n >> x;
  cout << solve(n, x) + x << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using db = double;
using vi = vector<int>;
#define pb push_back
#define fir first
#define sec second

ll n, x;

ll solve(ll n, ll x) {
  if (x * 2 == n)
    return 2 * x;
  ll t = n - x;
  ll s = (x - 1) / t;
  if (s == 0)
    return solve(n - x, n - 2 * x) + n;
  else
    return solve(n - s * t, x - s * t) + 2 * s * t;
}

int main() {
  cin >> n >> x;
  cout << solve(n, x) + x << endl;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FIR first
#define SEC second
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

int L(int a, int b) {
  if (a < b) {
    if (b % a == 0)
      return a * (2 * (b / a) - 1);
    else
      return 2 * a * (b / a) + L(a, b % a);
  } else {
    return L(b, a);
  }
}

signed main() {
  int n, x;
  cin >> n >> x;
  cout << n + L(x, n - x) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FIR first
#define SEC second
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

int L(int a, int b) {
  if (a > b)
    swap(a, b);
  if (b % a == 0)
    return a * (2 * (b / a) - 1);
  else
    return 2 * a * (b / a) + L(a, b % a);
}

signed main() {
  int n, x;
  cin >> n >> x;
  cout << n + L(x, n - x) << endl;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

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

  ll N, X;
  cin >> N >> X;

  ll A = X;
  ll B = N - X;

  if (A == B)
    co(A * 3);
  else if (A < B) {
    ce(B);
    ll kotae = N;
    while (true) {
      kotae += (B / A) * 2 * A;
      ce(kotae);
      if (B % A == 0) {
        kotae -= A;
        co(kotae);
        return 0;
      }
      B = B % A;
      swap(A, B);
    }

  } else {

    ll kotae = A - B + (A / B) * 2 * B;
    A %= B;
    while (true) {
      kotae += (B / A) * 2 * A;
      if (B % A == 0) {
        kotae -= A;
        co(kotae);
        return 0;
      }
      B = B % A;
      swap(A, B);
    }
  }

  Would you please return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

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

  ll N, X;
  cin >> N >> X;

  ll A = X;
  ll B = N - X;

  if (A == B)
    co(A * 3);
  else if (A < B) {
    ce(B);
    ll kotae = N;
    while (true) {
      kotae += (B / A) * 2 * A;
      ce(kotae);
      if (B % A == 0) {
        kotae -= A;
        co(kotae);
        return 0;
      }
      B = B % A;
      swap(A, B);
    }

  } else {

    ll kotae = A + B + (A / B) * 2 * B;
    if (A % B == 0) {
      kotae -= B;
      co(kotae);
      return 0;
    }
    A %= B;
    while (true) {
      kotae += (B / A) * 2 * A;
      if (B % A == 0) {
        kotae -= A;
        co(kotae);
        return 0;
      }
      B = B % A;
      swap(A, B);
    }
  }

  Would you please return 0;
}
Problem p04048 (C++)

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, X;
  cin >> N >> X;
  ll ans = 0;
  // 1
  ans += X;
  // 2
  ans += N - X;
  // 3
  ll cnt = (N - X) / X;
  ans += 2 * X * cnt;
  // 4
  ll d = (N - X) % X;
  ans += 2 * d * (X / d) - d;
  cout << ans << endl;
  return 0;
}

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, X;
  cin >> N >> X;
  ll ans = X;
  ll d = N - X;
  ll cnt = N / d;
  ans += 2 * d * cnt - d;
  ll tmp = d;
  if (N % d == 0)
    ans -= d;
  d = N % d;
  N = tmp;
  while (d > 0) {
    cnt = N / d;
    ans += 2 * d * cnt;
    tmp = d;
    if (N % d == 0) {
      ans -= d;
    }
    d = N % d;
    N = tmp;
  }
  cout << ans << endl;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>

int main() {
  std::uint64_t SizeOfTriangle;
  std::uint64_t InputMysteriousLight;

  std::cin >> SizeOfTriangle >> InputMysteriousLight;

  std::uint64_t LocusLength = SizeOfTriangle;

  std::uint64_t a = InputMysteriousLight;
  std::uint64_t b = (SizeOfTriangle - InputMysteriousLight);

  std::uint64_t l = (a > b) ? a : b;
  std::uint64_t s = (a < b) ? a : b;

  while (true) {
    if (l == s) {
      LocusLength += s;
      break;
    }
    if (s == 1) {
      LocusLength += l * 2 - 1;
      break;
    }

    std::uint64_t m = l / s;
    LocusLength += (2 * s * m);

    std::uint64_t t = l % s;
    l = s;
    s = t;
  }

  std::cout << LocusLength << std::endl;

  return 0;
}

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

int main() {
  std::uint64_t SizeOfTriangle;
  std::uint64_t InputMysteriousLight;

  std::cin >> SizeOfTriangle >> InputMysteriousLight;

  std::uint64_t LocusLength = SizeOfTriangle;

  std::uint64_t a = InputMysteriousLight;
  std::uint64_t b = (SizeOfTriangle - InputMysteriousLight);

  std::uint64_t l = (a > b) ? a : b;
  std::uint64_t s = (a < b) ? a : b;

  while (true) {
    if (l == s) {
      LocusLength += s;
      break;
    }
    if (s == 1) {
      LocusLength += l * 2 - 1;
      break;
    }
    if (s == 0) {
      LocusLength -= l;
      break;
    }

    std::uint64_t m = l / s;
    LocusLength += (2 * s * m);

    std::uint64_t t = l % s;
    l = s;
    s = t;
  }

  std::cout << LocusLength << std::endl;

  return 0;
}
Problem p04048 (C++)

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

ll solve(ll a, ll b) {
  if (a == b)
    return a;
  return 2 * a + solve(b - a, a);
}

void Main() {
  ll N, X;
  cin >> N >> X;
  ll a = X;
  ll b = N - X;
  if (a > b)
    swap(a, b);
  cout << N + solve(a, b) << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}


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

ll solve(ll a, ll b) {
  if (b % a == 0)
    return 2 * b - a;
  return b / a * a * 2 + solve(b % a, a);
}

void Main() {
  ll N, X;
  cin >> N >> X;
  ll a = X;
  ll b = N - X;
  if (a > b)
    swap(a, b);
  cout << N + solve(a, b) << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define popCnt(x) (__builtin_popcountll(x))
typedef unsigned long long Long;

Long f(Long n, Long x) {
  if (n == x)
    return x;
  int k = (n + x - 1) / x - 1;
  return 2 * x * k + f(x, n - k * x);
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  Long n, x;
  cin >> n >> x;

  cout << n + f(n - x, x);
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define popCnt(x) (__builtin_popcountll(x))
typedef unsigned long long Long;

Long f(Long n, Long x) {
  if (n == x)
    return x;
  Long k = (n + x - 1) / x - 1;
  return 2 * x * k + f(x, n - k * x);
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  Long n, x;
  cin >> n >> x;

  cout << n + f(n - x, x);
}
Problem p04048 (C++)

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

int64 f(int64 x, int64 y) {
  int64 mx = max(x, y);
  int64 mn = min(x, y);
  if (mx == mn)
    return x;
  return f(mn, mx - mn) + 2 * mn;
}

int main() {
  int64 n, x;
  cin >> n >> x;
  int64 ans = x + (n - x) + f(x, n - x);
  cout << ans << endl;
  return 0;
}

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

int64 f(int64 x, int64 y) {
  int64 mx = max(x, y);
  int64 mn = min(x, y);
  if (mx % mn == 0)
    return (mx / mn) * (2L * mn) - mn;
  return f(mn, mx % mn) + (mx / mn) * (2L * mn);
}

int main() {
  int64 n, x;
  cin >> n >> x;
  int64 ans = x + (n - x) + f(x, n - x);
  cout << ans << endl;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

unsigned long long solve(unsigned long long a, unsigned long long b) {
  if (a == 0 || b == 0)
    return 0;
  if (a > b)
    return 3 * b + solve(a - b, b);
  return 3 * a + solve(b - a, a);
}

int main() {
  unsigned long long N, X;
  scanf("%lld %lld", &N, &X);
  printf("%lld\n", solve(N - X, X));
}


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

unsigned long long solve(unsigned long long a, unsigned long long b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  unsigned long long fac = a / b;
  return 3 * fac * b + solve(b, a - fac * b);
}

int main() {
  unsigned long long N, X;
  scanf("%lld %lld", &N, &X);
  printf("%lld\n", solve(N - X, X));
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long gcd(int x, int y) {
  if (x == 0)
    return y;
  if (x < y)
    swap(x, y);
  return gcd(x % y, y);
}
int main() {
  cin >> n >> m;
  cout << ((n / gcd(n, m) - 1) * 3 * gcd(n, m)) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long gcd(long long x, long long y) {
  if (x == 0)
    return y;
  if (x < y)
    swap(x, y);
  return gcd(x % y, y);
}
int main() {
  cin >> n >> m;
  cout << ((n / gcd(n, m) - 1) * 3 * gcd(n, m)) << endl;
  return 0;
}
Problem p04048 (C++)

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

int main() {
  int n, x, length;
  int a[2];
  cin >> n >> x;
  a[0] = n - x;
  a[1] = x;
  length = x;
  while (a[0] != a[1]) {
    if (a[0] > a[1]) {
      length = length + ((a[0] - 1) / a[1]) * a[1];
      a[0] = (a[0] - 1) % a[1] + 1;
    }
    if (a[0] < a[1]) {
      a[1] = (a[1] - 1) % a[0] + 1;
    }
  }
  cout << length * 3;
}

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

int main() {
  long int n, x, length;
  long int a[2];
  cin >> n >> x;
  a[0] = n - x;
  a[1] = x;
  length = x;
  while (a[0] != a[1]) {
    if (a[0] > a[1]) {
      length = length + ((a[0] - 1) / a[1]) * a[1];
      a[0] = (a[0] - 1) % a[1] + 1;
    }
    if (a[0] < a[1]) {
      a[1] = (a[1] - 1) % a[0] + 1;
    }
  }
  cout << length * 3;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, X;
ll ans;
void func(ll a, ll b) {
  if (a < b) {
    if (b % a == 0) {
      ans += ((b / a) * 2 - 1) * a;
      return;
    }
    ans += (b / a) * 2 * a;
    b = b % a;
    func(a, b);
  } else
    func(b, a);
}
int main() {
  cin >> N >> X;
  ans = N;
  func(X, N - X);
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, X;
ll ans;
void func(ll a, ll b) {
  if (a <= b) {
    if (b % a == 0) {
      ans += ((b / a) * 2 - 1) * a;
      return;
    }
    ans += (b / a) * 2 * a;
    b = b % a;
    func(a, b);
  } else
    func(b, a);
}
int main() {
  cin >> N >> X;
  ans = N;
  func(X, N - X);
  cout << ans;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
void swap(long long &a, long long &b) {
  long long t;
  t = a;
  a = b;
  b = t;
}
int main() {
  long long n, x;
  scanf("%lld%lld", &n, &x);
  long long ans = n;
  long long a = x, b = n - x;
  while (b) {
    if (a > b)
      swap(a, b);
    ans += a << 1;
    b -= a;
  }
  ans -= a;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
void swap(long long &a, long long &b) {
  long long t;
  t = a;
  a = b;
  b = t;
}
int main() {
  long long n, x;
  scanf("%lld%lld", &n, &x);
  long long ans = n;
  long long a = x, b = n - x;
  while (b) {
    if (a > b)
      swap(a, b);
    ans += (b / a) * (a << 1);
    b %= a;
  }
  ans -= a;
  printf("%lld\n", ans);
  return 0;
}
Problem p04048 (C++)

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

typedef long long ll;

ll ans;

void slv(ll a, ll b) {
  if (a == 0)
    return;
  ll op1 = 2 * a - b;
  if (op1 >= 0) {
    ans += 2 * (b - a);
    slv(op1, a);
  } else {
    ans += b;
    slv(b - 2 * a, b - a);
  }
}

int main() {
  ll n, x;
  scanf("%lld%lld", &n, &x);
  ans = x;
  slv(x, n);
  printf("%lld\n", ans);
  return 0;
}


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

typedef long long ll;

ll ans;

void slv(ll a, ll b) {
  if (a == 0)
    return;
  ll tmp = a / (b - a) * (b - a);
  ans += 2 * tmp;
  a -= tmp;
  b -= tmp;
  if (a == 0)
    return;
  ans += b;
  slv(b - 2 * a, b - a);
}

int main() {
  ll n, x;
  scanf("%lld%lld", &n, &x);
  ans = x;
  slv(x, n);
  printf("%lld\n", ans);
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
/* Written by Filip Hlasek 2016 */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, b) for (int i = 0; i < (b); i++)

using namespace std;

int main(int argc, char *argv[]) {
  long long N, X;
  scanf("%lld%lld", &N, &X);
  if (N == 2 * X) {
    printf("%lld\n", 3 * X);
    return 0;
  }
  if (2 * X > N)
    X = N - X;
  long long A = N - 2 * X, B = X, ans = N + X;
  while (true) {
    if (A <= B) {
      ans += 3 * B;
      long long nB = B % A;
      long long nA = A - nB;
      if (B % A == 0) {
        ans -= A;
        break;
      }
      ans -= nB;
      B = nB;
      A = nA;
    } else {
      long long cnt = A / B;
      ans += 2 * cnt * B;
      A -= cnt * B;
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
/* Written by Filip Hlasek 2016 */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, b) for (int i = 0; i < (b); i++)

using namespace std;

int main(int argc, char *argv[]) {
  long long N, X;
  scanf("%lld%lld", &N, &X);
  if (N == 2 * X) {
    printf("%lld\n", 3 * X);
    return 0;
  }
  if (2 * X > N)
    X = N - X;
  long long A = N - 2 * X, B = X, ans = N + X;
  while (true) {
    if (!A)
      break;
    if (A <= B) {
      ans += 3 * B;
      long long nB = B % A;
      long long nA = A - nB;
      if (B % A == 0) {
        ans -= A;
        break;
      }
      ans -= nB;
      B = nB;
      A = nA;
    } else {
      long long cnt = A / B;
      ans += 2 * cnt * B;
      A -= cnt * B;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
ll f(ll a, ll b) {
  ll mx = max(a, b);
  ll mn = min(a, b);
  if (mn == mx)
    return mx;
  if (mx % mn == 0)
    return 2 * mn + f(mn, mx - mn);
  ;
  return 2 * mn * (mx / mn) + f(mn, mx % mn);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, X;
  cin >> N >> X;
  cout << N + f(X, N - X) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
ll f(ll a, ll b) {
  ll mx = max(a, b);
  ll mn = min(a, b);
  if (mn == mx)
    return mx;
  if (mx % mn == 0)
    return 2 * mx - mn;
  return 2 * mn * (mx / mn) + f(mn, mx % mn);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, X;
  cin >> N >> X;
  cout << N + f(X, N - X) << endl;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
long long ans = 0;
void dfs(long long a, long long b) {
  if (b > a)
    dfs(b, a);
  if (b == a)
    ans += b;
  if (a > b) {
    ans += 2 * b;
    dfs(a - b, b);
  }
}
int main() {
  long long n, x;
  cin >> n >> x;
  dfs(x, n - x);
  cout << ans + n;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
long long ans = 0;
void dfs(long long a, long long b) {
  if (b > a)
    dfs(b, a);
  if (b == a)
    ans += b;
  if (a > b) {
    long long tmp = 1 + ((a - b - 1) / b);
    ans += tmp * 2 * b;
    dfs(a - tmp * b, b);
  }
}
int main() {
  long long n, x;
  cin >> n >> x;
  dfs(x, n - x);
  cout << ans + n;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define REMOVE(Itr, n)                                                         \
  (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end())
#define PB_VEC(Itr1, Itr2)                                                     \
  (Itr1).insert((Itr1).end(), (Itr2).begin(), (Itr2).end())
#define UNIQUE(Itr)                                                            \
  sort((Itr).begin(), (Itr).end());                                            \
  (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end())
#define LBOUND(Itr, val) lower_bound((Itr).begin(), (Itr).end(), (val))
#define UBOUND(Itr, val) upper_bound((Itr).begin(), (Itr).end(), (val))
typedef long long ll;

ll light(ll a, ll b) {
  if (a == b)
    return a * 3;
  ll m = max(a, b), n = min(a, b);
  return n * (m / n) * 3 + light(m % n, n);
}

int main() {

  ll N, X;
  cin >> N >> X;

  cout << light(X, N - X) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define REMOVE(Itr, n)                                                         \
  (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end())
#define PB_VEC(Itr1, Itr2)                                                     \
  (Itr1).insert((Itr1).end(), (Itr2).begin(), (Itr2).end())
#define UNIQUE(Itr)                                                            \
  sort((Itr).begin(), (Itr).end());                                            \
  (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end())
#define LBOUND(Itr, val) lower_bound((Itr).begin(), (Itr).end(), (val))
#define UBOUND(Itr, val) upper_bound((Itr).begin(), (Itr).end(), (val))
typedef long long ll;

ll light(ll a, ll b) {
  if (a == b)
    return a * 3;
  if (a == 0)
    return 0;
  ll m = max(a, b), n = min(a, b);
  return n * (m / n) * 3 + light(m % n, n);
}

int main() {

  ll N, X;
  cin >> N >> X;

  cout << light(X, N - X) << endl;

  return 0;
}
Problem p04048 (C++)

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

using namespace std;

typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

long long f(long long A, long long B) {
  if (A > B)
    return f(B, A);
  if (A == B)
    return A;
  return 2 * A + f(A, B - A);
}

int main() {
  long long N, X;
  cin >> N >> X;

  cout << N + f(X, N - X) << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double Double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

long long f(long long A, long long B) {
  if (A > B)
    return f(B, A);
  if (A == 0)
    return -B;
  return 2 * A * (B / A) + f(B % A, A);
}

int main() {
  long long N, X;
  cin >> N >> X;

  cout << N + f(X, N - X) << endl;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793
using namespace std;
typedef long long ll;
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
int gcd(int a, int b) { // ユークリッドの互除法
  if (a < b)
    gcd(b, a); // aの方がbよりでかいのが前提
  if (b == 0)
    return a; // aをbで割り切れたらreturn
  else
    gcd(b, a % b);
}
const int INF = 1 << 30;
const int MOD = 1000000007;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

int main() {
  ll N, X;
  cin >> N >> X;
  cout << 3 * (N - gcd(N, X)) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
#define rep(i, n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793
using namespace std;
typedef long long ll;
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
ll gcd(ll a, ll b) { // ユークリッドの互除法
  if (a < b)
    gcd(b, a); // aの方がbよりでかいのが前提
  if (b == 0)
    return a; // aをbで割り切れたらreturn
  else
    gcd(b, a % b);
}
const int INF = 1 << 30;
const int MOD = 1000000007;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

int main() {
  ll N, X;
  cin >> N >> X;
  cout << 3 * (N - gcd(N, X)) << endl;

  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

int main(void) {
  int n, x, a, b, c, l;
  cin >> n >> x;

  l = n;
  a = x;
  b = n - x;
  while (1) {
    if (a > b)
      swap(a, b);
    if (b % a == 0) {
      l += b * 2 - a;
      break;
    }
    l += (b / a) * 2 * a;
    b = b % a;
  }

  cout << l << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))

int main(void) {
  lli n, x, a, b, c, l;
  cin >> n >> x;

  l = n;
  a = x;
  b = n - x;
  while (1) {
    if (a > b)
      swap(a, b);
    if (b % a == 0) {
      l += b * 2 - a;
      break;
    }
    l += (b / a) * 2 * a;
    b = b % a;
  }

  cout << l << endl;
  return 0;
}
Problem p04048 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

N, X = map(int, input().split())
print(3 * (N - gcd(N, X)))


Fixed submission:
from fractions import gcd

N, X = map(int, input().split())
print(3 * (N - gcd(N, X)))
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main(void) {
  long long n, x;
  cin >> n >> x;

  long long ans = x + n - x;

  long long width = n - (n - x);
  long long height = n - x;

  while (true) {
    if (width >= height) {
      ans += (width / height) * height * 2;
      if (width % height == 0) {
        ans -= height;
        break;
      }
      width = width % height;
    } else if (width < height) {
      ans += (height / width) * width * 2;
      if (width % height == 0) {
        ans -= width;
        break;
      }
      height = height % width;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <iostream>

using namespace std;

int main(void) {
  long long n, x;
  cin >> n >> x;

  long long ans = x + n - x;

  long long width = n - (n - x);
  long long height = n - x;

  while (true) {
    if (width >= height) {
      ans += (width / height) * height * 2;
      if (width % height == 0) {
        ans -= height;
        break;
      }
      width = width % height;
    } else if (width < height) {
      ans += ((height / width) * width * 2);
      if (height % width == 0) {
        ans -= width;
        break;
      }
      height = height % width;
    }
  }

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

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

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long double ld;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

const int MOD = 1e9 + 7;

ll f(ll a, ll b) {
  if (a > b)
    swap(a, b);
  if (a == b)
    return a;
  return (a * 2LL + f(a, b - a));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, x;
  cin >> n >> x;
  cout << f(x, n - x) + n;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef long double ld;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

const int MOD = 1e9 + 7;

ll f(ll a, ll b) {
  if (a > b)
    swap(a, b);
  if (a == b)
    return a;
  ll q = b / a;
  if (q == 1)
    return (a * 2LL + f(a, b - a));
  return (a * 2LL * (q - 1) + f(a, b - (q - 1) * a));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, x;
  cin >> n >> x;
  cout << f(x, n - x) + n;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long int parallelogram(long long int, long long int);
long long int trapezium(long long int, long long int);
long long int parallelogram(long long int s1, long long int s2) {
  if (s1 == s2)
    return s1;
  else if (s1 < s2)
    return s1 + trapezium(s2 - s1, s1);
  else
    return s2 + trapezium(s1 - s2, s2);
}
long long int trapezium(long long int s1, long long int s2) {
  return s2 + parallelogram(s1, s2);
}
int main() {
  long long int n, x;
  cin >> n >> x;
  long long int ans = x + trapezium(x, n - x);
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long int parallelogram(long long int, long long int);
long long int trapezium(long long int, long long int);
long long int parallelogram(long long int s1, long long int s2) {
  if (s1 < s2)
    swap(s1, s2);
  if (s1 % s2 == 0)
    return (2ll * (s1 / s2) - 1) * s2;
  return (2ll * (s1 / s2) * s2) + parallelogram(s1 % s2, s2);
}
long long int trapezium(long long int s1, long long int s2) {
  return s2 + parallelogram(s1, s2);
}
int main() {
  long long int n, x;
  cin >> n >> x;
  long long int ans = x + trapezium(x, n - x);
  cout << ans << "\n";
  return 0;
}
Problem p04048 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, init, n) for (int i = init; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll solve(ll a, ll b) {
  ll x = min(a, b);
  ll y = max(a, b);

  if (y % 2 == 0)
    return 2 * x * y / x - x;
  return 2 * x * (y / x) + solve(x, y % x);
}

int main() {
  ll N, X;
  cin >> N >> X;

  cout << N + solve(N - X, X) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, init, n) for (int i = init; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll solve(ll a, ll b) {
  ll x = min(a, b);
  ll y = max(a, b);

  if (y % x == 0)
    return 2 * x * (y / x) - x;
  return 2 * x * (y / x) + solve(x, y % x);
}

int main() {
  ll N, X;
  cin >> N >> X;

  cout << N + solve(N - X, X) << endl;

  return 0;
}
Problem p04048 (C++)

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

#define lint long long int
using namespace std;

lint Solve(lint N, lint X) {
  if (!N || !X)
    return 0;

  // cout << "Solve(" << N << ", " << X << ") - Type ";
  if (2 * X <= N) {
    // cout << "1" << endl;
    return 3 * X + Solve(N - X, N - 2 * X);
  } else {
    // cout << "2" << endl;
    return 3 * (N - X) + Solve(X, N - 2 * (N - X));
  }
}

void Stress() {
  static const lint NMAX = 1000000000000LL;
  static const double MAGIC = (3 - sqrt(5)) * 0.5;

  lint N = NMAX;
  lint X = N * MAGIC;

  cout << Solve(N, X) << '\n';
}

void Submit() {
  lint N, X;
  cin >> N >> X;

  cout << Solve(N, X) << '\n';
}

int main() {
  // Stress();
  Submit();

  return 0;
}

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

#define lint long long int
using namespace std;

lint Solve(lint N, lint X) {
  if (!N || !X)
    return 0;

  // cout << "Solve(" << N << ", " << X << ") - Type ";
  if (2 * X <= N) {
    // cout << "1" << endl;
    return 3 * X + Solve(N - X, N - 2 * X);
  } else {
    // cout << "2" << endl;
    lint C = X / (N - X);
    return X + (N - X) * (3 * C + 1) - N +
           Solve(N - (N - X) * C, N - (N - X) * (C + 1));
  }
}

void Stress() {
  static const lint NMAX = 1000000000000LL;
  static const double MAGIC = (3 - sqrt(5)) * 0.5;

  lint N = NMAX;
  lint X = N * MAGIC;

  cout << Solve(N, X) << '\n';
}

void Submit() {
  lint N, X;
  cin >> N >> X;

  cout << Solve(N, X) << '\n';
}

int main() {
  // Stress();
  Submit();

  return 0;
}
Problem p04048 (C++)

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

typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define REP(i, n) FOR(i, 0, n)

ll calc_distance(ll a, ll b) {
  ll ans;

  if (a % b == 0 || b % a == 0) {
    ans = (a >= b) ? b * (2 * (a / b) - 1) : a * (2 * (b / a) - 1);
  } else if (a >= b) {
    ans = 2 * b * (a / b) + calc_distance(a % b, b);
  } else {
    ans = 2 * a * (b / a) + calc_distance(a, b & a);
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  ll x;
  cin >> x;

  ll ans = n + calc_distance(n - x, x);

  cout << ans << endl;
}

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

typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define REP(i, n) FOR(i, 0, n)

ll calc_distance(ll a, ll b) {
  ll ans;

  if (a % b == 0 || b % a == 0) {
    ans = (a >= b) ? b * (2 * (a / b) - 1) : a * (2 * (b / a) - 1);
  } else if (a >= b) {
    ans = 2 * b * (a / b) + calc_distance(a % b, b);
  } else {
    ans = 2 * a * (b / a) + calc_distance(a, b % a);
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  ll x;
  cin >> x;

  ll ans = n + calc_distance(n - x, x);

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

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

long f(long a, long b) {
  if (b < a)
    swap(a, b);
  if (min(a, b - a) == 0)
    return a;
  return 2 * a + f(a, b - a);
}

int main() {
  long N, X;
  cin >> N >> X;
  long ans = N;
  long a = N - X, b = X;
  cout << N + f(N - X, X) << endl;
}

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

long f(long a, long b) {
  if (b < a)
    swap(a, b);
  if (b % a == 0)
    return 2 * (b / a) * a - a;
  return 2 * (b / a) * a + f(a, b % a);
}

int main() {
  long N, X;
  cin >> N >> X;
  long ans = N;
  long a = N - X, b = X;
  cout << N + f(N - X, X) << endl;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int n, m;
int xx, yy;
int ans;

int main() {
  cin >> n >> m;
  ans += n;
  xx = m;
  yy = n - m;
  if (xx < yy)
    swap(xx, yy);
  do {
    if (xx % yy != 0)
      ans += xx / yy * 2 * yy;
    else
      ans += (xx / yy * 2 - 1) * yy;
    xx = xx % yy;
    swap(xx, yy);
  } while (yy != 0);
  cout << ans << endl;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
long long n, m;
long long xx, yy;
long long ans;

int main() {
  cin >> n >> m;
  ans += n;
  xx = m;
  yy = n - m;
  if (xx < yy)
    swap(xx, yy);
  do {
    if (xx % yy != 0)
      ans += xx / yy * 2 * yy;
    else
      ans += (xx / yy * 2 - 1) * yy;
    xx = xx % yy;
    swap(xx, yy);
  } while (yy != 0);
  cout << ans << endl;
}
Problem p04048 (C++)

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

ll f(ll a, ll b) {
  ll r;
  if (b % a) {
    r = 2 * (b / a) * a + f(b % a, a);
  } else {
    r = (2 * (b / a) - 1) * a;
  }
  return r;
}

int main() {
  int n, x;
  cin >> n >> x;
  cout << n + f(x, n - x) << endl;
  return 0;
}


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

ll f(ll a, ll b) {
  ll r;
  if (b % a) {
    r = 2 * (b / a) * a + f(b % a, a);
  } else {
    r = (2 * (b / a) - 1) * a;
  }
  return r;
}

int main() {
  ll n, x;
  cin >> n >> x;
  cout << n + f(x, n - x) << endl;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long N, X;

long long f(long long a, long long b) {
  if (a == b)
    return a;
  if (a > b)
    swap(a, b);
  int k = (b - 1) / a;
  return f(a, b - a * k) + 2 * a * k;
}

int main() {
  cin >> N >> X;
  cout << f(X, N - X) + N << "\n";
  return 0;
}


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

long long N, X;

long long f(long long a, long long b) {
  if (a == b)
    return a;
  if (a > b)
    swap(a, b);
  long long k = (b - 1) / a;
  return f(a, b - a * k) + 2 * a * k;
}

int main() {
  cin >> N >> X;
  cout << f(X, N - X) + N << "\n";
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <unordered_set>
using namespace std;

long long n, x;
long long work(long long h, long long w) {
  if (w % h == 0)
    return 2 * w;
  return 2 * h * (w / h) + 3 * (w % h) + work(h - w % h, w % h);
}
int main() {
  scanf("%lld%lld", &n, &x);
  if (x * 2 == n) {
    printf("%lld\n", 3 * x);
    return 0;
  }
  if (x * 2 < n) {
    printf("%lld\n", 4 * x + work(n - 2 * x, x));
  } else {
    printf("%lld\n", x + work(n - x, x));
  }
  return 0;
}


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

long long n, x;
long long work(long long h, long long w) {
  if (w < h) {
    return h + w + work(w, h - w);
  }
  if (w % h == 0)
    return 2 * w;
  return 2 * h * (w / h) + 3 * (w % h) + work(h - w % h, w % h);
}
int main() {
  scanf("%lld%lld", &n, &x);
  if (x * 2 == n) {
    printf("%lld\n", 3 * x);
    return 0;
  }
  if (x * 2 < n) {
    printf("%lld\n", 4 * x + work(n - 2 * x, x));
  } else {
    printf("%lld\n", x + work(n - x, x));
  }
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long f(long long n, long long x) {
  if (n == 0 || x == 0)
    return -x;
  if (n < x)
    return f(x, n);
  return 2 * x + f(n - x, x);
}
int main() {
  long long n, x;
  scanf("%lld%lld", &n, &x);
  printf("%lld\n", n + f(n - x, x));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long f(long long a, long long b) {
  if (b == 0)
    return -a;
  if (a >= b)
    return f(b, a % b) + 2 * b * (a / b);
  return f(b, a % b);
}
int main() {
  long long n, x;
  scanf("%lld%lld", &n, &x);
  printf("%lld\n", n + f(n - x, x));
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define U(v) cerr << #v << ": " << (v) << endl

int main() {
  long long N, X;
  cin >> N >> X;
  long long sm = N;
  long long a = X, b = N - X;
  while (a != b) {
    if (a > b)
      swap(a, b);
    if (a == 1) {
      sm += (b - 1) * 2;
      b = 1;
    } else {
      sm += b / a * a * 2;
      b %= a;
    }
  }
  sm += a;
  cout << sm << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define U(v) cerr << #v << ": " << (v) << endl

int main() {
  long long N, X;
  cin >> N >> X;
  long long sm = N;
  long long a = X, b = N - X;
  while (a != b) {
    if (a > b)
      swap(a, b);
    if (a == 1) {
      sm += (b - 1) * 2;
      b = 1;
    } else {
      sm += b / a * a * 2;
      b %= a;
      if (b == 0) {
        cout << sm - a << endl;
        return 0;
      }
    }
  }
  sm += a;
  cout << sm << endl;
  return 0;
}
Problem p04049 (C++)

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

template <typename T> void chmin(T &x, const T &y) {
  if (x > y)
    x = y;
}
typedef long long s64;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, r, l) for (int i = r; i >= l; --i)
const int N = 100 + 5;
s64 f(s64 x, s64 y) {
  s64 ans = 0;
  while (x) {
    s64 _y = y % x;
    ans += (y - _y) * 2;
    y = x;
    x = _y;
  }
  return ans - y;
}
int n, k;
vector<int> lk[N];
int dp[N][N];

void dfs(int x, int fr) {
  dp[x][0] = 1;
  for (auto y : lk[x])
    if (y != fr) {
      dfs(y, x);
      dp[x][0] += dp[y][0];
      per(j, k, 1) dp[x][j] = min(dp[y][j - 1] + dp[x][min(j, k - (j - 1))],
                                  dp[x][j] + dp[y][min(j - 1, k - j)]);
      rep(j, 1, k) chmin(dp[x][j], dp[x][j - 1]);
    }
}

int main() {
  // freopen("1.in","r",stdin);
  cin >> n >> k;
  rep(i, 1, n - 1) {
    int x, y;
    cin >> x >> y;
    lk[x].push_back(y);
    lk[y].push_back(x);
  }
  ++k;
  dfs(1, 0);
  int ans = dp[1][k];
  rep(i, 1, n) chmin(ans, dp[i][k] + n - dp[i][0]);
  cout << ans << endl;
}

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

template <typename T> void chmin(T &x, const T &y) {
  if (x > y)
    x = y;
}
typedef long long s64;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, r, l) for (int i = r; i >= l; --i)
const int N = 2000 + 5;
s64 f(s64 x, s64 y) {
  s64 ans = 0;
  while (x) {
    s64 _y = y % x;
    ans += (y - _y) * 2;
    y = x;
    x = _y;
  }
  return ans - y;
}
int n, k;
vector<int> lk[N];
int dp[N][N];

void dfs(int x, int fr) {
  dp[x][0] = 1;
  for (auto y : lk[x])
    if (y != fr) {
      dfs(y, x);
      dp[x][0] += dp[y][0];
      per(j, k, 1) dp[x][j] = min(dp[y][j - 1] + dp[x][min(j, k - (j - 1))],
                                  dp[x][j] + dp[y][min(j - 1, k - j)]);
      rep(j, 1, k) chmin(dp[x][j], dp[x][j - 1]);
    }
}

int main() {
  // freopen("1.in","r",stdin);
  cin >> n >> k;
  rep(i, 1, n - 1) {
    int x, y;
    cin >> x >> y;
    lk[x].push_back(y);
    lk[y].push_back(x);
  }
  ++k;
  dfs(1, 0);
  int ans = dp[1][k];
  rep(i, 1, n) chmin(ans, dp[i][k] + n - dp[i][0]);
  cout << ans << endl;
}
Problem p04049 (C++)

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

int n, k;
vector<int> e[1001];
int d[1001][1001];
vector<int> eA, eB;

void dfs(int cur, int from, int root, int curDist) {
  d[root][cur] = curDist;
  for (int i = 0; i < e[cur].size(); i++) {
    int t = e[cur][i];
    if (t != from)
      dfs(t, cur, root, curDist + 1);
  }
}

int MAIN() {
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
    eA.push_back(a);
    eB.push_back(b);
  }
  for (int i = 1; i <= n; i++)
    dfs(i, -1, i, 0);
  int bestAns = 0;

  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = 1; j <= n; j++)
      if (2 * d[i][j] <= k)
        cnt++;
    bestAns = max(bestAns, cnt);
  }
  for (int i = 0; i < eA.size(); i++) {
    int a = eA[i];
    int b = eB[i];
    int cnt = 0;
    for (int j = 1; j <= n; j++)
      if (2 * min(d[j][a], d[j][b]) + 1 <= k)
        cnt++;
    bestAns = max(bestAns, cnt);
  }
  cout << n - bestAns << endl;
  return 0;
}

int main() {
  int start = clock();
#ifdef LOCAL_TEST
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios ::sync_with_stdio(false);
  cout << fixed << setprecision(16);
  int ret = MAIN();
#ifdef LOCAL_TEST
  cout << "[Finished in " << clock() - start << " ms]" << endl;
#endif
  return ret;
}


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

int n, k;
vector<int> e[2001];
int d[2001][2001];
vector<int> eA, eB;

void dfs(int cur, int from, int root, int curDist) {
  d[root][cur] = curDist;
  for (int i = 0; i < e[cur].size(); i++) {
    int t = e[cur][i];
    if (t != from)
      dfs(t, cur, root, curDist + 1);
  }
}

int MAIN() {
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    e[a].push_back(b);
    e[b].push_back(a);
    eA.push_back(a);
    eB.push_back(b);
  }
  for (int i = 1; i <= n; i++)
    dfs(i, -1, i, 0);
  int bestAns = 0;

  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = 1; j <= n; j++)
      if (2 * d[i][j] <= k)
        cnt++;
    bestAns = max(bestAns, cnt);
  }
  for (int i = 0; i < eA.size(); i++) {
    int a = eA[i];
    int b = eB[i];
    int cnt = 0;
    for (int j = 1; j <= n; j++)
      if (2 * min(d[j][a], d[j][b]) + 1 <= k)
        cnt++;
    bestAns = max(bestAns, cnt);
  }
  cout << n - bestAns << endl;
  return 0;
}

int main() {
  int start = clock();
#ifdef LOCAL_TEST
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios ::sync_with_stdio(false);
  cout << fixed << setprecision(16);
  int ret = MAIN();
#ifdef LOCAL_TEST
  cout << "[Finished in " << clock() - start << " ms]" << endl;
#endif
  return ret;
}