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: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC131.C();
        }
        class ABC131
        {
            public static void A()
            {
                var S = Read.Str();
                Console.WriteLine(S[0] == S[1] || S[1] == S[2] || S[2] == S[3] ? "Bad" : "Good");
            }
            public static void B()
            {
                var N = Read.Int();
                var L = Read.Int();
                var min = L;
                var max = L + N - 1;
                var total = (min + max) * N / 2;
                if (min > 0)
                {
                    total -= min;
                }
                if (max < 0)
                {
                    total -= max;
                }
                Console.WriteLine(total);
            }
            public static void C()
            {
                var A = Read.Long();
                var B = Read.Long();
                var C = Read.Long();
                var D = Read.Long();
                var lcm = C * D / Util.GCD(C, D);
                var total = B - A + 1;
                var divC = B / C - (A - 1) / C;
                var divD = B / D - (A - 1) / D;
                var divCD = B / lcm - (A - 1) / lcm;
                Console.WriteLine(total - divC - divD + divCD);
            }
            public static void D()
            {
                var N = Read.Int();
                var AB = Read.TupleInt(N);
                AB = AB.OrderBy(x => x.Item2).ToArray();
                int current = 0;
                bool ok = true;
                for(int i = 0; i < N; ++i)
                {
                    current += AB[i].Item1;
                    if (current > AB[i].Item2)
                    {
                        ok = false;
                        break;
                    }
                }
                Console.WriteLine(ok ? "Yes" : "No");
            }
            public static void E()
            {
                var N = Read.Int();
                var K = Read.Int();
                if (K > (N - 1) * (N - 2) / 2) { Console.WriteLine(-1); return; }
                var list = new List<Tuple<int, int>>();
                for (int i = 2; i <= N; ++i)
                {
                    list.Add(new Tuple<int, int>(1, i));
                }
                int a = 2;
                int b = 3;
                int kill = (N - 1) * (N - 2) / 2 - K;
                for (int i = 0; i < kill; ++i)
                {
                    list.Add(new Tuple<int, int>(a, b));
                    b++;
                    if (b > N) { a++; b = a + 1; }
                }
                Console.WriteLine(list.Count);
                foreach(var t in list)
                {
                    Console.WriteLine("{0} {1}", t.Item1, t.Item2);
                }
            }
            public static void F()
            {
                var N = Read.Int();
                var XY = Read.TupleInt(N);
                Dictionary<int, HashSet<int>> dictX = new Dictionary<int, HashSet<int>>();
                Dictionary<int, HashSet<int>> dictY = new Dictionary<int, HashSet<int>>();
                for(int i = 0; i < N; ++i)
                {
                    var x = XY[i].Item1;
                    var y = XY[i].Item2;
                    if (!dictX.ContainsKey(x)) { dictX.Add(x, new HashSet<int>()); }
                    dictX[x].Add(i);
                    if (!dictY.ContainsKey(y)) { dictY.Add(y, new HashSet<int>()); }
                    dictY[y].Add(i);
                }

                HashSet<int> checkI = new HashSet<int>();
                HashSet<int> checkX = new HashSet<int>();
                HashSet<int> checkY = new HashSet<int>();
                long sum = 0;
                for(int i = 0; i < N; ++i)
                {
                    if (checkI.Contains(i)) { continue; }
                    HashSet<int> setI = new HashSet<int>();
                    HashSet<int> setX = new HashSet<int>();
                    HashSet<int> setY = new HashSet<int>();
                    Queue<int> qX = new Queue<int>();
                    Queue<int> qY = new Queue<int>();
                    setI.Add(i);
                    qX.Enqueue(XY[i].Item1);
                    qY.Enqueue(XY[i].Item2);
                    while (qX.Count + qY.Count > 0)
                    {
                        if (qX.Count > 0)
                        {
                            var x = qX.Dequeue();
                            foreach(var j in dictX[x])
                            {
                                if (!checkI.Contains(j))
                                {
                                    checkI.Add(j);
                                    setI.Add(j);
                                    var jX = XY[j].Item1;
                                    var jY = XY[j].Item2;
                                    if (!setX.Contains(jX)) { setX.Add(jX); qX.Enqueue(jX); }
                                    if (!setY.Contains(jY)) { setY.Add(jY); qY.Enqueue(jY); }
                                }
                            }
                        }
                        if (qY.Count > 0)
                        {
                            var y = qY.Dequeue();
                            foreach (var j in dictY[y])
                            {
                                if (!checkI.Contains(j))
                                {
                                    checkI.Add(j);
                                    setI.Add(j);
                                    var jX = XY[j].Item1;
                                    var jY = XY[j].Item2;
                                    if (!setX.Contains(jX)) { setX.Add(jX); qX.Enqueue(jX); }
                                    if (!setY.Contains(jY)) { setY.Add(jY); qY.Enqueue(jY); }
                                }
                            }
                        }
                    }
                    sum += setX.Count * setY.Count - setI.Count;
                }
                Console.WriteLine(sum);
            }
        }

        class Util
        {
            public static long GCD(long a, long b)
            {
                while (b > 0)
                {
                    var c = a % b;
                    a = b;
                    b = c;
                }
                return a;
            }
        }

        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new HashSet<int>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1]);
                    dict[ab[1]].Add(ab[0]);
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}