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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println(calc(s.nextBigInteger(), s.nextBigInteger(),
				s.nextBigInteger(), s.nextBigInteger()));

	}

	public static BigInteger calc(BigInteger a, BigInteger b, BigInteger c,
			BigInteger d) {
		BigInteger zenKosu = b.subtract(a).add(BigInteger.ONE);
		return zenKosu.subtract(calcBiasuKosu(a, b, c, d));
	}

	public static BigInteger calcBiasuKosu(BigInteger a, BigInteger b, BigInteger c,
			BigInteger d) {
		BigInteger cKosu = calcBiasuKosu(a, b, c);
		BigInteger dKosu = calcBiasuKosu(a, b, d);
		BigInteger cdKosu = calcBiasuKosu(a, b, calcKoBaisu(c, d));
		return cKosu.add(dKosu).subtract(cdKosu);
	}

	public static BigInteger calcKoBaisu(BigInteger x, BigInteger y) {
		BigInteger a = x;
		BigInteger b = y;
		BigInteger mult = x.multiply(y);
		if (a.compareTo(b) < 0) {
			BigInteger tmp = a;
			a = b;
			b = tmp;
		}
		BigInteger r = a.mod(b);
		while (!r.equals(BigInteger.ZERO)) {
			a = b;
			b = r;
			r = a.mod(b);
		}
		return mult.divide(b);
	}

	public static BigInteger calcBiasuKosuFrom1(BigInteger n, BigInteger to) {
		return n.divide(to);
	}

	public static BigInteger calcBiasuKosu(BigInteger a, BigInteger b, BigInteger n) {
		BigInteger res = calcBiasuKosuFrom1(b, n).subtract(calcBiasuKosuFrom1(a, n));
		if (a.mod(n).equals(BigInteger.ZERO)) {
			res = res.add(BigInteger.ONE);
		}
		return res;
	}
}