Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define rg register
#define il inline
#define lch(x) ((x) << 1)
#define rch(x) ((x) << 1 ^ 1)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep0(i, n) for (register int i = 0; i < (n); ++i)
#define per0(i, n) for (register int i = (n)-1; i >= 0; --i)
#define rep(i, st, ed) for (register int i = (st); i <= (ed); ++i)
#define per(i, ed, st) for (register int i = (ed); i >= (st); --i)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> il T qmin(const T &a, const T &b) {
  return a < b ? a : b;
}
template <typename T> il T qmax(const T &a, const T &b) {
  return a > b ? a : b;
}
template <typename T> il void getmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <typename T> il void getmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
il void fileio(string s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const int inf = (int)1e9 + 7;
const ll linf = (ll)1e17 + 7;

ll n, x, y;

il ll solve(ll x, ll y, bool tp) {
  if (!tp) {
    if (x % y == 0)
      return x * 3;
    return x + x / y * (y << 1) + y + x % y + solve(x % y, y - x % y, 1);
  } else {
    if (x % y == 0)
      return x * 3 - y;
    return x + x / y * (y << 1) + x % y + solve(x % y, y - x % y, 1);
  }
}

int main() {
  scanf("%lld%lld", &n, &x);
  y = n - x;
  if (x % y == 0)
    return printf("%lld\n", 3 * x), 0;
  ll ans = x + x / y * (y << 1) + y + x % y;
  x %= y, y -= x;
  while (x) {
    if (x % y == 0) {
      ans += x * 3 - y;
      break;
    }
    ans += x + x / y * (y << 1) + x % y;
    x %= y, y -= x;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define rg register
#define il inline
#define lch(x) ((x) << 1)
#define rch(x) ((x) << 1 ^ 1)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep0(i, n) for (register int i = 0; i < (n); ++i)
#define per0(i, n) for (register int i = (n)-1; i >= 0; --i)
#define rep(i, st, ed) for (register int i = (st); i <= (ed); ++i)
#define per(i, ed, st) for (register int i = (ed); i >= (st); --i)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> il T qmin(const T &a, const T &b) {
  return a < b ? a : b;
}
template <typename T> il T qmax(const T &a, const T &b) {
  return a > b ? a : b;
}
template <typename T> il void getmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <typename T> il void getmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
il void fileio(string s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const int inf = (int)1e9 + 7;
const ll linf = (ll)1e17 + 7;

ll n, x, y;

il ll solve(ll x, ll y, bool tp) {
  if (!tp) {
    if (x % y == 0)
      return x * 3;
    return x + x / y * (y << 1) + y + x % y + solve(x % y, y - x % y, 1);
  } else {
    if (x % y == 0)
      return x * 3 - y;
    return x + x / y * (y << 1) + x % y + solve(x % y, y - x % y, 1);
  }
}

int main() {
  scanf("%lld%lld", &n, &x);
  y = n - x;
  if (x % y == 0)
    return printf("%lld\n", 3 * x), 0;
  ll ans = x + x / y * (y << 1) + y + x % y;
  x %= y, y -= x;
  while (x) {
    if (x % y == 0) {
      ans += x * 3 - y;
      break;
    }
    if (y % x == 0) {
      ans += y << 1;
      break;
    }
    if (x > y) {
      ans += x + x / y * (y << 1) + x % y;
      x %= y, y -= x;
    } else {
      ans += y / x * (x << 1);
      y %= x;
    }
  }
  printf("%lld\n", ans);
  return 0;
}