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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b[110], c, d;

  cin >> a;

  for (int i = 0; i < (2 * a); i++)
    cin >> b[i];

  sort(b, b + (2 * a));

  c = 0;

  for (int i = 0; i < (2 * a); i += 2) {
    c += b[i];
  }

  cout << c << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b[11000], c, d;

  cin >> a;

  for (int i = 0; i < (2 * a); i++)
    cin >> b[i];

  sort(b, b + (2 * a));

  c = 0;

  for (int i = 0; i < (2 * a); i += 2) {
    c += b[i];
  }

  cout << c << endl;

  return 0;
}
Problem p04047 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 102
using namespace std;
int n, i, a[N], ans;
int read() {
  char c = getchar();
  int w = 0;
  while (c < '0' || c > '9')
    c = getchar();
  while (c <= '9' && c >= '0') {
    w = w * 10 + c - '0';
    c = getchar();
  }
  return w;
}
int main() {
  n = read();
  for (i = 1; i <= 2 * n; i++)
    a[i] = read();
  sort(a + 1, a + 2 * n + 1);
  for (i = 1; i <= 2 * n; i += 2)
    ans += a[i];
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 202
using namespace std;
int n, i, a[N], ans;
int read() {
  char c = getchar();
  int w = 0;
  while (c < '0' || c > '9')
    c = getchar();
  while (c <= '9' && c >= '0') {
    w = w * 10 + c - '0';
    c = getchar();
  }
  return w;
}
int main() {
  n = read();
  for (i = 1; i <= 2 * n; i++)
    a[i] = read();
  sort(a + 1, a + 2 * n + 1);
  for (i = 1; i <= 2 * n; i += 2)
    ans += a[i];
  printf("%d\n", ans);
  return 0;
}
Problem p04047 (C++)

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

int main() {
  int N;
  cin >> N;
  int L[110];
  for (auto i = 0; i < 2 * N; i++) {
    cin >> L[i];
  }
  sort(L, L + 2 * N);
  int ans = 0;
  for (auto i = 0; i < 2 * N; i++) {
    if (i % 2 == 0)
      ans += L[i];
  }
  cout << ans << endl;
}


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

int main() {
  int N;
  cin >> N;
  int L[220];
  for (auto i = 0; i < 2 * N; i++) {
    cin >> L[i];
  }
  sort(L, L + 2 * N);
  int ans = 0;
  for (auto i = 0; i < 2 * N; i++) {
    if (i % 2 == 0)
      ans += L[i];
  }
  cout << ans << endl;
}
Problem p04047 (C++)

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

int main() {
  int N, L[101], ans = 0;

  cin >> N;

  N *= 2;

  for (int i = 0; i < N; i++) {
    cin >> L[i];
  }

  sort(L, L + N);

  for (int i = 0; i < N; i += 2) {
    ans += L[i];
  }

  cout << ans << endl;

  return 0;
}

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

int main() {
  int N, L[201], ans = 0;

  cin >> N;

  N *= 2;

  for (int i = 0; i < N; i++) {
    cin >> L[i];
  }

  sort(L, L + N);

  for (int i = 0; i < N; i += 2) {
    ans += L[i];
  }

  cout << ans << endl;

  return 0;
}
Problem p04047 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, i, a[110];
  cin >> n;
  n *= 2;
  for (i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  int b = 0, c = 0;
  for (i = 0; i < n; i += 2) {
    b += a[i];
  }
  for (i = 1; i < n - 1; i += 2) {
    c += a[i];
  }
  c += a[0];
  cout << max(b, c) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, i, a[210];
  cin >> n;
  n *= 2;
  for (i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  int b = 0, c = 0;
  for (i = 0; i < n; i += 2) {
    b += a[i];
  }
  for (i = 1; i < n - 1; i += 2) {
    c += a[i];
  }
  c += a[0];
  cout << max(b, c) << endl;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef int64_t Int;
#define dump(x) cout << (x) << '\n'
Int mod = 1e9 + 7;

Int gcd(Int n, Int m) {
  Int a = max(n, m);
  Int b = min(n, m);
  return a % b == 0 ? b : gcd(b, a % b);
}

int main() {
  Int n, x;
  cin >> n >> x;
  if (n % x == 0) {
    dump((n - x) * 3LL);
    return 0;
  }
  Int res = x;
  Int tmp = n - x;
  n = x;
  x = tmp;
  // cout << n << " " << x << endl;
  res += x + (n / x) * (2LL * x);
  while (true) {
    tmp = n % x;
    n = x;
    x = tmp;
    // cout << n << " " << x << endl;
    if (n % x == 0) {
      res += (n / x) * (2LL * x) - x;
      break;
    } else {
      res += (n / x) * (2LL * x);
    }
  }
  dump(res);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef int64_t Int;
#define dump(x) cout << (x) << '\n'
Int mod = 1e9 + 7;

Int gcd(Int n, Int m) {
  Int a = max(n, m);
  Int b = min(n, m);
  return a % b == 0 ? b : gcd(b, a % b);
}

int main() {
  Int n, x;
  cin >> n >> x;
  if (n % x == 0) {
    dump((n - x) * 3LL);
    return 0;
  }
  Int res = x;
  Int tmp = n - x;
  n = x;
  x = tmp;
  if (n % x == 0) {
    dump(res + 2LL * (n / x));
    return 0;
  }
  // cout << n << " " << x << endl;
  res += x + (n / x) * (2LL * x);
  while (true) {
    tmp = n % x;
    n = x;
    x = tmp;
    // cout << n << " " << x << endl;
    if (n % x == 0) {
      res += (n / x) * (2LL * x) - x;
      break;
    } else {
      res += (n / x) * (2LL * x);
    }
  }
  dump(res);
  return 0;
}
Problem p04048 (C++)

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

using namespace std;

typedef long long ll;

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

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

Fixed submission:
#include <iostream>

using namespace std;

typedef long long ll;

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

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

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

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define ll long long
using namespace std;
int n, x;
ll dfs(ll a, ll b) {
  if (b % a == 0)
    return 2 * b - a;
  return b / a * a * 2 + dfs(b % a, a);
}
int main() {
  cin >> n >> x;
  cout << dfs(x, n - x) + n;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define ll long long
using namespace std;
ll n, x;
ll dfs(ll a, ll b) {
  if (b % a == 0)
    return 2 * b - a;
  return b / a * a * 2 + dfs(b % a, a);
}
int main() {
  cin >> n >> x;
  cout << dfs(x, n - x) + n;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pint pair<int, int>
using namespace std;

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

ll fun(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b) {
    ll temp = a;
    a = b;
    b = temp;
  }
  if (a % b == 0) {
    ll temp = a / b;
    return (2 * temp - 1) * b;
  }

  ll temp = a / b;

  return 2 * b + fun(a - b, b);
}

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

  cout << n + fun(n - x, x) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define pint pair<int, int>
using namespace std;

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

ll fun(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b) {
    ll temp = a;
    a = b;
    b = temp;
  }
  if (a % b == 0) {
    ll temp = a / b;
    return (2 * temp - 1) * b;
  }

  ll temp = a / b;

  return 2 * temp * b + fun(a % b, b);
}

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

  cout << n + fun(n - x, x) << endl;

  return 0;
}
Problem p04048 (C++)

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

#define mp make_pair
#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
typedef long long ll;

ll get(ll n, ll m) {
  if (n == m)
    return n;
  if (n > m)
    swap(n, m);
  return 2 * n + get(n, m - n);
}

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


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

#define mp make_pair
#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
typedef long long ll;

ll get(ll n, ll m) {
  if (n == m)
    return n;
  if (n > m)
    swap(n, m);
  // m-n*z <= n
  // m <= n * (z+1)
  // m / n <= z+1
  ll z = (m + n - 1) / n - 1;
  return 2LL * z * n + get(n, m - z * n);
}

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

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

#define REPU(i, a, b) for (int i = (a); i < (b); ++i)
#define REPD(i, a, b) for (int i = (a); i > (b); --i)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
#define DEBUG(x) cerr << #x << " = " << x << endl

typedef long long ll;
const int MOD = 1000000007;

template <class T, class U> inline T tmin(T a, U b) { return (a < b) ? a : b; }
template <class T, class U> inline T tmax(T a, U b) { return (a > b) ? a : b; }
template <class T, class U> inline void amax(T &a, U b) {
  if (b > a)
    a = b;
}
template <class T, class U> inline void amin(T &a, U b) {
  if (b < a)
    a = b;
}
template <class T> inline T tabs(T a) { return (a > 0) ? a : -a; }
template <class T> T gcd(T a, T b) {
  while (b != 0) {
    T c = a;
    a = b;
    b = c % b;
  }
  return a;
}

ll f(ll N, ll X) {
  if (N <= 1 || X == 0 || X == N)
    return 0;
  if (X + X >= N) {
    return f(X, X + X - N) + 3 * (N - X);
  } else {
    ll k = N / X - 1;
    return f(N - k * X, X) + 3 * k * X;
  }
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);

  ll N, X;

  cin >> N >> X;
  cout << f(N, X) << endl;

  return 0;
}

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

#define REPU(i, a, b) for (int i = (a); i < (b); ++i)
#define REPD(i, a, b) for (int i = (a); i > (b); --i)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
#define DEBUG(x) cerr << #x << " = " << x << endl

typedef long long ll;
const int MOD = 1000000007;

template <class T, class U> inline T tmin(T a, U b) { return (a < b) ? a : b; }
template <class T, class U> inline T tmax(T a, U b) { return (a > b) ? a : b; }
template <class T, class U> inline void amax(T &a, U b) {
  if (b > a)
    a = b;
}
template <class T, class U> inline void amin(T &a, U b) {
  if (b < a)
    a = b;
}
template <class T> inline T tabs(T a) { return (a > 0) ? a : -a; }
template <class T> T gcd(T a, T b) {
  while (b != 0) {
    T c = a;
    a = b;
    b = c % b;
  }
  return a;
}

ll f(ll N, ll X) {
  if (N <= 1 || X == 0 || X == N)
    return 0;
  if (X + X >= N) {
    return f(X, N - X) + 3 * (N - X);
  } else {
    ll k = N / X - 1;
    return f(N - k * X, X) + 3 * k * X;
  }
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);

  ll N, X;

  cin >> N >> X;
  cout << f(N, X) << endl;

  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1000000007;
const int BASE = 27;
const int oo = 1e9;
const double EPS = 1e-9;
const int MX = 1e5 + 1;

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

ll n, x;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
typedef long long ll;
const int MOD = 1000000007;
const int BASE = 27;
const int oo = 1e9;
const double EPS = 1e-9;
const int MX = 1e5 + 1;

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

ll n, x;

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

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

#define MP make_pair
#define PB push_back
#define ST first
#define ND second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef long long LL;

void init_ios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

const int N = 110;

int n, x;

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

int licz(int n, int x) {
  if (n - x == x)
    return 3 * x;
  if (n - x > x)
    return n + x + rowno(x, n - 2 * x);
  if (x % (n - x) == 0)
    return 3 * x;
  return n + (x / (n - x)) * (n - x) * 2 + x % (n - x) +
         rowno(x % (n - x), n - x - (x % (n - x)));
}

signed main() {
  init_ios();
  cin >> n >> x;
  cout << licz(n, x) << "\n";
}

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

#define MP make_pair
#define PB push_back
#define ST first
#define ND second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef long long LL;

void init_ios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

const int N = 110;

int n, x;

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

int licz(int n, int x) {
  if (n - x == x)
    return 3 * x;
  if (n - x > x)
    return n + x + rowno(x, n - 2 * x);
  if (x % (n - x) == 0)
    return 3 * x;
  return n + (x / (n - x)) * (n - x) * 2 + x % (n - x) +
         rowno(x % (n - x), n - x - (x % (n - x)));
}

signed main() {
  init_ios();
  cin >> n >> x;
  cout << licz(n, x) << "\n";
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> IP;
typedef vector<ll> V;
typedef vector<V> V2;
typedef vector<vector<P>> G;
void g_dir(G &graph, ll a, ll b, ll w = 1) { graph[a].push_back(P(b, w)); }
void g_undir(G &graph, ll a, ll b, ll w = 1) {
  g_dir(graph, a, b, w);
  g_dir(graph, b, a, w);
}
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; (i)--)
template <class T> void chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
  }
}
const ll INF = 1145141919;
const ll MOD = 1000000007;
const ll NUM = 101010;

