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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define REP(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define pb(a) push_back(a)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

typedef long long unsigned int ll;
typedef pair<ll, ll> P;

int main(){
  ll a,b,c,d;
  cin >> a >> b >> c >> d;
  ll a_c = a/c;
  ll a_d = a/d;
  ll b_c = b/c;
  ll b_d = b/d;
  if(c<d) swap(c,d);
  ll cd = c*d;
  ll r = c%d;
  while(r!=0){
    c = d;
    d = r;
    r = c%d;
  }
  cd = cd/d;

  ll a_cd = a/cd;
  ll b_cd = b/cd;
  ll count_c,count_d,count_cd;
  if(a%c==0){
    count_c = b_c-a_c+1;
  }else{
    count_c = b_c-a_c;
  }
  if(a%d==0){
    count_d = b_d-a_d+1;
  }else{
    count_d = b_d-a_d;
  }
  if(a%d==0){
    count_cd = b_cd-a_cd+1;
  }else{
    count_cd = b_cd-a_cd;
  }
  cout << b-a+1 - (count_c+count_d-count_cd) << endl;



}