Problem p04048 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int gcd(int a, int b) {
  while (b) {
    int m = a % b;
    a = b;
    b = m;
  }
  return a;
}

int lcm(int a, int b) { return (a * b) / gcd(a, b); }

int isPrim(int a) {
  if (a == 1) {
    return 0;
  }
  for (int i = 2; i <= (a + 1) / 2; i++) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}

long long mod_pow(long long x, long long n, long long mod) {
  // xのn乗を計算するのにn乗を2進表記にして計算
  // x^22 = x^16 + x^4 + x^2
  long long ret = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      ret = (ret * x) % mod; // 答えに付加
    }
    x = (x * x) % mod; // 2乗
    n = n / 2;
  }
  return ret;
}

struct VV {
  int x;
  int y;
  int z;
};

class LessInt {
public:
  bool operator()(const VV &riLeft, const VV &riRight) const {
    if (riLeft.x == riRight.x) {
      return riLeft.y > riRight.y;
    }
    return riLeft.x > riRight.x;
  }
};

class indInt {
public:
  bool operator()(const VV &riLeft, const VV &riRight) const {
    return riLeft.x < riRight.x;
  }
};

struct XX {
  int a;
  int b;
};

class xxIntu {
public:
  bool operator()(const XX &riLeft, const XX &riRight) const {
    // 第1条件
    if (riLeft.a != riRight.a) {
      return riLeft.a > riRight.a; //<:昇順(小さい順)、>:降順(大きい順)
    }
    // 第2条件
    return riLeft.b > riRight.b;
  }
};
bool operator<(const XX &riLeft, const XX &riRight) {
  return riLeft.a < riRight.a;
};
bool operator>(const XX &riLeft, const XX &riRight) {
  return riLeft.a > riRight.a;
};

int f(int n, int r) {
  int si = 1;
  int bo = 1;
  for (int i = 0; i < r; i++) {
    si = si * (si - i);
  }
  for (int i = 0; i < r; i++) {
    bo = bo * (bo - i);
  }
  return si / bo;
}

int main(int argc, const char *argv[]) {
  // std::ios::sync_with_stdio(false);
  // scanf("%s",S);
  // scanf("%d",&N);
  // getline(cin, target);
  // cin >> x >> y;
  // ここから

  long long N, X;
  cin >> N >> X;

  long long ans = 0;

  long long shou;
  long long amari = 1;
  lldiv_t l;

  ans = N;
  long long tate = N - X;
  long long yoko = X;
  while (amari != 0) {

    l = lldiv(yoko, tate);
    shou = l.quot;
    amari = l.rem;
    if (shou == 0 && amari == 0) {
      break;
    } else if (amari == 0) {
      ans = ans + (2 * (shou - 1) * tate + tate);
    } else {
      ans = ans + (amari + 2 * (shou)*tate + amari);
    }
    yoko = amari;
    tate = tate - yoko;
  }

  cout << ans << endl;

  // ここまで
  // cout << "ans" << endl;改行含む
  // printf("%.0f\n",ans);//小数点以下表示なし
  // printf("%.7f\n",p);
  // printf("%f\n",pow(2,ans.size()));

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int gcd(int a, int b) {
  while (b) {
    int m = a % b;
    a = b;
    b = m;
  }
  return a;
}

int lcm(int a, int b) { return (a * b) / gcd(a, b); }

int isPrim(int a) {
  if (a == 1) {
    return 0;
  }
  for (int i = 2; i <= (a + 1) / 2; i++) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}

long long mod_pow(long long x, long long n, long long mod) {
  // xのn乗を計算するのにn乗を2進表記にして計算
  // x^22 = x^16 + x^4 + x^2
  long long ret = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      ret = (ret * x) % mod; // 答えに付加
    }
    x = (x * x) % mod; // 2乗
    n = n / 2;
  }
  return ret;
}

struct VV {
  int x;
  int y;
  int z;
};

class LessInt {
public:
  bool operator()(const VV &riLeft, const VV &riRight) const {
    if (riLeft.x == riRight.x) {
      return riLeft.y > riRight.y;
    }
    return riLeft.x > riRight.x;
  }
};

class indInt {
public:
  bool operator()(const VV &riLeft, const VV &riRight) const {
    return riLeft.x < riRight.x;
  }
};

struct XX {
  int a;
  int b;
};

class xxIntu {
public:
  bool operator()(const XX &riLeft, const XX &riRight) const {
    // 第1条件
    if (riLeft.a != riRight.a) {
      return riLeft.a > riRight.a; //<:昇順(小さい順)、>:降順(大きい順)
    }
    // 第2条件
    return riLeft.b > riRight.b;
  }
};
bool operator<(const XX &riLeft, const XX &riRight) {
  return riLeft.a < riRight.a;
};
bool operator>(const XX &riLeft, const XX &riRight) {
  return riLeft.a > riRight.a;
};

int f(int n, int r) {
  int si = 1;
  int bo = 1;
  for (int i = 0; i < r; i++) {
    si = si * (si - i);
  }
  for (int i = 0; i < r; i++) {
    bo = bo * (bo - i);
  }
  return si / bo;
}

int main(int argc, const char *argv[]) {
  // std::ios::sync_with_stdio(false);
  // scanf("%s",S);
  // scanf("%d",&N);
  // getline(cin, target);
  // cin >> x >> y;
  // ここから

  long long N, X;
  cin >> N >> X;

  long long ans = 0;

  long long shou;
  long long amari = 1;
  lldiv_t l;

  ans = N;
  long long tate = N - X;
  long long yoko = X;
  while (amari != 0) {
    if (tate > yoko)
      swap(tate, yoko);

    l = lldiv(yoko, tate);
    shou = l.quot;
    amari = l.rem;
    if (shou == 0 && amari == 0) {
      break;
    } else if (amari == 0) {
      ans = ans + (2 * (shou - 1) * tate + tate);
    } else {
      ans = ans + (amari + 2 * (shou)*tate + amari);
    }
    yoko = amari;
    tate = tate - yoko;
  }

  cout << ans << endl;

  // ここまで
  // cout << "ans" << endl;改行含む
  // printf("%.0f\n",ans);//小数点以下表示なし
  // printf("%.7f\n",p);
  // printf("%f\n",pow(2,ans.size()));

  return 0;
}