Problem p04048 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

long long int INF = 1e18;
long long int mod = 1000000007;
double Pi = 3.1415926535897932384626;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
char dir[] = "DRUL";
ll bit[500005];
// ↓,→,↑,←

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(15) << x << endl;
#define pu(x) cout << x;
#define re(i, n)                                                               \
  for (i = 0; i < n; i++)                                                      \
    ;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout << #x << " = " << x << endl

ll rui(ll abc, ll bed) {
  // abcのbed乗を計算する
  if (bed == 0) {
    return 1;
  } else {
    ll ced = rui(abc, bed / 2);
    ced *= ced;
    ced %= mod;
    if (bed % 2 == 1) {
      ced *= abc;
      ced %= mod;
    }
    return ced;
  }
}
ll gcd(ll number1, ll number2) {
  if (number1 > number2) {
    swap(number1, number2);
  }
  if (number1 == 0 || number1 == number2) {
    return number2;
  } else {
    return gcd(number2 % number1, number1);
  }
}

ll i, j, k, ii, jj, r, l;
ll n, num, sum, ans;
ll a, b, c, d, e, g, h;
// ll x[500005],y[500005],z[500005];
ll dp1[1050000], dp2[1050000];
double x[500005], w[500005];

ll bitsum(ll i) {
  ll s = 0;
  while (i > 0) {
    // p("hoi");
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void bitadd(ll b, ll x) {
  while (b <= n) {
    // p("hoi");
    bit[b] += x;
    b += b & -b;
  }
}

void solve(ll a, ll b) {
  if (a < b)
    swap(a, b);
  // a > bにする
  if (b == 1) {
    ans += 2 * a - 1;
    return;
  } else if (a == b) {
    ans += a;
  } else {
    ans += (a / b) * b * 2;
    solve(b, a % b);
  }
}

int main() {
  cin >> n >> k;
  ans = n;
  solve(k, n - k);
  p(ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

long long int INF = 1e18;
long long int mod = 1000000007;
double Pi = 3.1415926535897932384626;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop

int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
char dir[] = "DRUL";
ll bit[500005];
// ↓,→,↑,←

#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(15) << x << endl;
#define pu(x) cout << x;
#define re(i, n)                                                               \
  for (i = 0; i < n; i++)                                                      \
    ;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout << #x << " = " << x << endl

ll rui(ll abc, ll bed) {
  // abcのbed乗を計算する
  if (bed == 0) {
    return 1;
  } else {
    ll ced = rui(abc, bed / 2);
    ced *= ced;
    ced %= mod;
    if (bed % 2 == 1) {
      ced *= abc;
      ced %= mod;
    }
    return ced;
  }
}
ll gcd(ll number1, ll number2) {
  if (number1 > number2) {
    swap(number1, number2);
  }
  if (number1 == 0 || number1 == number2) {
    return number2;
  } else {
    return gcd(number2 % number1, number1);
  }
}

ll i, j, k, ii, jj, r, l;
ll n, num, sum, ans;
ll a, b, c, d, e, g, h;
// ll x[500005],y[500005],z[500005];
ll dp1[1050000], dp2[1050000];
double x[500005], w[500005];

ll bitsum(ll i) {
  ll s = 0;
  while (i > 0) {
    // p("hoi");
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void bitadd(ll b, ll x) {
  while (b <= n) {
    // p("hoi");
    bit[b] += x;
    b += b & -b;
  }
}

void solve(ll a, ll b) {
  if (a < b)
    swap(a, b);
  // a > bにする
  if (b == 1) {
    ans += 2 * a - 1;
    return;
  } else if (a == b) {
    ans += a;
  } else if (a % b == 0) {
    ans += (a / b) * b * 2 - b;
  } else {
    ans += (a / b) * b * 2;
    solve(b, a % b);
  }
}

int main() {
  cin >> n >> k;
  ans = n;
  solve(k, n - k);
  p(ans);

  return 0;
}