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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace A
{
    partial class Program
    {
        long a;
        long b;
        long c;
        long d;
        public IEnumerable<string> Solve()
        {
            a = Num;
            b = Num;
            c = Num;
            d = Num;

            var res = Slv(b) - Slv(a - 1);

            yield return res.ToString();
        }

        long Slv(long n)
        {
            var cc = n / c;
            var dd = n / d;
            var lcm = c * d / Gcm(c, d);
            var cd = n / lcm;



            return n - cc - dd + cd;
        }

        long Gcm(long x, long y)
        {
            if (x < y)
                return Gcm(y, x);
            if (x % y == 0)
                return y;

            return Gcm(y, x % y);
        }
    }

    class StartingPoint
    {
        static void Main(string[] args)
        {
            if (args?.Any() == true)
            {
                var assembly = Assembly.GetExecutingAssembly();
                var resourceName = "AtCoder.Resources.In.txt";
                var stream = assembly.GetManifestResourceStream(resourceName);
                var streamReader = new StreamReader(stream, Encoding.UTF8);
                var str = streamReader.ReadToEnd();
                var textReader = new StringReader(str);
                Console.SetIn(textReader);
            }

            try
            {
                foreach (var s in new Program().Solve())
                {
                    Console.WriteLine(s);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                if (args?.Any() != true)
                    throw e;
            }

            if (args?.Any() == true)
                Console.ReadLine();
        }
    }

    partial class Program
    {
        long[] Nums(long count) => Enumerable.Range(0, (int)count).Select(_ => Num).ToArray();
        long Num => long.Parse(Str);
        double NumD => (double)Num;

        StringBuilder sb = new StringBuilder();

        string Str => GetStr();
        string GetStr()
        {
            sb.Clear();

            int r = 0;
            bool? b = null;
            do
            {
                r = Console.Read();
                b = IsBreak(r);
            } while (b == true);

            if (b == null)
                throw new Exception("input error: unexpected end of stream");

            do
            {
                sb.Append((char)r);
                r = Console.Read();
                b = IsBreak(r);
            } while (b == false);

            return sb.ToString();
        }

        bool? IsBreak(int c)
        {
            switch (c)
            {
                case -1:
                    return null;
                case (int)' ':
                case (int)'\n':
                case (int)'\r':
                    return true;
                default:
                    return false;
            }
        }
    }
}