Problem:
Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;

#define rep(i, s, n) for(int i = s;i<n;i++)
#define repe(i, s, n) for(int i = s;i<=n;i++)
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<int> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
static const ll maxLL = (ll) 1 << 62;
const ll MOD = 1000000007, INF = 1e18;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};

template<typename T1, typename T2>
bool pairCompare(const pair<T1, T2> &firstElof, const pair<T1, T2> &secondElof) {
    return firstElof.second > secondElof.second;
}

int mapMaxValue(std::map<ll, int> m) {
    int max = 0;
    for (std::map<ll, int>::iterator it = m.begin(); it != m.end(); it++) {
        if (max < it->second) max = it->second;
    }
    return max;
}

ll Gcd(ll m, ll n) {
    ll temp;
    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }
    return n;
}

ll Lcm(ll m, ll n) {
    return m * n / Gcd(m, n);
}

int main(void) {
    ll a, b, c, d;

    cin >> a >> b >> c >> d;

    ll c_ue_count = b / c;
    ll d_ue_count = b / d;
    if (c_ue_count * c > b) c_ue_count--;
    if (d_ue_count * d > b) d_ue_count--;

    ll c_sita_count = a / c;
    ll d_sita_count = a / d;
    if (c_sita_count * c <= a) c_sita_count++;
    if (d_sita_count * d <= a) d_sita_count++;

    ll lcm = Lcm(c, d);
    ll lcm_ue_count = b / lcm;
    ll lcm_sita_count = a / lcm;
    if (lcm_ue_count * lcm > b) lcm_ue_count--;
    if (lcm_sita_count * lcm <= a) lcm_sita_count++;

    ll c_count = c_ue_count - c_sita_count;
    if (c_ue_count != 0 && c_sita_count != 0) c_count++;

    ll d_count = d_ue_count - d_sita_count;
    if (d_ue_count != 0 && d_sita_count != 0) d_count++;

    ll lcm_count = lcm_ue_count - lcm_sita_count;
    if (lcm_ue_count != 0 && lcm_sita_count != 0) lcm_count++;

    ll ans = b - a + 1 - (c_count + d_count - lcm_count);

    cout << ans << endl;
}