Problem p02995 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define null nullptr
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mp make_pair
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

using namespace std;

typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
typedef unordered_map<int, int> mp;

const int mod = 1e9 + 7;
const int least = -1e9;
const int most = 1e9;

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

int32_t main() {
  FAST;

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int a, b;
  cin >> a >> b;

  int c, d;
  cin >> c >> d;

  int L = a - 1, R = b;

  int div1 = (R / c + R / d - R / lcm(c, d));
  int div2 = (L / c + L / d - L / lcm(c, d));

  // cout << div1 << " " << div2 << endl;

  int first = R - div1;
  int secnd = L - div2;

  // cout << first << " " << secnd << endl;

  cout << (first - secnd) << endl;

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define null nullptr
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define mp make_pair
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

using namespace std;

typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
typedef unordered_map<int, int> mp;

const int mod = 1e9 + 7;
const int least = -1e9;
const int most = 1e9;

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

int32_t main() {
  FAST;

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif

  int a, b;
  cin >> a >> b;

  int c, d;
  cin >> c >> d;

  int L = a - 1, R = b;

  int div1 = (R / c + R / d - R / lcm(c, d));
  int div2 = (L / c + L / d - L / lcm(c, d));

  // cout << div1 << " " << div2 << endl;

  int first = R - div1;
  int secnd = L - div2;

  // cout << first << " " << secnd << endl;

  cout << (first - secnd) << endl;

  return 0;
}