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 sys

def gcd(a, b): return gcd(b, a % b) if b else abs(a)
def lcm(a, b): return abs(a // gcd(a, b) * b)

a, b, c, d = map(int, sys.stdin.readline().split())

def count(n):
    return n - (n // c + n // d - n // lcm(c, d))

def main():
    print(count(b) - count(a-1))
    
if __name__ ==  '__main__':
    main()

Solution (Python):
import fractions
a,b,c,d=map(int,input().split())
base=b-a+1
n_c=b//c-(a+c-1)//c+1
n_d=b//d-(a+d-1)//d+1
cd=c*d // fractions.gcd(c,d)
n_cd=b//cd-(a+cd-1)//cd+1
print(base-n_c-n_d+n_cd)

Solution (Python):
from sys import stdin
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
A,B,C,D=list(map(int,stdin.readline().strip().split()))
C_A=(A-1)//C
C_B=B//C
D_A=(A-1)//D
D_B=B//D
CD=lcm(C,D)
CD_A=(A-1)//(CD)
CD_B=B//(CD)

print((1+B-A)-((C_B-C_A)+((D_B-D_A)-(CD_B-CD_A))))

Solution (Python):
from fractions import gcd
A, B, C, D = map(int, input().split())
E = C*D//gcd(C, D)
X = B - B//C - B//D + B//E
Y = A-1 - (A-1)//C - (A-1)//D + (A-1)//E
print(X-Y)

Solution (Python):
from fractions import gcd
A, B, C, D = map(int, input().split())
E = C*D//gcd(C, D)
X = B - B//C - B//D + B//E
Y = A-1 - (A-1)//C - (A-1)//D + (A-1)//E
print(X-Y)

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

if a % d == 0:
    d_times = b//d - a//d +1
else:
    d_times = b//d - a//d

lcm = c*d // gcd(c,d)
if a % lcm == 0:
    lcm_times = b//lcm - a//lcm +1
else:
    lcm_times = b//lcm - a//lcm

total = (b-a+1) - (c_times + d_times - lcm_times)
print(total)

Solution (Python):










mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    
    
    a,b,c,d = readInts()
    
    
    from fractions import gcd
    lcm = ( c // gcd(c,d) ) * d
    
    


    


    B_ = b - (b // c + b // d - b // lcm)
    
    
    A_ = a-1 - ((a-1) // c  + (a-1) // d - (a-1) // lcm) 
    
    print(B_ - A_)
if __name__ == '__main__':
  main()

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


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


A = A - 1

ans = B - (B // C) - (B // D) + (B // (lcm(C, D)))
p = (A - (A // C) - (A // D) + (A // (lcm(C, D))))

print(ans - p)

Solution (Python):
import math
import fractions
a,b,c,d=map(int,input().split())
base=b-a+1
n_c=b//c-(a+c-1)//c+1
n_d=b//d-(a+d-1)//d+1
cd=c*d//fractions.gcd(c,d)
n_cd=b//cd-(a+cd-1)//cd+1
print(base-n_c-n_d+n_cd)

Solution (Python):
import fractions
A, B, C, D = map(int, input().split())
lcm = C * D // fractions.gcd(C,D)
wari_A = (A-1) - ((A-1) // C + (A-1) // D - (A-1)//lcm)
wari_B = B - (B// C + B // D - B//lcm)
print(wari_B -wari_A)

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

c_num = b//c - (a-1)//c
d_num = b//d - (a-1)//d
from fractions import gcd
lcm = c*d//gcd(c,d)
cd_num = b//lcm - (a-1)//lcm
print(total_num-c_num-d_num+cd_num)

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

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

cd = c * d // gcd(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):
import fractions
a, b, c, d = map(int,input().split())

l = int(fractions.gcd(c,d) * c / fractions.gcd(c,d) * d / fractions.gcd(c,d))

x = b // c - a // c
y = b // d - a // d
z = b // l - a // l
if a % c == 0 or a % d == 0:
    print(int(b-a-(x+y-z)))
else:
    print(int(b-a-(x+y-z))+1)

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

p = (c * d) // gcd(c, d)

a_under = a-1

ans = (b - b//c - b//d + b//p) - (a_under - a_under//c - a_under//d + a_under//p)

print(ans)

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, input().split(' '))


c_numbers = b//c - (a-1)//c

d_numbers = b//d - (a-1)//d

gcd_number = c*d // gcd(c, d)
ortak = b//gcd_number - (a-1)//gcd_number

print(b-a+1-(c_numbers+d_numbers-ortak))

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, input().split(' '))


c_numbers = b//c - (a-1)//c

d_numbers = b//d - (a-1)//d

gcd_number = c*d // gcd(c, d)
ortak = b//(gcd_number) - (a-1)//(gcd_number)

print(b-a+1-(c_numbers+d_numbers-ortak))

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, input().split(' '))











cd = gcd(c, d)
cd = c * d // cd
c_able = b // c - (a - 1) // c
d_able = b // d - (a - 1) // d
cd_able = b // (cd) - (a - 1) // (cd)

print(b - a + 1 - (c_able + d_able - cd_able))

Solution (Python):
import sys
import fractions
def input(): return sys.stdin.readline().rstrip()
def cunt_multi(a,b,n):
    
    if b%n<=(b-a+1)%n-1:
        return (b-a+1)//n+1
    else:
        return (b-a+1)//n

def lcm(a,b):
    f=a*b//fractions.gcd(a,b)
    return f

def main():
    a,b,c,d=map(int,input().split())
    print(b-a+1-cunt_multi(a,b,c)-cunt_multi(a,b,d)+cunt_multi(a,b,lcm(c,d)))

if __name__=='__main__':
    main()

Solution (Python):
import fractions
 
def gcd(a,b):
    return fractions.gcd(a,b)
a,b,c,d = map(int, input().split())

cd = gcd(c,d)
cd = c*d //cd
c_able = b//c - (a-1)//c
d_able = b//d - (a-1)//d
cd_able = b//(cd) - (a-1)//(cd)

print(b- a + 1 - (c_able + d_able - cd_able))

Solution (Python):
import fractions
a, b, c, d=map(int, input().split())
k=fractions.gcd(c, d)
lcm=c*d//k
x=b//c - (a-1)//c
y=b//d - (a-1)//d
z=b//lcm - (a-1)//lcm
print(b-a+1-(x+y-z))

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

ans = b - a + 1
cd = c*d//fractions.gcd(c,d)
tmp = int(b // c) - int((a-1) // c)
tmp2 = int(b // d) - int((a-1) // d)
tmp3 = int(b //cd) - int((a-1) // cd)


print(ans - tmp - tmp2 + tmp3 )

Solution (Python):
import fractions

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

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

multiple_C_under_A = (A - 1) // C
multiple_D_under_A = (A - 1) // D
multiple_CD_under_A = (A - 1) // lcm

multiple_C_under_B = B // C
multiple_D_under_B = B // D
multiple_CD_under_B = B // lcm

total = B - A + 1

print(total - (multiple_C_under_B - multiple_C_under_A + multiple_D_under_B - multiple_D_under_A - (
            multiple_CD_under_B - multiple_CD_under_A)))

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

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):
from fractions import gcd
a, b, c, d = map(int, input().split())
a = a-1
C, D = b // c - a // c, b // d - a // d
l = c*d // gcd(c, d)
CD = b // l - a // l
print(b - a - C - D + CD)

Solution (Python):
from fractions import gcd

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

num1 = B // C - (A-1) // C
num2 = B // D - (A-1) // D
gcdCD = C * D // gcd(C, D)
num3 = B // gcdCD - (A-1) // gcdCD

print(B - A - num1 - num2 + num3 + 1)

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

Solution (Python):
from fractions import gcd


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


A, B, C, D = map(int, input().split())
sc = B//C - (A-1)//C
sd = B//D - (A-1)//D
scd = B//lcm(C, D) - (A-1)//lcm(C, D)


print(B-A+1 - sc - sd + scd)

Solution (Python):
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)

A,B,C,D = map(int,input().split())
all_count = B-A+1
c_count = B//C - (A-1)//C
d_count = B//D - (A-1)//D
cd_count = B//lcm(C,D) - (A-1)//lcm(C,D)
ans = all_count - c_count - d_count + cd_count
print(ans)

Solution (Python):
def gcd(a, b):
    
    if a < b:
        a, b = b, a

    if a % b == 0:
        return b
    else:
        return gcd(b, a % b)
a,b,c,d = map(int,input().split())
b_c = b//c
b_d = b//d
a_c = (a-1)//c
a_d = (a-1)//d
b_cd = b//int((c*d)/gcd(c, d))
a_cd = (a-1)//int((c*d)/gcd(c, d))
print((b-a+1)-(b_c+b_d-b_cd-(a_c+a_d-a_cd)))

Solution (Python):
import sys

lines = 1
inp = list()

for _ in range(lines):
    inp.append(sys.stdin.readline().strip())


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


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


lst = [int(i) for i in inp[0].split()]

a = lst[0]
b = lst[1]
c = lst[2]
d = lst[3]


if c > d:
    c, d = d, c

size = b - a + 1

c_size = b // c + (-a // c) + 1
d_size = b // d + (-a // d) + 1
cd_lcm = lcm(c, d)
cd_size = b // cd_lcm + (-a // cd_lcm) + 1

print(size - c_size - d_size + cd_size)

Solution (Python):
def sumcnt(a, b, val):
  am=(a-1)//val
  bm=b//val
  return bm-am

a,b,c,d=map(int,input().split())
p=c
q=d
while p%q>=1:
  bk=p%q
  p=q
  q=bk
mc=c//q
md=d//q
m=mc*md*q
summ=sumcnt(a,b,m)
sumc=sumcnt(a,b,c)
sumd=sumcnt(a,b,d)
print((b-a+1)-(sumc+sumd-summ))

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

c_cnt = b // c  - (a-1) // c
d_cnt = b // d  - (a-1) // d

lcm = c * d // gcd(c, d)
lcm_cnt = b // lcm  - (a-1) // lcm

print((b - a + 1) - (c_cnt + d_cnt - lcm_cnt))

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

num1 = c
num2 = d
while (num2>0):
  num1,num2 = max(num1,num2),min(num1,num2)
  r = num1%num2
  num1 = num2
  num2 = r
lcm = int(c*d/num1)

n1 = (a-1)//c+(a-1)//d-(a-1)//lcm
n2 = b//c+b//d-b//lcm
n = n2-n1
ans = b-a+1-n
print(int(ans))

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

big = max(c, d)
small = min(c, d)
lcm = 0
for i in range(1, 10 ** 8):
    surplus = big % small
    if surplus == 0:
        lcm = c * d // small
        break
    
    big = small
    small = surplus
    

c_num = b // c - (a - 1) // c
d_num = b // d - (a - 1) // d
cd_num = b // lcm - (a - 1) // lcm
num = b - a + 1


ans = num - (c_num + d_num - cd_num)

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)

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

num_all = b - a + 1
num_c = (b // c) - (a // c)
if a % c == 0:
    num_c += 1
num_d = (b // d) - (a // d)
if a % d == 0:
    num_d += 1
lcm_cd = lcm(c, d)
num_cd = (b // lcm_cd) - (a // lcm_cd)
if a % lcm_cd == 0:
    num_cd += 1

print(num_all - num_c - num_d + num_cd)

Solution (Python):
import math
import fractions

A,B,C,D=map(int,input().split())
E=C*D//(fractions.gcd(C,D))
def P(A,B,Q):
	if A%Q==0:
		return B//Q-A//Q+1
	else:
		return B//Q-A//Q
print(B-A+1-(P(A,B,C))-(P(A,B,D))+(P(A,B,E)))

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


def func(x,y):
    if(x<0):
        assert()
    if(y<=0):
        assert()
    
    
    
    return int((x//y))




lcm = int((C * D) / int(fractions.gcd(C, D)))
print(int( B - func(B, C) - func(B, D) + func(B, lcm) - A + 1 + func(A-1, C) + func(A-1, D) - func(A-1, lcm)))

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

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

A,B,C,D=MI()
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

a = (A-1) - ((A-1) // C + (A-1) // D - (A-1) // lcm(C, D))
b = B - (B // C + B // D - B // lcm(C, D))
print(b-a)

Solution (Python):
import fractions
import sys
sys.setrecursionlimit(10**8)


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


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


def solve(v):
    return v - v // c - v // d + v // lcm2(c, d)


print(solve(b) - solve(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


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


a = (A-1) - ((A-1) // C + (A-1) // D - (A-1) // lcm(C, D))
b = B - (B // C + B // D - B // lcm(C, D))
print(b-a)

Solution (Python):
import fractions
import math

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

NUM = B-A+1
CD = int((C*D)/fractions.gcd(C,D))

Ac = A
if Ac%C != 0:
  Ac += C-(Ac%C)

if B > Ac:
  DivC = ((B - Ac)//C)+1
elif B == Ac:
  DivC = 1
else:
  DivC = 0


Ad = A
if Ad%D != 0:
  Ad += D-(Ad%D)

if B > Ad:
  DivD = ((B - Ad)//D)+1
elif B == Ad:
  DivD = 1
else:
  DivD = 0

Acd = A
if Acd%CD != 0:
  Acd += CD-(Acd%CD)

if B > Acd:
  DivCD = ((B - Acd)//CD)+1
elif B == Acd:
  DivCD = 1
else:
  DivCD = 0

print(NUM - DivC - DivD + DivCD)

Solution (Python):
from fractions import gcd

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

Solution (Python):
from fractions import gcd

def my_count(a, b, num): 
  return b//num - (a-1)//num

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

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

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

res = (B//C + B//D) - ((A-1)//C + (A-1)//D) - (B//f2 - (A-1)//f2)

print((B-A+1) - res)

Solution (Python):
A,B,C,D=map(int,input().split())
import fractions
CD=C*D//(fractions.gcd(C,D))
l=[]
for i in [C,D,CD]:
    if A%i==0:
        l.append(B//i-A//i+1)
    else:
        l.append(B//i-A//i)
print(B-A+1-l[0]-l[1]+l[2])

Solution (Python):
def gcd(x, y):
  while(x % y != 0 and y % x != 0):
    if(x > y):
      x = x % y
    else:
      y = y % x
  if(x > y):
    return y
  else:
    return x
  
def cnt(N, C, D):
  return (N // C) + (N // D) - (N // (C * D // gcd(C, D)))

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

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

cntA = 0
cntA += (A-1) // C
cntA += (A-1) // D
cntA -= (A-1) // g


cntB = 0
cntB += B // C
cntB += B // D
cntB -= B // g

cnt = cntB - cntA
cnt = (B - A + 1) - cnt

print(cnt)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

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 = i_l()

A -= 1
l = lcm(C,D)

a = (B + C) // C - (A + C) // C
b = (B + D) // D - (A + D) // D
c = (B + l) // l - (A + l) // l

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

Solution (Python):


import sys
import math
import collections
import itertools
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def PL(L) : print(*L, sep="\n")

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

A, B, C, D = LI()

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

Solution (Python):
import math
from decimal import *
getcontext().prec = 50
a,b,c,d = map(int,input().split())

from functools import reduce
def gcd(a,b):
  if a < b:
    a, b = b, a
  while a % b != 0:
    a, b = b, a % b
  return b

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

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

gcd = lcm_list([c,d])

def solve(n):
	start = n * math.ceil(Decimal(a) / Decimal(n))
	end = n * (Decimal(b) // Decimal(n))
	
	return (end - start) // n + 1


ans = solve(c) + solve(d) - solve(gcd)

print(b - a - ans + 1)

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

def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if x%y == 0:
        return y
    else:
        return gcd(y, x%y)

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

print(muri(b)-muri(a-1))

Solution (Python):
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()
    if ans is not None:
        print(ans)


def solve():
    A, B, C, D = iip(False)

    a = B//C - (A-1)//C
    b = B//D - (A-1)//D

    cc = soinsuu_bunkai(C)
    dd = D
    for ccc in cc:
        if dd % ccc == 0:
            dd //= ccc
    CD = dd * C
    

    c = B//CD - (A-1)//CD

    
    return B-A+1 -a-b+c





def iip(listed=True, num_only=True):
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def sort_tuples(l, index):
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):
    if index >= len(l):
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):
    return "".join(sorted(s))


def iip_ord(startcode="a"):
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):
    for i in s:
        print(i)


def bitall(N):
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret


def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side="left"):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_=mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Solution (Python):
import fractions

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

f=fractions.gcd(c,d)
f2=c*d//f

p = b//c - (a-1)//c
q = b//d - (a-1)//d
r = b//(f2) - (a-1)//(f2)

print((b-a+1) - (p+q-r))

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

Solution (Python):
def q3():
    """ C - Anti-Division """
    from fractions import gcd  
    A,B,C,D = (int(i) for i in input().split())

    def lcm(x, y):
        """ 最小公倍数を求める """
        return (x * y) // gcd(x, y)

    num_C = B // C - (A - 1) // C  
    num_D = B // D - (A - 1) // D  
    lcm_CD = lcm(C, D)
    num_CD = B // lcm_CD - (A - 1) // lcm_CD  
    num_all = num_C + num_D - num_CD  
    ans = B - A + 1 - num_all
    print(ans)


if __name__ == '__main__':
    q3()

Solution (Python):
from fractions import gcd
a,b,c,d = map(int,input().split())
def l(x,y):
    return (x*y)//gcd(x,y)
U = b-a+1
s1 = b//c - (a-1)//c
s2 = b//d - (a-1)//d
s3 = b//l(c,d) - (a-1)//l(c,d)
print(U-(s1+s2-s3))

Solution (Python):
import fractions
a, b, c, d = [int(i) for i in input().split()]
"""
print(b // c, b // d, b // ((c * d) // fractions.gcd(c,d)), (a-1) // c, (a-1) // d, (a-1) // ((c * d) // fractions.gcd(c,d)))
print((c * d) // fractions.gcd(c,d))
"""
print(
    b - a + 1 - 
    (b // c + b // d 
        - b // ((c * d) // fractions.gcd(c,d)) 
    - (a-1) // c - (a-1) // d 
        + (a-1) // ((c * d) // fractions.gcd(c,d)))
)

Solution (Python):
import fractions

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

lcm = (C * D)//fractions.gcd(C, D)
area = B - A + 1
Carea = (B // C - (A-1) // C)
Darea = (B // D - (A-1) // D)
CDarea = (B//lcm - (A-1)//lcm)

print(area - (Carea+Darea-CDarea))

Solution (Python):
from fractions import gcd
a,b,c,d = map(int,input().split())
a = a-1
l = int(c*d/gcd(d,c))
cn = b//c-a//c
dn = b//d-a//d
ln = b//l-a//l
ans = b-a-cn-dn+ln
print(ans)

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

Solution (Python):
def my_gcd(x, y):
    if x >= y:
        m = x
        n = y
    else:
        m = y
        n = x
    r = m % n
    while r:
        m = n
        n = r
        r = m % n
    return n

def cm_between(a, b, p):
    a_mod = a % p
    b_mod = b % p
    if b - a < p and 0 < a_mod <= b_mod:
        return 0
    elif a_mod == 0:
        return ((b - b_mod - a) // p + 1)
    else:
        return (((b - b_mod) - (a + p - a_mod)) // p + 1)

a, b, c, d = map(int, input().split())
l = (c * d) // my_gcd(c, d)
print((b - a + 1) - cm_between(a, b, c) - cm_between(a, b, d) + cm_between(a, b, l))

Solution (Python):
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

a,b,c,d = load_list()

def f(n, x, y):
    return n // x + n // y - n // lcm(x, y)



print((b-a+1) - (f(b, c, d) - f(a-1, c, d)))

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










def get_non_div_cnt_with_range( upper ):
  num_of_range_with_C  = ( upper // C )
  num_of_range_with_D  = ( upper // D )
  num_of_range_with_CD = ( upper // ( C * D // gcd( C, D ) ))
  return upper - ( num_of_range_with_C + num_of_range_with_D - num_of_range_with_CD )

upper_limit = B
lower_limit = A - 1
  
print( get_non_div_cnt_with_range( upper_limit ) - get_non_div_cnt_with_range( lower_limit ) )

Solution (Python):
A,B,C,D=map(int,input().split())
import math,fractions
cd=C*D//fractions.gcd(C,D)
Cnum=B//C-(A-1)//C
Dnum=B//D-(A-1)//D
CDnum=B//cd-(A-1)//cd
print(B-A+1-Cnum-Dnum+CDnum)

Solution (Python):
from fractions import gcd

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

a_mod_c = a%c
if a_mod_c == 0:
  a_mod_c = c
a_c = a + (c - a_mod_c)
count_c = (b - a_c)//c + 1

a_mod_d = a%d
if a_mod_d == 0:
  a_mod_d = d
a_d = a + (d - a_mod_d)
count_d = (b - a_d)//d + 1

lcm = (c*d)//gcd(c,d)

a_mod_lcm = a%lcm
if a_mod_lcm == 0:
  a_mod_lcm = lcm
a_lcm = a + (lcm - a_mod_lcm)
count_lcm = (b - a_lcm)//lcm + 1

ans = (b-a+1) - (count_d + count_c - count_lcm)

print(int(ans))

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










def get_non_div_cnt_with_range( upper ):
  num_of_range_with_C  = ( upper // C )
  num_of_range_with_D  = ( upper // D )
  num_of_range_with_CD = ( upper // ( C * D // gcd( C, D ) ))
  return upper - ( num_of_range_with_C + num_of_range_with_D - num_of_range_with_CD )

upper_limit = B
lower_limit = A - 1
  
print( get_non_div_cnt_with_range( upper_limit ) - get_non_div_cnt_with_range( lower_limit ) )

Solution (Python):
import fractions

"""
A,B,C,D = list(map(int, input().split()))
cc = int(B/C) - int((A-1)/C)
dd = int(B/D) - int((A-1)/D)
cd = int(C*D / fractions.gcd(C,D))
ee = int(B/cd) - int((A-1)/cd)
U = B-A+1
print(U - (cc + dd - ee))
"""



A,B,C,D = list(map(int, input().split()))
def y(n, c, d):
    cd = c // fractions.gcd(c,d) * d
    cc = n//c
    dd = n//d
    ee = n//cd
    return n - cc - dd + ee

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

Solution (Python):
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect




from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  A, B, C, D = map(int, readline().split())
  
  def F(x):
    y = x//C
    y += x//D
    lcm = C*D//gcd(C,D)
    y -= x//lcm
    return x-y
  ans = F(B) -F(A-1)
  print(ans)
  
  
if __name__ == '__main__':
  main()

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, input().split())
"""
ans: B以下の整数から条件を満たす個数　 - A-1以下の整数から条件を満たす個数
X以下の整数から条件： X - X//c -X//d + X//lcm(c,d)
"""
def count(X, t):
    return X//t
cd = c * d // gcd(c, d)
b_under = b - count(b, c) - count(b, d) + count(b, cd)
a_under = a-1 - count(a-1, c) - count(a-1, d) + count(a-1, cd)

print( b_under - a_under )

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

bc = B // C 
bd = B // D
bcd = B // (C*D//fractions.gcd(C, D))
ans = B- (bc + bd - bcd)
ac = (A-1) // C
ad = (A-1) // D
acd = (A-1) //(C*D//fractions.gcd(C, D))
ans2=  A-1 - (ac + ad - acd)
ans = ans - ans2
print(ans)

Solution (Python):
from fractions import gcd

a,b,c,d = map(int,input().split())
a -= 1
def div(m,n):
	return m // n

ans_a = a - div(a,c) - div(a,d) + div(a,c*d//gcd(c,d))
ans_b = b - div(b,c) - div(b,d) + div(b,c*d//gcd(c,d))

print(ans_b - ans_a)

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 f(abc):
    a, b, c = abc
    return b//c-a//c


a, b, c, d = map(int, input().split())
a -= 1
print(b-a+sum(map(f, [(b, a, c), (b, a, d), (a, b, lcm(c, d))])))

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


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


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


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

Solution (Python):
from fractions import gcd

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

lcm = c * d // gcd(c, d)
a -= 1

under_b = b - (b//c + b//d - b//lcm)
under_a = a - (a//c + a//d - a//lcm)

answer = under_b - under_a
print(answer)

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

Solution (Python):
import fractions
a, b, c, d =map(int, input().split())
lcm = (c//fractions.gcd(c, d))*d
cnt = (b-a+1)
c_cnt = (b//c) - ((a-1)//c)
d_cnt = (b//d) - ((a-1)//d)
lcm_cnt = (b//lcm) - ((a-1)//lcm)
print(cnt + lcm_cnt - c_cnt - d_cnt)