int main() {

  ll N, X;
  cin >> N >> X;
  ll ans = N;
  ll a = X, b = N - X;
  for (;;) {
    if (a > b)
      swap(a, b);
    if (a == b) {
      ans += a;
      break;
    }
    ans += 2 * a;
    b -= a;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> IP;
typedef vector<ll> V;
typedef vector<V> V2;
typedef vector<vector<P>> G;
void g_dir(G &graph, ll a, ll b, ll w = 1) { graph[a].push_back(P(b, w)); }
void g_undir(G &graph, ll a, ll b, ll w = 1) {
  g_dir(graph, a, b, w);
  g_dir(graph, b, a, w);
}
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; (i)--)
template <class T> void chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
  }
}
const ll INF = 1145141919;
const ll MOD = 1000000007;
const ll NUM = 101010;

int main() {

  ll N, X;
  cin >> N >> X;
  ll ans = N;
  ll a = X, b = N - X;
  for (;;) {
    if (a > b)
      swap(a, b);
    if (a == b) {
      ans += a;
      break;
    }
    ll c = b / a;
    if (c > 1)
      c--;
    ans += 2 * c * a;
    b -= c * a;
  }
  cout << ans << endl;

  return 0;
}
Problem p04048 (C++)

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

typedef long long ll;

