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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;

namespace AtCoder20190622
{
	class ProgramC
	{
		static void Main(string[] args)
		{
			var inputs = Console.ReadLine().Split(' ');
			var A = decimal.Parse(inputs[0]);
			var B = decimal.Parse(inputs[1]);
			var C = int.Parse(inputs[2]);
			var D = int.Parse(inputs[3]);
			CalcC(A, B, C, D);
		}
		static void CalcC(decimal A, decimal B, int C, int D)
		{
			// 最小公倍数
			decimal lcm = Lcm(C, D);

			// C で割り切れる個数
			decimal restC = A % C;
			decimal startC = A + restC;
			decimal countC = Math.Floor((B - startC) / C) + 1;
			if (startC < A || B < startC)
			{
				countC = 0;
			}

			// D で割り切れる個数
			decimal restD = A % D;
			decimal startD = A + restD;
			decimal countD = Math.Floor((B - startD) / D) + 1;
			if (startD < A || B < startD)
			{
				countD = 0;
			}

			// 最小公倍数で割り切れる個数
			decimal restCD = A % lcm;
			decimal startCD = A + restCD;
			decimal countCD = Math.Floor((B - startCD) / lcm) + 1;
			if (startCD < A || B < startCD)
			{
				countCD = 0;
			}

			// C でも D でも割り切れない個数
			decimal result = B - A + 1 - (countC + countD - countCD);
			Console.WriteLine(result);
		}

		// 最小公倍数 (Least common multiple)
		// https://stackoverflow.com/questions/13569810/least-common-multiple
		public static decimal LCM(decimal a, decimal b)
		{
			decimal num1, num2;
			if (a > b)
			{
				num1 = a; num2 = b;
			}
			else
			{
				num1 = b; num2 = a;
			}

			for (decimal i = 1; i < num2; i++)
			{
				if ((num1 * i) % num2 == 0)
				{
					return i * num1;
				}
			}
			return num1 * num2;
		}

		// https://qiita.com/gushwell/items/f08d0e71fa0480dbb396
		// 最小公倍数
		public static decimal Lcm(int a, int b)
		{
			return 1M * a * b / Gcd(a, b);
		}

		// ユークリッドの互除法 (最大公約数)
		public static int Gcd(int a, int b)
		{
			if (a < b)
				// 引数を入替えて自分を呼び出す
				return Gcd(b, a);
			while (b != 0)
			{
				var remainder = a % b;
				a = b;
				b = remainder;
			}
			return a;
		}
	}
}