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):
import fractions
A, B ,C, D = list(map(int, input().split()))

lcm = C * D //fractions.gcd(C, D)
b =  B // C + B // D - B // lcm
b = B - b

A -= 1
a =  A // C + A // D - A // lcm
a = A - a

print(b - a)

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(x, y):
    return (x * y) // gcd(x, y)

cd = lcm(c, d)

div_c = b // c - (a-1)//c
div_d = b // d - (a-1)//d
div_cd = b // cd - (a-1)//cd
count = b - a + 1 - div_c - div_d + div_cd
print(count)

Solution (Python):

from fractions import gcd
a, b, c, d = map(int, input().split())
l = c * d // gcd(c, d)
a -= 1
nac = a // c
nad = a // d
nal = a // l
nbc = b // c
nbd = b // d
nbl = b // l
na = nac + nad - nal
nb = nbc + nbd - nbl
print(b - a - (nb - na))

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

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

def f(N):
    return N - (N//C + N//D - N//lcm(C, D))

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

print(f(B)-f(A-1))

Solution (Python):
from fractions import gcd 

A, B, C, D = map(int, input().split())
GCD = gcd(C,D)
LCM = (C * D) // GCD

def countnumber(x):
    number = B // x
    number -= A // x
    if A % x == 0:
        number += 1
    return number 

number = countnumber(C) + countnumber(D) - countnumber(LCM)
answer = B - A - number + 1
print(answer)

Solution (Python):
import fractions
def lcm(a, b):
    return a * b // fractions.gcd(a, b)
A,B,C,D = map(int,input().split())
E = lcm(C,D)
i = (A-1)//C
j = B//C
k = (A-1)//D
l = B//D

m = (A-1)//E
n = B//E

ans = B - A + 1 - ((j-i) + (l-k) - (n-m))

print(ans)

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

def kosuu(a, b, hoge):
    mae = a // hoge
    if a%hoge == 0:
        mae -= 1
    ato = b // hoge
    wareru = ato - mae
    return wareru

def gcd(x, y):
    while y > 0:
        x, y = y, x%y
    return x

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

c_kosuu = kosuu(A,B,C)
d_kosuu = kosuu(A,B,D)
cd_kosuu = kosuu(A,B,lcm(C,D))



print(B-A+1-(c_kosuu+d_kosuu-cd_kosuu))

Solution (Python):
import fractions
import math
a,b,c,d=map(int,input().split())
k=c*d//fractions.gcd(c,d)
ans=b-a+1
ca=(a-1)//c+1
cb=b//c
da=(a-1)//d+1
db=b//d
ka=(a-1)//k+1
kb=b//k
ans-=cb-ca+1
ans-=db-da+1
ans+=kb-ka+1
print(ans)

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


def overMinMulti(a, b):
  if a % b == 0:
    return a
  return (a // b + 1) * b


def underMaxMulti(a, b):
  return a // b * b

A, B, C, D = map(int, input().split())
E = C * D // gcd(C, D)
print(
  B - A + 1
  - ((underMaxMulti(B, C) - overMinMulti(A, C)) // C + 1)
  - ((underMaxMulti(B, D) - overMinMulti(A, D)) // D + 1)
  + ((underMaxMulti(B, E) - overMinMulti(A, E)) // E + 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())

dd=(b//d)-((a-1)//d)
cc=(b//c)-((a-1)//c)
cd=(b//lcm(c,d))-((a-1)//lcm(c,d))


print(b-a+1-cc-dd+cd)

Solution (Python):
from fractions import gcd

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

A, B, C, D = [int(i) for i in input().split()]

c = (B // C) - ((A - 1) // C)
d = (B // D) - ((A - 1) // D)
cd = (B // (lcm(C, D))) - (A - 1) // (lcm(C, D))

N = B - A + 1

print(N - c - d + cd)

Solution (Python):

def gcd(x,y): 
    if x==0:
        return y
    else:
        return gcd(y%x,x)



C,D,A,B =map(int,input().split())
la =[D//A,(C-1)//A]
lb =[D//B,(C-1)//B]
lcm = A*B //gcd(A,B)
lab =[D//lcm,(C-1)//lcm]

ans = la[0] +lb[0] -la[1] -lb[1] -(lab[0]-lab[1])
print(D-C+1-ans)

Solution (Python):
def gcd(x,y):
    while y:
        x,y=y,x%y
    return x

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

def kosu(b, c, d):
    return b - ((b // c) + (b // d) - (b // lcm(c, d)))

a, b, c, d = map(int, input().split())
print(kosu(b, c, d) - kosu(a - 1, c, d))

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)
e=lcm(c,d)
cc=b//c-(a-1)//c
dd=b//d-(a-1)//d
ee=b//e-(a-1)//e
print(b-a+1-cc-dd+ee)

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

def euclid(p, q):
    if p > q:
        p, q = q, p
    while q > 0:
        p, q = q, p % q
    return p

def lcm(p, q):
    return p * q // euclid(p, q)

c_a = (a-1) // c
c_b = b // c
d_a = (a-1) // d
d_b = b // d
cd_a = (a-1) // lcm(c, d)
cd_b = b // lcm(c, d)


print(b - a + 1 - ((c_b + d_b - cd_b) - (c_a + d_a - cd_a)))

Solution (Python):
import fractions

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

def anti_division(n,a,b):
    anti_a = n // a
    anti_b = n // b
    anti_ab = n // lcm(a,b)
    return n - anti_a - anti_b + anti_ab


a,b,c,d = map(int, input().split())
ans = anti_division(b,c,d) - anti_division(a-1,c,d)
print(ans)

Solution (Python):
class LCM():
    
    def __init__(self,a,b):
        if a < b:a,b = b,a
        self.a = a
        self.b = b

    
    def gcd(self):
        a,b = self.a,self.b
        def nono(a,b):
            if a < b:a,b = b,a
            if a % b == 0:return b
            else:return nono(b,a % b)
        return nono(a,b)

    
    def lcm(self):
        a,b = self.a,self.b
        def nono(a,b):
            if a < b:a,b = b,a
            if a % b == 0:return b
            else:return nono(b,a % b)
        num = nono(a,b)
        return a*b//num
a,b,c,d = map(int,input().split())
a -= 1

numa = 0
numa += a // c
numa += a // d
cc = LCM(c,d).lcm()
numa -= a // cc

numb = 0
numb += b // c
numb += b // d
numb -= b // cc
print((b-a) - (numb - numa))

Solution (Python):
from fractions import gcd

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

a = A // C + A // D - A // ((C * D) // gcd(C, D))
b = B // C + B // D - B // ((C * D) // gcd(C, D))

print(B - A - (b - a))

Solution (Python):
import fractions

a, b, c, d = map(int, input().split())
num = b - a + 1
div_c = b // c - (a - 1) // c 
div_d = b // d - (a - 1) // d 
lcm_cd = c * d // fractions.gcd(c, d)
div_lcm = b // lcm_cd - (a - 1) // lcm_cd
print(num - div_c -div_d + div_lcm)

Solution (Python):
import math
import os
import sys
from decimal import *
from fractions import gcd

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

A, B, C, D = list(map(int, sys.stdin.readline().split()))
A = Decimal(A)
B = Decimal(B)
C = Decimal(C)
D = Decimal(D)


def count(n):
    st = math.ceil(A / n)
    en = math.floor(B / n)
    return max(0, en - st + 1)


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

Solution (Python):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
A,B,C,D= map(int, input().split())
AC = (A-1)//C
AD = (A-1)//D
ACD =(A-1)//(C*D//gcd(C,D))
ans1 = A -1-AC -AD + ACD
BC = B//C
BD = B//D
BCD = B//(C*D//gcd(C,D))
ans2 = B - BC - BD +BCD
print(ans2-ans1)

Solution (Python):

import sys
def input(): return sys.stdin.readline().rstrip()
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
import heapq
INF = float("inf")
sys.setrecursionlimit(10**7)

dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def mi(): return map(int, input().split())
def ii(): return int(input())


import fractions
A, B, C, D = mi()

cdall = B//round((C * D / fractions.gcd(C, D)))
call = B//C
dall = B//D

cdminus = (A-1)//round((C * D / fractions.gcd(C, D)))
cminus = (A-1)//(C)
dminus = (A-1)//(D)

divcntall = call+dall-cdall
divcntminus = cminus+dminus-cdminus

print(B-(A-1)-(divcntall-divcntminus))

Solution (Python):










import sys
import os
from io import IOBase, BytesIO


def main():
    a, b, c, d = get_ints()
    l = 0
    if a % c == 0:
        l = a
    else:
        l = (a // c) * c + c
    r = (b // c) * c
    n1 = (r - l) // c + 1

    if a % d == 0:
        l = a
    else:
        l = (a // d) * d + d
    r = (b // d) * d
    n2 = (r - l) // d + 1

    from fractions import gcd
    g = (c * d) // gcd(c, d)
    if a % g == 0:
        l = a
    else:
        l = (a // g) * g + g
    r = (b // g) * g
    n3 = (r - l) // g + 1

    print(b - a + 1 - (n1 + n2 - n3))


BUFSIZE = 8192


class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill():
            pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill()
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

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


class IOWrapper(IOBase):
    def __init__(self, file):
        py2 = round(0.5)
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2 == 1:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            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")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)


def get_array(): return list(map(int, sys.stdin.readline().split()))


def get_ints(): return map(int, sys.stdin.readline().split())


def input(): return sys.stdin.readline().strip()


if __name__ == "__main__":
    main()

Solution (Python):
def gcd(n1,n2):
    if n2 > n1:
        return gcd(n2,n1)
    if n2 == 0:
        return n1
    return gcd(n2,n1%n2)

def main():
    a,b,c,d = map(int,input().split())
    total = b-a+1
    n1 = (b//c)-(a//c)
    n2 = (b//d)-(a//d)
    lcm = (c*d)//gcd(c,d)
    n3 = (b//lcm)-(a//lcm)
    if a%c == 0:
        n1 += 1
    if a%d == 0:
        n2 += 1
    if a%lcm == 0:
        n3 += 1

    
    print(total-n1-n2+n3)

main()

Solution (Python):
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 solve(warareru, c, d):
    
    num1 = (warareru) // c
    
    num2 = (warareru) // d
    
    waru = lcm(c, d)
    num3 = (warareru) // waru
    return warareru - num1 - num2 + num3
    
a, b, c, d = map(int, input().split())

ans1 = solve(a-1, c, d)
ans2 = solve(b, c, d)
print(ans2-ans1)

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

cmul = (B // C) - ((A - 1) // C)
dmul = (B // D) - ((A - 1) // D)
CD = C * D // gcd(C, D)
cdmul = (B // CD) - ((A - 1) // CD)

print((B - A + 1) - (cmul + dmul - cdmul))

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

A, B, C, D = list(map(int, input().split()))
_all = B - A + 1
lcm = int(C * D / gcd(C, D))
x = (A-1) - ((A-1) // C) - ((A-1) // D) + ((A-1) // lcm)
y = B - (B // C) - (B // D) + (B // lcm)
print(y - x)

Solution (Python):
from fractions import gcd

A, B, C, D = map(int, input().split())
G = gcd(C, D)
L = C * D // G
ca = (A - 1) // C
cb = B // C
da = (A - 1) // D
db = B // D
la = (A - 1) // L
lb = B // L
div = cb + db - (ca + da) - (lb - la)
print(B - (A - 1) - div)

Solution (Python):


import fractions

def solve(a,b,c,d):
	a = a - 1
	l = c * d // fractions.gcd(c, d)
	r1 = a - (a // c + a // d - a // l)
	r2 = b - (b // c + b // d - b // l)
	return (r2 - r1)


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

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 func(n,c,d):
    a1 = n//c
    a2 = n//d
    a3 = n//lcm(c,d)
    return n - (a1+a2-a3)

a,b,c,d = map(int,input().split(' '))
ans = func(b,c,d)-func(a-1,c,d)
print(ans)

Solution (Python):
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a
A, B, C, D = map(int, input().split())
E = C * D // gcd(C, D)
x = A - 1 - (A - 1) // C - (A - 1) // D + (A - 1) // E
y = B - B // C - B // D + B // E
print(y - x)

Solution (Python):

def divides_u(x,y):
    return x // y
def divides_d(x,y):
    return (x+y-1) // y
def gcd(a,b):
    if a < b:
        a, b = b, a
    while b >= 1:
        a, b = b, a % b
    return a
def lcd(a,b):
	return a * b // gcd(a,b)


a, b, c, d =  [ int(v) for v in input().split() ]
e = lcd(c,d)
n = b - a + 1

m1 = divides_u(b,c) - divides_d(a,c) + 1
m2 = divides_u(b,d) - divides_d(a,d) + 1
m3 = divides_u(b,e) - divides_d(a,e) + 1


print(n-m1-m2+m3)

Solution (Python):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def lcm(a,b):
  return a*b //gcd(a,b)
A,B,C,D = map(int,input().split())
c = B//C-(A-1)//C
d = B//D-(A-1)//D
LCM = lcm(C,D)
l = B//LCM-(A-1)//LCM
if C==D==1:
    print(0)
else:
    print(B-A+1-c-d+l)

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right





def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")




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


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


A, B, C, D = l_inpl()
A -= 1
B_ans = B - (B // C + B // D - B // lcm(C, D))

A_ans = A - (A // C + A // D - A // lcm(C, D))

print(B_ans - A_ans)

Solution (Python):
A,B,C,D = [int(i) for i in input().split()]

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

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

E = lcm(C,D)

p = B-A+1

ac = p//C
if A%C+p%C >= C+1:
    ac += 1

ad = p//D
if A%D+p%D >= D+1:
    ad += 1

ae = p//E
if A%E+p%E >= E+1:
    ae += 1

print(p-(ac+ad-ae))

Solution (Python):
def get_a(a, b, c):
	q1, mod1 = divmod(a, c)
	q2, mod2 = divmod(b, c)
	if mod1 == 0:
		count = q2 - q1 + 1
	else:
		count = q2 - q1
	return count

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

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

def main():
	import sys
	input = sys.stdin.readline
	A, B, C, D = map(int, input().split())
	LCM = lcm(C, D)
	notans = get_a(A, B, C) + get_a(A, B, D) - get_a(A, B, LCM)
	sum = B - A + 1
	print(sum - notans)

if __name__ == '__main__':
    main()

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

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

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

zero_b = b - b // c - b // d + b // lcm(c,d)
zero_a = (a-1) - (a-1) // c - (a-1) // d + (a-1) // lcm(c,d)

print(zero_b - zero_a)

Solution (Python):
import fractions

A, B, C, D = map(int, input().split())
lcm = C * D // fractions.gcd(C, D)

def f(n):
    return n - (n // C + n // D - n // lcm)

print(f(B) - f(A - 1))

Solution (Python):
a,b,c,d=map(int,input().split())
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
p=c*d//gcd(c,d)

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


print(y-x)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    s = S()
    for i in range(3):
        if s[i] == s[i + 1]:
            print("Bad")
            return
    print("Good")
    return


def B():
    n, l = LI()
    lis = [l + i for i in range(n)]
    s = sum(lis)
    a = inf
    for i in lis:
        if abs(a) > abs(i):
            a = i
    print(s-a)
    return


def C():
    def gcd(a, b):
        a, b = max(a, b), min(a, b)
        while b:
            a, b = b, a % b
        return a
    def lcm(a, b):
        return a * b // gcd(a, b)
    a, b, c, d = LI()
    x = b // c - (a-1) // c
    y = b // d - (a-1) // d
    z = b // lcm(c , d) - (a-1) // lcm(c , d)
    print(b-a+1-(x+y-z))
    return


def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == '__main__':
    C()

Solution (Python):

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

def gcd(x,y):
    while y>0:
        x,y = y,x%y
    return x

x = c*d//gcd(c,d)
a -= 1
ans = b//c - a//c
ans += b//d - a//d
ans -= b//x - a//x

ans = b-a-ans
print(ans)

Solution (Python):

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

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

def cn(n,x):
    return n-(n//x)

def cnt(a,b,x):
    return cn(b,x) - cn(a-1,x)

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






print(cnt(A,B,C)+cnt(A,B,D)-cnt(A,B,E))

Solution (Python):
from fractions import gcd

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

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

main()

Solution (Python):
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
A,B,C,D=map(int,input().split())
A=A-1

AnC=A//C
AnD=A//D
AnCandD=A//lcm(C,D)
Aans=A-(AnC+AnD-AnCandD)

BnC=B//C
BnD=B//D
BnCandD=B//lcm(C,D)
Bans=B-(BnC+BnD-BnCandD)

print(Bans-Aans)

Solution (Python):
import sys
import fractions

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def main():
    A, B, C, D = MAP()
    E = lcm(C, D)
    nc = (B // C) - ((A - 1) // C)
    nd = (B // D) - ((A - 1) // D)
    ne = (B // E) - ((A - 1) // E)
    print(B - A + 1 - (nc + nd - ne))
if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd

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

al = B-A+1

dic = B//C - (A-1)//C
did = B//D - (A-1)//D

g = gcd(C,D)
lcs = g * (C//g) * (D//g)

dicd = B//(lcs) - (A-1)//(lcs)

ans = al - (dic + did - dicd)

print(ans)

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

def count(N,a,b):
    a_c = N//a+1
    b_c = N//b+1
    lcm = a*b//gcd(a,b)
    ab_c = N//lcm+1
    return a_c+b_c-ab_c

A,B,C,D = map(int,input().split())
print(B+1-count(B,C,D)-(A-count(A-1,C,D)))

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

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




c_w = B//C

if A%C==0:
    c_w -= A//C - 1
if A%C!=0:
    c_w -= A//C 
d_w = B//D

if A%D==0:
    d_w -= A//D - 1
if A%D!=0:
    d_w -= A//D

E = lcm(C,D)

cd_w = B//E

if A%E==0:
    cd_w -= A//E - 1
if A%D!=0:
    cd_w -= A//E


print(B-A+1-c_w-d_w+cd_w)

Solution (Python):
a, b, c, d = [int(i) for i in 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)

def count(a, b, c):
    d = a // b
    e = a // c
    f = a // lcm(b, c)
    return a - d - e + f
ans = count(b, c, d) - count(a, c, d)
if(a % c != 0 and a % d != 0):
    ans += 1
print(ans)

Solution (Python):
from fractions import gcd
a,b,c,d = map(int,input().split())
def solve(a,b,n):
    if a == b:
        if a%n==0:
            return 1
        else:
            return 0
    elif a%n == 0:
        return b//n-a//n+1
    elif b%n==0:
        return b//n-a//n
    else:
        return b//n-a//n

print(b-a+1-(solve(a,b,c)+solve(a,b,d)-solve(a,b,c*d//gcd(c,d))))

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, input().split())
cd = c * d // gcd(c, d)
xa =(a - 1) - ((a - 1) // c + (a - 1) // d - (a - 1) // cd)
xb = b - (b // c + b // d - b // cd)
print(xb - xa)

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

a,b,x,y = list(map(int, input().split()))

z = x * y // gcd(x,y)

def num(u,v,k):
    ww=v//k - (u-1)//k
    return ww

print((b-a+1)-(num(a,b,x)+num(a,b,y)-num(a,b,z)))

Solution (Python):

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



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


A, B, C, D = map(int, input().split())
ans = B - A + 1



def divnum(X, d):
    return X // d


divC = divnum(B, C) - divnum(A - 1, C)
divD = divnum(B, D) - divnum(A - 1, D)
divCD = divnum(B, lcm(C, D)) - divnum(A - 1, lcm(C, D))

print(ans - divC - divD + divCD)

Solution (Python):
import fractions

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

ac_div, ac_mod = divmod(A-1, C)
ad_div, ad_mod = divmod(A-1, D)
ae_div, ae_mod = divmod(A-1, int(C*D / fractions.gcd(C, D)))
bc_div, bc_mod = divmod(B, C)
bd_div, bd_mod = divmod(B, D)
be_div, be_mod = divmod(B, int(C*D / fractions.gcd(C, D)))

ret = B - A + 1 - (bc_div - ac_div) - (bd_div - ad_div) + (be_div - ae_div)

print(ret)

Solution (Python):
A,B,C,D=map(int,input().split())
A-=1
def gcd(x,y):
  while y:
    x,y=y,x%y
  return x
lCD=C*D//gcd(C,D)
print(B-B//C-B//D+B//lCD-(A-A//C-A//D+A//lCD))

Solution (Python):

 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 
import time
import sys
from pprint import pprint
import itertools
import fractions

A, B, C, D = [ int(x) for x in input().split() ]

lcm = int(C * D // fractions.gcd(C, D))

def not_devide_num(n):
    return n - (n // C + n // D - n // lcm)

print (not_devide_num(B) - not_devide_num(A - 1))

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

def f(a, b):
    if a == 0:
        return b
    if b == 0:
        return a        
    if a > b:
        return f(a%b, b)
    else:
        return f(a, b%a)

def g(a, b):
    return a * b // f(a,b)

def h(x, a, b):
    return x - (x // a) - (x // b) + (x // g(a,b))

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

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

def gcd(m, n):
    while m % n:
        t = n
        n = m % n
        m = t
    return n

def lcm(m, n):
    return m * n / gcd(m, n)

def foo(N, C, D):
    return N - (N / C) - (N / D) + (N / lcm(C, D))

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

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

def gcd(x,y):
    if x<y:
        x,y=y,x
    
    if y==0:
        return x
    if x%y==0:
        return y
    else:
        return gcd(y,x%y)

def lcm(x,y):
    return x*y//gcd(x,y)
ab = b-a+1
a= a-1
cd = lcm(c,d)
a_count = (a//c)+(a//d)-(a//cd)
b_count = (b//c)+(b//d)-(b//cd)

print(ab-(b_count-a_count))

Solution (Python):
A, B, C, D = map(int, input().split())
def gcd(a, b):
    return gcd(b, a%b) if b!=0 else a
lcd = C*D//gcd(C,D)
A = A-1
print( (B - B//C - B//D + B//lcd) - (A - A//C - A//D + A//lcd) )

Solution (Python):
def gcd(x,y):
    if y==0:
        return x
    else:
        return gcd(y,x%y)

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

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

n = B - A + 1
c = B//C - (A-1)//C
d = B//D - (A-1)//D
cd = B//lcm(C,D) - (A-1)//lcm(C,D)

print(n - (c + d - cd))

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa == x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))

import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


a,b,c,d = m()

kk = -(-a//c)

kkk = b//c

ll = -(-a//d)

lll = b//d

p = lcm(c,d)

pp = -(-a//p)

ppp = b//p
print(b-a+1-(kkk-kk+1)-(lll-ll+1)+(ppp-pp+1))

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

div_c = b // c - (a - 1) // c
div_d = b // d - (a - 1) // d


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


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


lcm_cd = lcm(c, d)

div_cd = b // lcm_cd - (a - 1) // lcm_cd

print(b - a + 1 - div_c - div_d + div_cd)

Solution (Python):
from math import sqrt, factorial
from fractions import gcd
from sys import setrecursionlimit
from itertools import product
from string import ascii_lowercase as abc
setrecursionlimit(10000000)

def inpl(): return list(map(int, input().split()))
A, B, C, D = inpl()
E = C*D//gcd(C, D)
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
Z = B//E - (A-1)//E
print(B-A+1-X-Y+Z)

Solution (Python):
import fractions

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

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

E = lcm(C,D)

print(B - B // C - B // D + B // E - (A - A // C - A // D + A // E))

Solution (Python):
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a
def lcm(a, b):
	return a * b // gcd (a, b)

A, B, C, D = map(int, input().split())
c_div = max(B // C - ((A+C-1) // C) + 1, 0)
d_div = max(B // D - ((A+D-1) // D) + 1, 0)
cd_div = max(B // lcm(C, D) - ((A+lcm(C, D)-1) // lcm(C, D)) + 1, 0)
ans = B - A + 1 - c_div - d_div + cd_div
print(ans)

Solution (Python):

import sys
import fractions

def solve(A: int, B: int, C: int, D: int):
    E = C * D // fractions.gcd(C, D)
    count_c = B // C - (A-1) // C
    count_d = B // D - (A-1) // D
    count_e = B // E - (A-1) // E
    print(B-A+1 - count_c - count_d + count_e)
    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):
A, B, C, D = map(int, input().split())

def gcd(m, n) :
    r = m % n
    while r != 0 :
        m, n = n, r
        r = m % n
    return n

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

C1 = (A - 1) // C
C2 = B // C
D1 = (A - 1) // D
D2 = B // D
CD1 = (A - 1) // CD
CD2 = B // CD

print(B - A + 1 - (C2 - C1 + D2 - D1 - (CD2 - CD1)))

Solution (Python):
def divide(num):
    return b//num - (a-1)//num
import fractions
a,b,c,d=map(int,input().split())
lcm=c*d//fractions.gcd(c,d)
print(b - (a - 1) - (divide(c) + divide(d) - divide(lcm)))

Solution (Python):
A, B, C, D = [int(elem) for elem in input().split()]


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


num_mul_C = B // C - A // C + (A % C == 0)
num_mul_D = B // D - A // D + (A % D == 0)

lcm = C * D // gcd(C, D)
num_mul_lcm = B // lcm - A // lcm + (A % lcm == 0)
print(B - A + 1 - (num_mul_C + num_mul_D - num_mul_lcm))

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

import fractions
CD = C * D // fractions.gcd(C, D)

A_C = (A-1) // C
B_C = B // C
A_D = (A-1) // D
B_D = B // D
A_CD = (A-1) // CD
B_CD = B //CD

ans =(B-A+1) - ((B_C - A_C) + (B_D - A_D) - (B_CD - A_CD))
print(ans)

Solution (Python):
from collections import deque
from itertools import accumulate
import sys
sys.setrecursionlimit(10**6)
mo=10**9+7

def gcd(x,y):
    if x%y==0:
        return y
    while x%y!=0:
        x,y=y,x%y
    return y
        

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


def f(n,k):
    return n//k

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

ans=f2(b)-f2(a-1)
print(ans)

Solution (Python):
from sys import stdin
import fractions

A, B, C, D = [int(x) for x in stdin.readline().rstrip().split()]


def div(n, x, y):
    lcs = int(x * y / fractions.gcd(x, y))
    return n - int(n // x) - int(n // y) + int(n // lcs)


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

Solution (Python):
import fractions
gcd = fractions.gcd

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

CD =  C* D // gcd(C, D)
def calc(X):
    num_c = X // C
    num_d = X // D
    num_cd = X // CD
    ret = num_c + num_d - num_cd
    return ret

o1 = calc(B)
o2 = calc(A-1)

o = o1 - o2
out = B - A + 1 - o
print(out)

Solution (Python):
from math import factorial
def gcd(a, b):
     while b:
     	a, b = b, a % b
     return a
def lcm(a, b):
     return a * b // gcd (a, b)

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

upper=b//c+b//d-b//lcm(c,d)
lower=(a-1)//c+(a-1)//d-(a-1)//lcm(c,d)



print(b-a+1-(upper-lower))

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

diff = B - A + 1

num_c = diff // C
if A % C + diff % C > C:
    num_c += 1
num_d = diff // D
if A % D + diff % D > D:
    num_d += 1
cd = C * D // gcd(C, D)
num_cd = diff // cd
if A % cd + diff % cd > cd:
    num_cd += 1

ans = diff - (num_c + num_d - num_cd)
print(ans)

Solution (Python):
from fractions import gcd
A,B,C,D=map(int,input().split())
A-=1
g=gcd(C,D)
d=(C//g)*(D//g)*(g)
ansA=A-(A//C+A//D-A//d)
ansB=B-(B//C+B//D-B//d)
print(ansB-ansA)