int main() {
  ll N, X, ans;
  cin >> N >> X;
  ans = X;
  while (true) {
    if (2 * X == N) {
      ans += N;
      break;
    }
    if (2 * X < N) {
      ans += N;
      N = N - X;
      X = N - X;
    } else {
      ll side = N - X, tmp1 = X / side, tmp2 = X % side;
      ans += tmp1 * 2 * side;
      N = N - tmp1 * side;
      X = X - tmp1 * side;
    }
  }
  cout << ans << endl;
  return 0;
}


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

typedef long long ll;

int main() {
  ll N, X, ans;
  cin >> N >> X;
  ans = X;
  while (X != 0) {
    if (2 * X == N) {
      ans += N;
      break;
    }
    if (2 * X < N) {
      ans += N;
      N = N - X;
      X = N - X;
    } else {
      ll side = N - X, tmp1 = X / side, tmp2 = X % side;
      ans += tmp1 * 2 * side;
      N = N - tmp1 * side;
      X = X - tmp1 * side;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

long long int N, X;

long long int gcd(long long int m, long long int n) {
  if (0 == m || 0 == n) {
    return 0;
  }

  while (m != n) {
    if (m > n) {
      m = m - n;
    } else {
      n = n - m;
    }
  }
  return m;
}

int main(void) {
  scanf("%lld%lld", &N, &X);
  printf("%lld\n", 3 * (N - gcd(N, X)));
  return 0;
}

Fixed submission:
#include <stdio.h>

long long int N, X;

long long int gcd(long long int m, long long int n) {
  while (n != 0) {
    long long int temp = m % n;
    m = n;
    n = temp;
  }
  return m;
}

int main(void) {
  scanf("%lld%lld", &N, &X);
  printf("%lld\n", 3 * (N - gcd(N, X)));
  return 0;
}
Problem p04048 (C++)

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

int64_t f(int64_t a, int64_t b) {
  if (a == b) {
    return a;
  }

  if (a > b) {
    std::swap(a, b);
  }

  if (b % a != 0) {
    return 2 * a * (b / a) + f(a, b % a);
  } else {
    return 2 * a + f(a, b - a);
  }
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int64_t n, x;
  std::cin >> n >> x;

  std::cout << n + f(x, n - x) << '\n';
}

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

int64_t f(int64_t a, int64_t b) {
  if (a == b) {
    return a;
  }

  if (a > b) {
    std::swap(a, b);
  }

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

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int64_t n, x;
  std::cin >> n >> x;

  std::cout << n + f(x, n - x) << '\n';
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define si short int
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pill pair<ll, ll>
#define f first
#define s second
#define pilc pair<ll, char>
#define all(a) (a).begin(), (a).end()
#define rep(s, e, step) for (int i = (s); i < (e); i += step)
#define vrep(s, e, step) for (int j = (s); j < (e); j += step)
#define ex exit(0)
#define sz(a) (a).size()

using namespace std;

const ll N = 1e3;
const ll big = 1e18;
const ll block = 800;
const ll mod = 1e6;

ll n, x;

int main() {
  speed;
  cin >> n >> x;
  ll z = n - 2 * x;
  ll f = x / z;
  cout << x * 3 + (n - x) + (f * 2 - 1) * z;
}

/*
        Code written by
        Codeforces : I_Love_Trott
        mail : dzhon.snoudin@mail.ru
*/


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define si short int
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pill pair<ll, ll>
#define f first
#define s second
#define pilc pair<ll, char>
#define all(a) (a).begin(), (a).end()
#define rep(s, e, step) for (int i = (s); i < (e); i += step)
#define vrep(s, e, step) for (int j = (s); j < (e); j += step)
#define ex exit(0)
#define sz(a) (a).size()

using namespace std;

const ll N = 1e3;
const ll big = 1e18;
const ll block = 800;
const ll mod = 1e6;

ll n, x;

int main() {
  speed;
  cin >> n >> x;
  cout << 3 * (n - __gcd(n, x));
}

/*
        Code written by
        Codeforces : I_Love_Trott
        mail : dzhon.snoudin@mail.ru
*/
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ull = unsigned long long;
using ll = long long;
using lli = long long int;
using ld = long double;
using pa = pair<int, int>;
#define SORT(v, n) sort(v, v + n);
#define ALL(v) v.begin(), v.end()
#define VSORT(v) sort(ALL(v))
#define GRESORT(v) sort(ALL(v), greater<ll>())
#define REVERSE(v) reverse(ALL(v))
#define overlap(v) v.erase(unique(ALL(v)), v.end())
#define debug(x) cout << #x << ": " << x << endl
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (b - 1); i >= a; i--)
#define rrep(i, n) RFOR(i, 0, n)
#define INF 999999999
#define mins(a, b) a = min(a, b)
#define maxs(a, b) a = max(a, b)

// 4近傍
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

// 8近傍
// int dy[]={0,0,1,-1,1,1,-1,-1};
// int dx[]={1,-1,0,0,1,-1,1,-1};

ll mod = 1000000007;

ll gcd(ll n, ll x) {
  while (n != x)
    n > x ? n -= x : x -= n;
  return n;
}

void solve() {
  ll n, x;
  cin >> n >> x;
  cout << 3 * (n - gcd(n, x)) << endl;
}

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

  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ull = unsigned long long;
using ll = long long;
using lli = long long int;
using ld = long double;
using pa = pair<int, int>;
#define SORT(v, n) sort(v, v + n);
#define ALL(v) v.begin(), v.end()
#define VSORT(v) sort(ALL(v))
#define GRESORT(v) sort(ALL(v), greater<ll>())
#define REVERSE(v) reverse(ALL(v))
#define overlap(v) v.erase(unique(ALL(v)), v.end())
#define debug(x) cout << #x << ": " << x << endl
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (b - 1); i >= a; i--)
#define rrep(i, n) RFOR(i, 0, n)
#define INF 999999999
#define mins(a, b) a = min(a, b)
#define maxs(a, b) a = max(a, b)

// 4近傍
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

// 8近傍
// int dy[]={0,0,1,-1,1,1,-1,-1};
// int dx[]={1,-1,0,0,1,-1,1,-1};

ll mod = 1000000007;

ll gcd(ll n, ll x) {
  if (n == 0)
    return x;
  return gcd(x % n, n);
}

void solve() {
  ll n, x;
  cin >> n >> x;
  cout << 3 * (n - gcd(n, x)) << endl;
}

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

  solve();

  return 0;
}
Problem p04048 (C++)

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

int main() {
  int N, X, a;
  int long_length, short_length;
  int ans;
  cin >> N >> X;
  if (N / 2 < X)
    X = N - X;
  ans = N;
  long_length = N - X;
  short_length = X;
  while (1) {
    a = long_length % short_length;
    if (a == 0) {
      ans += (2 * long_length - short_length);
      cout << ans;
      return 0;
    } else {
      ans += ((long_length - a) * 2);
      long_length = short_length;
      short_length = a;
    }
  }
}


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

int main() {
  long long int N, X, a;
  long long int long_length, short_length;
  long long int ans;
  cin >> N >> X;
  if (N / 2 < X)
    X = N - X;
  ans = N;
  long_length = N - X;
  short_length = X;
  while (1) {
    a = long_length % short_length;
    if (a == 0) {
      ans += (2 * long_length - short_length);
      cout << ans;
      return 0;
    } else {
      ans += ((long_length - a) * 2);
      long_length = short_length;
      short_length = a;
    }
  }
}
Problem p04048 (C++)

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

ll n, x;

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

int main() {
  ios::sync_with_stdio(0);
  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 n, x;

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

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#define ll long long int
int main(void) {
  ll N, X;
  std::cin >> N >> X;

  ll a[2];
  a[0] = X;
  a[1] = N;

  ll dist = 0;
  while (a[0] != a[1]) {
    a[1] -= a[0];
    std::sort(a, a + 2);
    dist += a[0] * 3;
  }
  std::cout << dist << std::endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define ll long long int
int main(void) {
  ll N, X;
  std::cin >> N >> X;

  ll a[2];
  a[0] = X;
  a[1] = N;

  ll dist = 0;
  while (a[0] != a[1]) {
    if (a[1] > a[0] * 1000005) {
      a[1] -= a[0] * 1000000;
      dist += a[0] * 3000000;
    }
    a[1] -= a[0];
    std::sort(a, a + 2);
    dist += a[0] * 3;
  }
  std::cout << dist << std::endl;
}
Problem p04048 (C++)

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

signed main() {
  int n, x;
  cin >> n >> x;
  int a = x, b = n - x, c = n;
  int res = x;
  if (a == b)
    return cout << x * 3, 0;
  while (true) {
    if (a > b) {
      res += b * 2;
      a -= b;
      c -= b;
    } else if (a < b) {
      res += c;
      int an, bn, cn;
      an = 2 * b - c;
      bn = c - b;
      cn = b;
      a = an;
      b = bn;
      c = cn;
    } else {
      res += b * 2;
      return cout << res, 0;
    }
  }
}

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

signed main() {
  int n, x;
  cin >> n >> x;
  int a = x, b = n - x, c = n;
  int res = x;
  if (a == b)
    return cout << x * 3, 0;
  while (true) {
    if (a > b) {
      int t = a / b;
      if (t > 1)
        --t;
      res += t * b * 2;
      cerr << t << "\n";
      a -= t * b;
      c -= t * b;
      cerr << "a,c:" << a << "," << c << "\n";
    } else if (a < b) {
      res += c;
      int an, bn, cn;
      an = 2 * b - c;
      bn = c - b;
      cn = b;
      a = an;
      b = bn;
      c = cn;
    } else {
      res += b * 2;
      return cout << res, 0;
    }
  }
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12; // 10^12
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
const double EPS = 1E-8;
////////////////////////////////////////

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

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

  ll n, x;
  cin >> n >> x;
  ll ans = n + f(n - x, x);
  cout << ans << endl;

  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12; // 10^12
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
const double EPS = 1E-8;
////////////////////////////////////////

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

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

  ll n, x;
  cin >> n >> x;
  ll ans = n + f(n - x, x);
  cout << ans << endl;

  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;

ll n, x, ans;

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

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;

ll n, x, ans;

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  ll N, X;
  cin >> N >> X;
  if (X > N - X)
    X = N - X;
  ll Tmp = X + N;
  ll L = N - 2 * X;
  while (1) {
    if (X >= L) {
      if (X % L == 0) {
        Tmp += X + L * (2 * X / L - 1);
        break;
      } else {
        Tmp += X + 2 * L * (X / L) + X % L;
        ll nextX = X % L;
        ll nextL = L - X % L;
        X = nextX;
        L = nextL;
      }
    } else {
      if (L % X == 0) {
        Tmp += 2 * L;
        break;
      } else {
        Tmp += 2 * X * (L / X);
        L = L % X;
      }
    }
  }
  cout << Tmp << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)

int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  ll N, X;
  cin >> N >> X;
  if (X > N - X)
    X = N - X;
  ll Tmp = X + N;
  ll L = N - 2 * X;
  while (L != 0) {
    if (X >= L) {
      if (X % L == 0) {
        Tmp += X + L * (2 * X / L - 1);
        break;
      } else {
        Tmp += X + 2 * L * (X / L) + X % L;
        ll nextX = X % L;
        ll nextL = L - X % L;
        X = nextX;
        L = nextL;
      }
    } else {
      if (L % X == 0) {
        Tmp += 2 * L;
        break;
      } else {
        Tmp += 2 * X * (L / X);
        L = L % X;
      }
    }
  }
  cout << Tmp << endl;

  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define REEP(i, N) for (ll i = N - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define PB push_back
#define KIRIAGE(a, b) a += (b - a % b) % b // a を b の倍数値まで増やす
#define CEIL(c, a, b)                                                          \
  if (a % b == 0) {                                                            \
    c = a / b;                                                                 \
  } else {                                                                     \
    c = a / b + 1;                                                             \
  } // a / b 以上の最小整数
#define sp(n) setprecision(n)
#define INF 1000000000
#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(void) {
  ll N, X;
  cin >> N >> X;
  ll ans = N;
  ll nokori = X;
  if (2 * X <= N) {
    ans += 2 * X;
    ans += (nokori / (N - 2 * X) * 2 - 1) * (N - 2 * X);
  } else
    ans += (nokori / (N - X) * 2 - 1) * (N - X);
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define REEP(i, N) for (ll i = N - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define PB push_back
#define KIRIAGE(a, b) a += (b - a % b) % b // a を b の倍数値まで増やす
#define CEIL(c, a, b)                                                          \
  if (a % b == 0) {                                                            \
    c = a / b;                                                                 \
  } else {                                                                     \
    c = a / b + 1;                                                             \
  } // a / b 以上の最小整数
#define sp(n) setprecision(n)
#define INF 1000000000
#define MOD 1000000007

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(void) {
  ll N, X;
  cin >> N >> X;
  ll ans = N;
  ll nokori = N - X;
  ll step = X;
  while (nokori % step) {
    ans += (nokori / step) * step * 2;
    ll gomi = nokori;
    nokori = step;
    step = gomi % step;
  }
  ans += 2 * nokori - step;
  cout << ans << endl;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, X;
  cin >> N >> X;
  long long int A = N;
  N -= X;
  while (true) {
    if (N > X)
      swap(N, X);
    if (X % N == 0) {
      A += (2 * (X / N) * N - N);
      break;
    }
    A += (2 * (X / N) * N);
    X %= N;
  }

  cout << A << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long int N, X;
  // cin >> N >> X;
  scanf("%lld%lld", &N, &X);
  long long int A = N;
  N -= X;
  while (true) {
    if (N > X)
      swap(N, X);
    if (X % N == 0) {
      A += (2 * (X / N) * N - N);
      break;
    }
    A += (2 * (X / N) * N);
    X %= N;
  }

  cout << A << endl;

  return 0;
}
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#if 1
#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define int long long
constexpr int mod = 1000000007;

#define LOADVECTOR(type, name, N)                                              \
  std::vector<type> name(N);                                                   \
  for (int nnn = 0; nnn < N; ++nnn) {                                          \
    cin >> name[nnn];                                                          \
  }

#define LOAD(type, name)                                                       \
  type name;                                                                   \
  cin >> name;

int daikei(int n, int x) {
  if (2 * x == n) {
    return n;
  } else if (2 * x >= n) {
    return 2 * (n - x) + daikei(x, 2 * x - n);
  } else {
    return n + daikei(n - x, n - 2 * x);
  }
}

signed main() {
  ios::sync_with_stdio(false);
  LOAD(int, N);
  LOAD(int, X);

  cout << X + daikei(N, X) << endl;
  return 0;
}
#endif


Fixed submission:
#if 1
#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define int long long
constexpr int mod = 1000000007;

#define LOADVECTOR(type, name, N)                                              \
  std::vector<type> name(N);                                                   \
  for (int nnn = 0; nnn < N; ++nnn) {                                          \
    cin >> name[nnn];                                                          \
  }

#define LOAD(type, name)                                                       \
  type name;                                                                   \
  cin >> name;

int daikei(int n, int x) {
  if (2 * x == n) {
    return n;
  }
  int k = x / (n - x);
  if (2 <= k)
    return 2 * (k - 1) * (n - x) +
           daikei(n - (k - 1) * (n - x), x - (k - 1) * (n - x));
  if (2 * x >= n) {
    return 2 * (n - x) + daikei(x, 2 * x - n);
  } else {
    return n + daikei(n - x, n - 2 * x);
  }
}

signed main() {
  ios::sync_with_stdio(false);
  LOAD(int, N);
  LOAD(int, X);

  cout << X + daikei(N, X) << endl;
  return 0;
}
#endif
Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
long long n, x;
long long ans;

long long solve(long long a, long long b) {
  if (a > b) {
    swap(a, b);
  }
  if (a == b) {
    return a;
  }

  return 2 * a + solve(a, b - a);
}

int main() {
  cin >> n >> x;
  ans = n;
  ans += solve(x, n - x);
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
long long n, x;
long long ans;

long long solve(long long a, long long b) {
  if (a > b) {
    swap(a, b);
  }
  if (a == b) {
    return a;
  }

  long long r = b % a;
  long long d = b / a;
  if (r == 0) {
    r += a;
    d--;
  }

  return 2 * a * d + solve(a, r);
}

int main() {
  cin >> n >> x;
  ans = n;
  ans += solve(x, n - x);
  cout << ans << "\n";
  return 0;
}
Problem p04048 (C++)

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

ll f(ll a, ll b) {
  ll aa = min(a, b);
  ll bb = max(a, b);
  if (aa == bb)
    return aa;
  //  return 2*aa+f(aa,bb-aa);
  if (bb % aa == 0)
    return 2 * (bb / aa) * aa + f(aa, bb % aa);
  return 2 * aa + f(aa, bb - aa);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(std::numeric_limits<float>::max_digits10);
  ll n, x;
  cin >> n >> x;
  cout << n + f(n - x, x) << endl;
  return 0;
}


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

ll f(ll aa, ll bb) {
  ll a = min(aa, bb);
  ll b = max(aa, bb);
  if (a == b)
    return a;
  // b>a
  if (b % a == 0)
    return 2 * b - a;
  // return 2*a+f(a,b-a);
  //    return a*(1+2*(b/a-1));
  //    return 2*b-a;
  return 2 * (b / a) * a + f(a, b % a);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(std::numeric_limits<float>::max_digits10);
  ll n, x;
  cin >> n >> x;
  cout << n + f(n - x, x) << endl;
  return 0;
}
Problem p04048 (C++)

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

long long calc(long long a, long long b) {
  long long d;
  if (a > b) {
    d = a / b;
    if (a == b * d) {
      return b * (2 * d - 1);
    } else {
      return 2 * b * d + calc(b, a - (b * d));
    }
  } else {
    return calc(b, a);
  }
}

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

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

long long calc(long long a, long long b) {
  long long d;
  if (a >= b) {
    d = a / b;
    if (a == b * d) {
      return b * (2 * d - 1);
    } else {
      return 2 * b * d + calc(b, a - (b * d));
    }
  } else {
    return calc(b, a);
  }
}

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

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

using namespace std;

typedef long long int LL;
typedef int ll;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#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 smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))

const ll M = 3e5 + 5, LG = 26, SM = 600, inf = 1e9 + 1;
ll mod = 95542721;

ll n, x;

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

int main() {
  ios::sync_with_stdio(0);
  cin >> n >> x;
  cout << calc(x, n - x) + n;
}

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

using namespace std;

typedef long long int LL;
typedef LL ll;
typedef string str;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;

#define X first
#define Y second
#define PB push_back
#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 smax(a, b) a = max(a, b)
#define smin(a, b) a = min(a, b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a, n, x) (lower_bound(a, (a) + (n), x) - (a))

const ll M = 3e5 + 5, LG = 26, SM = 600, inf = 1e9 + 1;
ll mod = 95542721;

ll n, x;

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

int main() {
  ios::sync_with_stdio(0);
  cin >> n >> x;
  cout << calc(x, n - x) + n;
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
// Author: Vamsi Krishna Reddy Satti
// With love for Competitive Programming!

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

#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);

using ll = int64_t;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

// ------------------------------------------------------------------------------------------------

ll n, x;

ll f(ll l, ll w) {
  if (w == 1) {
    return 2 * l - 1;
  }
  return 2 * (l / w) * w + f(w, l % w);
}

void func() {
  cin >> n >> x;
  cout << n + f(n - x, x) << endl;
}

int main() {
  fast_io;
  cout.precision(32);
  ll t = 1; // cin >> t;
  for (ll i = 1; i <= t; i++) {
    func();
  }
}


Fixed submission:
// Author: Vamsi Krishna Reddy Satti
// With love for Competitive Programming!

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

#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);

using ll = int64_t;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

// ------------------------------------------------------------------------------------------------

ll n, x;

ll f(ll l, ll w) {
  if (w == 0) {
    return -l;
  }
  return 2 * (l / w) * w + f(w, l % w);
}

void func() {
  cin >> n >> x;
  cout << n + f(n - x, x) << endl;
}

int main() {
  fast_io;
  cout.precision(32);
  ll t = 1; // cin >> t;
  for (ll i = 1; i <= t; i++) {
    func();
  }
}
Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll Mod = 1000000007;
int main() {
  ll N, X;
  cin >> N >> X;
  ll a = min(X, N - X);
  ll b = max(X, N - X);
  ll ans = N;
  ans += (b / a) * 2 * a;
  while (0 == 0) {
    int c = b % a;
    b = a;
    a = c;
    ans += ((b / a) * 2 * a);
    if (b % a == 0) {
      break;
    }
  }
  ans -= a;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll Mod = 1000000007;
int main() {
  ll N, X;
  cin >> N >> X;
  ll a = min(X, N - X);
  ll b = max(X, N - X);
  ll ans = N;
  ans += (b / a) * 2 * a;
  while (b % a != 0) {
    ll c = b % a;
    b = a;
    a = c;
    ans += ((b / a) * 2 * a);
    if (b % a == 0) {
      break;
    }
  }
  ans -= a;
  cout << ans << endl;
  return 0;
}