Problem p02995 (C++)

Buggy submission (status: Runtime Error):
/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                       O\  =  /O
                    ____/`---'\____
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================

                  Please give me AC.
*/

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define printn(v)                                                              \
  rep(i, (v).size() - 1) cout << (v)[i] << ' ';                                \
  cout << (v)[n - 1] << '\n';
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define debug2(x, y)                                                           \
  cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z)                                                        \
  cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " \
       << (z) << '\n'
#define dbg(v)                                                                 \
  for (size_t _ = 0; _ < v.size(); ++_) {                                      \
    cerr << #v << "[" << _ << "] : " << v[_] << '\n';                          \
  }
// constant
const int INF = (1 << 30) - 1;
const int64 INF64 = (1LL << 62) - 1;

// End of template.

// x 以下の自然数でdで割り切れるものの個数
int64 f(int64 x, int64 d) { return x / d; }

int64 gcd(int64 a, int64 b) {
  if (a < b)
    return gcd(b, a);
  int64 r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

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

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

  int64 a, b, c, d;
  cin >> a >> b >> c >> d;

  a--;
  int64 y = b - (f(b, c) + f(b, d) - f(b, lcm(c, d)));
  int64 x = a - (f(a, c) + f(a, d) - f(b, lcm(a, d)));
  print(y - x);

  return 0;
}


Fixed submission:
/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                       O\  =  /O
                    ____/`---'\____
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================

                  Please give me AC.
*/

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define printn(v)                                                              \
  rep(i, (v).size() - 1) cout << (v)[i] << ' ';                                \
  cout << (v)[n - 1] << '\n';
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define debug2(x, y)                                                           \
  cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z)                                                        \
  cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " \
       << (z) << '\n'
#define dbg(v)                                                                 \
  for (size_t _ = 0; _ < v.size(); ++_) {                                      \
    cerr << #v << "[" << _ << "] : " << v[_] << '\n';                          \
  }
// constant
const int INF = (1 << 30) - 1;
const int64 INF64 = (1LL << 62) - 1;

// End of template.

// x 以下の自然数でdで割り切れるものの個数
int64 f(int64 x, int64 d) { return x / d; }

int64 gcd(int64 a, int64 b) {
  if (a < b)
    return gcd(b, a);
  int64 r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

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

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

  int64 a, b, c, d;
  cin >> a >> b >> c >> d;

  a--;
  int64 y = b - (f(b, c) + f(b, d) - f(b, lcm(c, d)));
  int64 x = a - (f(a, c) + f(a, d) - f(a, lcm(c, d)));
  print(y - x);

  return 0;
}