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 (Python):
from fractions import gcd

a, b, c, d = [int(i) for i in input().split()]

def f(a):
    return a - (a // c + a // d - a // (c * d // gcd(c, d)))

print(f(b) - f(a - 1))

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

a, b, c, d = na()

def gcd(a, b):
    while b > 0:
        c = a % b
        a = b
        b = c
    return a

def cou(b, c, d):
    g = gcd(c, d)
    return b - b//c - b//d + b//(c//g*d)

print(cou(b, c, d) - cou(a-1, c, d))

Solution (Python):
def gcd(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def lcm(a,b):
    return a*b//gcd(a,b)

def divided(a,b,c):
    return b//c - (a-1)//c

A,B,C,D = map(int,input().split())

print( (B-A+1) - divided(A,B,C) - divided(A,B,D) + divided(A,B,lcm(C,D)))

Solution (Python):

def gcd(x,y):
    if x<y:
        x,y=y,x
    if y==0:
        return x
    return gcd(y,x%y)
def lcm(x,y):
    return x*y//gcd(x,y)
def count(a,b,k):
    cnt=b//k-a//k
    if a%k==0:
        cnt+=1
    return cnt

a,b,c,d=map(int,input().split())
l=lcm(c,d)
print(b-a+1-count(a,b,c)-count(a,b,d)+count(a,b,l))

Solution (Python):

import sys
sys.setrecursionlimit(300000)


def solve(A: int, B: int, C: int, D: int):
    def gcd(a, b):
        if b == 0:
            return a
        return gcd(b, a % b)
    g = gcd(C, D)
    c = B // C - (A - 1) // C
    d = B // D - (A - 1) // D
    cd = B // (C * D // g) - (A - 1) // (C * D // g)
    
    ret = B - A - c - d + cd + 1
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    D = int(next(tokens))  
    solve(A, B, C, D)

if __name__ == '__main__':
    main()

Solution (Python):

from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def gcd(x, y):
    """greatest common divisor of x and y"""
    while y:
        x, y = y, x % y
    return x


lcm = lambda a, b: a * b // gcd(a, b)


def main():
    a, b, c, d = map(int, input().split())

    cnt = b - ((b // c) + (b // d) - (b // lcm(c, d)))
    a -= 1
    tmp = a - ((a // c) + (a // d) - (a // lcm(c, d)))
    print(cnt - tmp)




BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

Solution (Python):
a, b, c, d = map(int, input().split())

from fractions import gcd

lcm = (c * d) // gcd(c, d)
x = b - (b // c + b // d - b // lcm)

a = a - 1
y = a - (a // c + a // d - a // lcm)

print(x - y)

Solution (Python):
import fractions as frac
a,b,c,d=map(int,input().split())
e=frac.gcd(c,d)
f=(c*d)//e
x=b//c
y=b//d
z=b//f
u=(a-1)//c
v=(a-1)//d
s=(a-1)//f
num1=(b-(x+y-z))
num2=((a-1)-(u+v-s))
print(num1-num2)

Solution (Python):
A, B, C, D = map(int, input().split())



def gcd(a, b):
    while b:
        a, b = b, a % b
    return a



def lcm(a, b):
    return a * b // gcd(a, b)


LCM = lcm(C, D)


def calc(n):
    div_c = n // C
    div_d = n // D
    div_c_d = n // LCM
    return n - div_c - div_d + div_c_d


print(calc(B) - calc(A - 1))

Solution (Python):
from fractions import gcd

def count_multiples(a,b,m):
    a = ((a-1)//m + 1)
    b = b//m
    return b-a+1

a,b,c,d = map(int,input().split())
print(b-a+1 - count_multiples(a,b,c) 
    - count_multiples(a,b,d) 
    + count_multiples(a,b,(c*d)//gcd(c,d)))

Solution (Python):
from fractions import gcd
A, B, C, D = map(int, input().split())

def f(n, x, y):
    m = 0
    m += n // x
    m += n // y
    m -= n // (x // gcd(x, y) * y)
    return n - m

print(f(B, C, D) - f(A - 1, C, D))

Solution (Python):
import sys
input = sys.stdin.readline
ri = lambda: int(input())


rl = lambda: map(int,input().split())
rr = lambda N: [list(l) for l in zip(*[rl() for _ in range(N)])]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD = 10**9+7

A,B,C,D=rl()

def gcd(a, b):
    while b > 0:
        a, b = b, a%b
    return a

def lcm(a,b):
    return a*b//gcd(a,b)

CD = lcm(C,D)

ans = B - (A-1) - (B//C - (A-1)//C) - (B//D - (A-1)//D) + (B//CD - (A-1)//CD)

print(ans)

Solution (Python):

from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

import random
import collections
import math
import itertools
import bisect

def gcd(x, y):
    """greatest common divisor of x and y"""
    while y:
        x, y = y, x % y
    return x

lcm = lambda a, b: a * b // gcd(a, b)

def real_main():
    a, b, c, d = read_int_array()
    def aux(x):
        n1 = x // c
        n2 = x // d
        n3 = x // (lcm(c, d))
        return x - n1 - n2 + n3

    print(aux(b) - aux(a-1))


def solve():
    pass


def main():
    if False and 'PYCHARM_HOSTED' in os.environ:
        main_pycharm()
    else:
        real_main()


from copy import deepcopy
def main_pycharm():
    solution = solve

    test_inputs = None
    test_outputs = None
    judge = None
    slow_solution = None
    if solution is not None:
        if test_outputs is not None:
            test_with_answers(solution, test_inputs, test_outputs)
        if judge is not None:
            test_with_judge(solution, test_inputs, judge)
        if slow_solution is not None:
            test_with_slow_solution(solution, test_inputs, slow_solution)


def test_with_answers(solution, inputs, answers):
    total, wrong = 0, 0
    for args, test_ans in zip(inputs, answers):
        ans = solution(*deepcopy(args))
        if ans != test_ans:
            print('WRONG! ans=%s, test_ans=%s, args=%s' % (ans, test_ans, args))
            wrong += 1
        else:
            print('GOOD')
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_judge(solution, inputs_gen, judge):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        if not judge(deepcopy(ans), *deepcopy(args)):
            print('WRONG! ans=%s, args=%s' % (ans, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))


def test_with_slow_solution(solution, inputs_gen, solution_slow):
    total, wrong = 0, 0
    for args in inputs_gen:
        ans = solution(*deepcopy(args))
        slow = solution_slow(*deepcopy(args))
        if ans != slow:
            print('WRONG! ans=%s, slow=%s, args=%s' % (ans, slow, args))
            wrong += 1
        total += 1
    print('ALL %d TESTS PASSED' % total if not wrong else '%d out of %d tests are WRONG' % (wrong, total))




def mergesort(A, key=lambda x: x, reverse=False):
    C = A
    A = list(range(len(A)))
    B = list(A)

    n = len(A)
    for i in range(0, n - 1, 2):
        if key(C[A[i]]) > key(C[A[i ^ 1]]):
            A[i], A[i ^ 1] = A[i ^ 1], A[i]

    width = 2
    while width < n:
        for i in range(0, n, 2 * width):
            R1, R2 = min(i + width, n), min(i + 2 * width, n)
            j, k = R1, i
            while i < R1 and j < R2:
                if key(C[A[i]]) > key(C[A[j]]):
                    B[k] = A[j]
                    j += 1
                else:
                    B[k] = A[i]
                    i += 1
                k += 1
            while i < R1:
                B[k] = A[i]
                k += 1
                i += 1
            while k < R2:
                B[k] = A[k]
                k += 1
        A, B = B, A
        width *= 2

    if reverse:
        A.reverse()
    return A




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

def read():
    return input()

def read_int():
    return int(input())

def read_array(sep=None, maxsplit=-1):
    return input().split(sep, maxsplit)

def read_int_array():
    return [int(a) for a in read_array()]



if __name__ == "__main__":
    main()

Solution (Python):
A, B, C, D = map(int, input().split())

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b,a%b)

def lcm(a, b):
    return a*b//gcd(a, b)

def count(X, a, b):
    return X//a + X//b - X//lcm(a, b)

print((B - A + 1) + count(A-1, C, D) - count(B, C, D))

Solution (Python):
from fractions import gcd
A, B, C, D = map(int, input().split())
def solve(N):
    ans = N
    ans -= N//C
    ans -= N//D
    ans += N//(C*D//gcd(C, D))
    return ans
print(solve(B) - solve(A-1))

Solution (Python):
from fractions import gcd
def lcm(num1, num2):
    return (num1 * num2) // gcd(num1, num2)
A, B, C, D = map(int, input().split())
a = lcm(C, D)
print((B-A+1) - (B//C - (A-1)//C) - (B//D - (A-1)//D) + (B//a - (A-1)//a))

Solution (Python):
A,B,C,D = map(int,input().split())
def f(x,y):
  if x<y:
    x,y = y,x
  if y==0:
    return x
  return f(y,x%y)
E = C*D//f(C,D)
C = B//C-A//C+1 if A%C==0 else B//C-A//C
D = B//D-A//D+1 if A%D==0 else B//D-A//D
E = B//E-A//E+1 if A%E==0 else B//E-A//E
print(B-A-C-D+E+1)

Solution (Python):





from fractions import gcd 



a,b,c,d = [int(i) for i in input().split()]

def res(n):
    return n - n//c - n//d + n//(c*d//gcd(c,d))

print(res(b) - res(a-1))

Solution (Python):
from fractions import gcd


def lcm(x, y):
    return x * y // gcd(x, y)


a, b, c, d = map(int, input().split())

cnt_b = b // c + b // d - b // lcm(c, d)
cnt_a = (a - 1) // c + (a - 1) // d - (a - 1) // lcm(c, d)

print(b - a + 1 - (cnt_b - cnt_a))

Solution (Python):











import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left, bisect_right
from fractions import gcd
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    A, B, C, D = MI()
    lcm = C * D // gcd(C, D)
    ans = B-(B // C + B // D - B // lcm)
    ans -= (A-1)-((A-1)//C + (A-1)//D - (A-1)//lcm)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys

from fractions import gcd
from itertools import groupby as gb
from itertools import permutations as perm
from itertools import combinations as comb
from collections import Counter as C
from collections import defaultdict as dd
sys.setrecursionlimit(10**5)
def y(f):
    if f:
        print('Yes')
    else:
        print('No')
def Y(f):
    if f:
        print('YES')
    else:
        print('NO')


a,b,c,d = map(int,input().split())
g = gcd(c, d)
l = c * d // g

def f(p, q):
    return p // q

res = b - a + 1
res -= f(b, c) - f(a - 1, c)
res -= f(b, d) - f(a - 1, d)
res += f(b, l) - f(a - 1, l)

print(res)

Solution (Python):
import fractions

a,b,c,d = map(int, input().split())

e = (c*d)//fractions.gcd(c,d)

x = (a-1)//c+(a-1)//d-(a-1)//e
y = b//c+b//d-b//e


print(b-a+1-(y-x))

Solution (Python):
a,b,c,d=map(int,input().split())
if a%c==0:
  countc1=(a//c)-1
else:
  countc1=a//c
countc2=b//c
if a%d==0:
  countd1=(a//d)-1
else:
  countd1=a//d
countd2=b//d
def gcd(n,m):
  a=max(n,m)
  b=min(n,m)
  while b!=0:
    a,b=b,a%b
  return a
e=c*d//gcd(c,d)
if a%e==0:
  counte1=(a//e)-1
else:
  counte1=a//e
counte2=b//e
print(b-a+1+countc1+countd1-counte1-countc2-countd2+counte2)

Solution (Python):
def gcd(a,b):
	if(a==0):
		return b
	else:
		return gcd(b%a,a)

a,b,c,d=map(int,input().split())
ans1=b//c-(a//c)
if(a%c==0):
	ans1+=1
ans2=b//d-(a//d)
if(a%d==0):
	ans2+=1

lcm=(c*d)//(gcd(min(c,d),max(c,d)))
ans3=b//lcm-(a//lcm)
if(a%lcm==0):
	ans3+=1
print(b-a+1-(ans1+ans2-ans3))

Solution (Python):
def gcd(a,b):
    a,b = max(a,b),min(a,b)
    while a%b!=0:
        a,b = b,a%b
    return b

A,B,C,D = list(map(int,input().split()))

cs = B//C-(A-1)//C
ds = B//D-(A-1)//D

E = C*D//gcd(C,D)

es = B//E-(A-1)//E

print(B-A+1-cs-ds+es)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from fractions import gcd

def main():
    A, B, C, D = LI()
    ans = B - A + 1
    ans -= (B // C) - ((A - 1) // C)
    ans -= (B // D) - ((A - 1) // D)
    E = C * D // gcd(C, D)
    ans += (B // E) - ((A - 1) // E)
    return ans

print(main())

Solution (Python):
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    if b == 0:
        return a
    return gcd(b, a % b)

def lcm(a, b):
    return a * b // gcd(a, b)

def count(X, C, D):
    ret = X - (X//C + X//D - X//lcm(C, D))
    return ret

def main():
    A, B, C, D = map(int, input().split())
    ans = count(B, C, D) - count(A-1, C, D)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
A, B, C, D = map(int, input().split())
def gcd(a, b):
    while b: a, b = b, a % b
    return a

def calc(n):
    return n-n//C-n//D+n//(C*D//gcd(C, D))

print(calc(B)-calc(A-1))

Solution (Python):
A,B,C,D=map(int,input().split())
def gcd(a,b):
    while a%b>0:
        a,b=b,a%b
    return b
CC=B//C-(A-1)//C
DD=B//D-(A-1)//D
CD=C*D//gcd(C,D)
EE=B//CD-(A-1)//CD
print(B-A+1-CC-DD+EE)

Solution (Python):
import sys
input = sys.stdin.readline

A,B,C,D=map(int,input().split())

import math

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
    
def lcm(x, y):
    return (x * y) // gcd(x, y)

E=lcm(C,D)

x=B-A+1
y=B//C-(A-1)//C
z=B//D-(A-1)//D
w=B//E-(A-1)//E

print(x-y-z+w)

Solution (Python):
A,B,C,D = map(int,input().split())

def gcd(a,b):
  while b:
    a,b = b,a%b
  return a

def F(N,C,D):
  x = N
  x -= N//C
  x -= N//D
  lcm = C*D//gcd(C,D)
  x += N//lcm
  return x

answer = F(B,C,D) - F(A-1,C,D)
print(answer)

Solution (Python):
import fractions
A, B, C, D = map(int, input().split())
total = B - A + 1
Cdiv = B // C - (A-1) // C
Ddiv = B // D - (A-1) // D
cddiv = (C*D)//fractions.gcd(C,D)
CDdiv = B // (cddiv) - (A-1) // (cddiv)

print(total - Cdiv - Ddiv + CDdiv)