Problem p02995 (C++)

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

#define what_is(x) cerr << #x << " is " << x << endl;
#define what_is_v(x)                                                           \
  cerr << #x << " is ";                                                        \
  for (auto &e : (x))                                                          \
    cerr << e << ' ';                                                          \
  cerr << '\n'; // vector, set
#define what_is_a(x, n)                                                        \
  cerr << #x << " is ";                                                        \
  for (int i = 0; i < n; i++)                                                  \
    cerr << x[i] << ' ';                                                       \
  cerr << '\n'; // n first element of array
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define st first
#define nd second
#define cerr_pair(x) '{' << x.st << ", " << x.nd << '}'
#define pwhat_is(x) cerr << #x << " is " << cerr_pair(x) << endl;
#define pwhat_is_v(x)                                                          \
  cerr << #x << " is ";                                                        \
  for (auto &e : (x))                                                          \
    cerr << cerr_pair(e) << ' ';                                               \
  cerr << '\n'; // vector, set
#define pwhat_is_a(x, n)                                                       \
  cerr << #x << " is ";                                                        \
  for (int i = 0; i < n; i++)                                                  \
    cerr << cerr_pair(x[i]) << ' ';                                            \
  cerr << '\n'; // n first element of array
#define endl '\n'
#define int long long

typedef pair<int, int> pii;
const int N = 1e5 + 5;
const int INF = 1e9;
const int MOD = 1e9 + 7;

int a, b, c, d;
int cound_divisors(int n, int d) { return n / d; }
int range_count(int l, int r, int d) {
  return cound_divisors(r, d) - cound_divisors(l - 1, d);
}
int32_t main() {
  IOS
          // freopen("input.txt", "r", stdin);
          cin >>
      a >> b >> c >> d;

  int lcm = (c * d) / __gcd(a, b);
  int ans = (b - a + 1) - range_count(a, b, c) - range_count(a, b, d) +
            range_count(a, b, lcm);
  cout << ans << endl;
  return 0;
}


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

#define what_is(x) cerr << #x << " is " << x << endl;
#define what_is_v(x)                                                           \
  cerr << #x << " is ";                                                        \
  for (auto &e : (x))                                                          \
    cerr << e << ' ';                                                          \
  cerr << '\n'; // vector, set
#define what_is_a(x, n)                                                        \
  cerr << #x << " is ";                                                        \
  for (int i = 0; i < n; i++)                                                  \
    cerr << x[i] << ' ';                                                       \
  cerr << '\n'; // n first element of array
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define st first
#define nd second
#define cerr_pair(x) '{' << x.st << ", " << x.nd << '}'
#define pwhat_is(x) cerr << #x << " is " << cerr_pair(x) << endl;
#define pwhat_is_v(x)                                                          \
  cerr << #x << " is ";                                                        \
  for (auto &e : (x))                                                          \
    cerr << cerr_pair(e) << ' ';                                               \
  cerr << '\n'; // vector, set
#define pwhat_is_a(x, n)                                                       \
  cerr << #x << " is ";                                                        \
  for (int i = 0; i < n; i++)                                                  \
    cerr << cerr_pair(x[i]) << ' ';                                            \
  cerr << '\n'; // n first element of array
#define endl '\n'
#define int long long

typedef pair<int, int> pii;
const int N = 1e5 + 5;
const int INF = 1e9;
const int MOD = 1e9 + 7;

int a, b, c, d;
int cound_divisors(int n, int d) { return n / d; }
int range_count(int l, int r, int d) {
  return cound_divisors(r, d) - cound_divisors(l - 1, d);
}
int32_t main() {
  IOS
          // freopen("input.txt", "r", stdin);
          cin >>
      a >> b >> c >> d;
  int lcm = (c * d) / __gcd(c, d);
  int ans = (b - a + 1) - range_count(a, b, c) - range_count(a, b, d) +
            range_count(a, b, lcm);
  cout << ans << endl;
  return 0;
}