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 <bits/stdc++.h>
using namespace std;

using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

lint count_multiple2(lint l, lint r, lint x)
{
    //l以上r以下の整数に含まれるxの倍数の数を返す
    lint ans;
    ans = r / x - (l - 1) / x;
    cout << ans << "\n";
    return ans;
}

lint count_multiple(lint l, lint r, lint x)
{
    //l以上r以下の整数に含まれるxの倍数の数を返す
    lint n, ans;
    n = r - l + 1;
    ans = n / x;
    if (n % x + l % x > x)
    {
        ans += 1;
    }
    cout << ans << "\n";
    return ans;
}

lint gcd(lint m, lint n)
{
    // 引数に０がある場合は０を返す
    if ((0 == m) || (0 == n))
        return 0;

    // ユークリッドの方法
    while (m != n)
    {
        if (m > n)
            m = m - n;
        else
            n = n - m;
    }
    return m;
} //gcd

lint lcm(lint m, lint n)
{
    // 引数に０がある場合は０を返す
    if ((0 == m) || (0 == n))
        return 0;

    return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
} //lcm

int main()
{

    lint a, b, c, d;
    cin >> a >> b >> c >> d;
    lint n, ans;
    n = b - a + 1;
    lint cd;
    cd = lcm(c, d);
    lint tmp = count_multiple(a, b, c) + count_multiple(a, b, d) - count_multiple(a, b, cd);
    ans = n - tmp;
    cout << ans << "\n";

    return 0;
}