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

def gcd(a,b):
  x = max([a,b])
  y = min([a,b])
  while y:
    x, y = y, x % y
  return x


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


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

cd = lcm(c,d)
b_c = b // c
b_d = b // d
a_c = (a-1)//c
a_d = (a-1)//d
b_cd = b//(cd)
a_cd = (a-1)//(cd)
nums = b-a+1

ans = nums - (b_c - a_c) - (b_d - a_d) + (b_cd - a_cd)


print(ans)

Solution (Python):
import fractions

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

countAB = B - A + 1
ansC = B // C - (A - 1) // C
ansD = B // D - (A - 1) // D
lcmCD = (C * D) // fractions.gcd(C,D)
ansCD = B // (lcmCD) - (A - 1) // (lcmCD)

print(countAB - (ansC + ansD - ansCD))

Solution (Python):
import fractions

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

lower_C = -(-A//C)
upper_C = B//C
num_C = upper_C - lower_C + 1
lower_D = -(-A//D)
upper_D = B//D
num_D = upper_D - lower_D + 1
lcm = C*D//fractions.gcd(C, D)
lower_lcm = -(-A//lcm)
upper_lcm = B//lcm
num_lcm = upper_lcm - lower_lcm + 1

print(B - A + 1 - num_C - num_D + num_lcm)

Solution (Python):
from sys import stdin
import fractions
def lcm(a,b):
    return a * b // fractions.gcd(a,b)
A,B,C,D = [int(x) for x in stdin.readline().rstrip().split()]
div_C = (B // C) - ((A-1) // C)
div_D = (B // D) - ((A-1) // D)
div_lcm = (B // lcm(C,D)) - ((A-1) // lcm(C,D))
ans = B - A  - div_C - div_D + div_lcm + 1
print(ans)

Solution (Python):
from sys import stdin


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


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


def main():
    A, B, C, D = [int(x) for x in stdin.readline().rstrip().split()]
    min_c = A // C if A % C == 0 else A // C + 1
    max_c = B // C if B % C == 0 else B // C
    min_d = A // D if A % D == 0 else A // D + 1
    max_d = B // D if B % D == 0 else B // D
    cd = lcm(C, D)
    min_cd = A // cd if A % cd == 0 else A // cd + 1
    max_cd = B // cd if B % cd == 0 else B // cd

    print((B - A + 1) - ((max_c + 1 - min_c) + (max_d + 1 - min_d) - (max_cd + 1 - min_cd)))


if __name__ == "__main__":
    main()

Solution (Python):
import fractions

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

def cantdivide(num, d1, d2):
  def lcm(num1, num2):
    return int(num1 * num2 / fractions.gcd(num1, num2))
  
  return num - (num//d1) - (num//d2) + (num//lcm(d1,d2))
  
ans = cantdivide(B, C, D) - cantdivide((A-1), C, D)

print(ans)

Solution (Python):

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

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

a_c=a//c
a_d=a//d
a_cd=a//(c*d//gcd(c,d))
a_ans=a-a_c-a_d+a_cd


b_c=b//c
b_d=b//d
b_cd=b//(c*d//gcd(c,d))
b_ans=b-b_c-b_d+b_cd


ans=1
if(a%c==0 or a%d==0):
  ans=0

print(int(b_ans-a_ans)+ans)

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

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

Cprimes = {}
Dprimes = {}

tmpC = C
d = 2
while tmpC > 1:
    if tmpC % d == 0:
        if d in Cprimes:
            Cprimes[d] += 1
        else:
            Cprimes[d] = 1
        tmpC //= d
    else:
        d += 1

tmpD = D
d = 2
while tmpD > 1:
    if tmpD % d == 0:
        if d in Dprimes:
            Dprimes[d] += 1
        else:
            Dprimes[d] = 1
        tmpD //= d
    else:
        d += 1

comPrimes = {}
for k in Cprimes:
    comPrimes[k] = Cprimes[k]
for k in Dprimes:
    if k in comPrimes:
        if comPrimes[k] < Dprimes[k]:
            comPrimes[k] = Dprimes[k]
    else:
        comPrimes[k] = Dprimes[k]

comVal = 1
for k in comPrimes:
    comVal *= k**comPrimes[k]






CdivNum = B//C - (A-1)//C
DdivNum = B//D - (A-1)//D
comDivNum = B//comVal - (A-1)//comVal
ans = B-(A-1)
ans -= CdivNum
ans -= DdivNum
ans += comDivNum

print(ans)

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

Solution (Python):


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

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

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


a_c = (A-1) // C
a_d = (A-1) // D
a_cd = (A-1) // lcm(C, D)


b_c = B // C
b_d = B // D
b_cd = B // lcm(C, D)


ab = (b_c + b_d - b_cd) - (a_c + a_d - a_cd)


ans = (B - A + 1) - ab

print(ans)

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




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

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

x = b-a+1
f = a-1
y = b//c+b//d-b//e
z = f//c+f//d-f//e

ans = x-(y-z)



print(ans)

Solution (Python):

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

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

a_div_c = a//c
ac_mod = a%c
bc = b//c

a_div_d = a//d
ad_mod = a%d
bd = b//d

a_div_lcm_cd = a//lcm_cd
alcm_cd_mod = a%lcm_cd
blcm_cd = b//lcm_cd

cnt_c = 0
cnt_d = 0
cnt_lcmcd = 0

cnt_c = (bc-a_div_c)
if not ac_mod:
    cnt_c+=1

cnt_d = (bd-a_div_d)
if not ad_mod:
    cnt_d+=1

cnt_lcmcd = (blcm_cd-a_div_lcm_cd)
if not alcm_cd_mod:
    cnt_lcmcd+=1
print(b-a+1-(cnt_c+cnt_d-cnt_lcmcd))

Solution (Python):
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
inputs = [int(s) for s in input().split()]
a = inputs[0]
b = inputs[1]
c = inputs[2]
d = inputs[3]
count = 0
minCD = min(c, d)
maxCD = max(c, d)

x0 = b-a+1
x1 = b//minCD - (a-1)//minCD
x2 = b//maxCD - (a-1)//maxCD
lcm = minCD * maxCD // gcd(minCD, maxCD)
x3 = b//lcm - (a-1)//lcm
if maxCD % minCD == 0:
    print(x0-x1)
else:
    print(x0-(x1+x2-x3))

Solution (Python):
import fractions

def fnc(start, end, divisor):
  if start > end:
    return False
  return start % divisor > end % divisor

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

rng = B - A + 1

Cmod = rng // C
if fnc(A+C*(rng // C), B, C):
  Cmod += 1

Dmod = rng // D
if fnc(A+D*(rng // D), B, D):
  Dmod += 1

lcm = C*D // fractions.gcd(C, D)
lcmod = rng // lcm
if fnc(A+lcm*(rng // lcm), B, lcm):
  lcmod += 1

print(rng - Cmod - Dmod+ lcmod)

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

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

Solution (Python):
import fractions

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


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

c_cnt = B//C - (A-1)//C


d_cnt = B//D - (A-1)//D

cd_lcm = lcm(C,D)

cd_cnt = B//cd_lcm - (A-1)//cd_lcm


print((B-A+1) - c_cnt - d_cnt + cd_cnt)

Solution (Python):
import sys,heapq,fractions
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

def foo(x,c,d):
    xc = (x)//c + 1
    xd = (x)//d + 1
    g = c * d // fractions.gcd(c,d)
    xg = (x)//g + 1
    z = x+1-(xc+xd-xg)
    ddprint("x {} c {} d {} xc {} xd {} g {} xg {} z {}".format(x,c,d,xc,xd,g,xg,z))
    return z

a,b,c,d = inm()
print(foo(b,c,d)-foo(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(x, y):
    return (x * y) // gcd(x, y)

div1 = b//c - a//c

div2 = b//d - a//d

e = lcm(c,d)
div3 = b//e - a//e

if a%c==0:
    div1 += 1
if a%d==0:
    div2 += 1
if a%e==0:
    div3 += 1

ans = b-a+1 - div1 - div2 + div3
print(ans)

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

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

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

dup=b//f-(a-1)//f

ans=b-(a-1)-(cc+dd-dup)

print(ans)

Solution (Python):
import fractions

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

def solve(a, b, c, d):
    cd = c * d // fractions.gcd(c, d)

    x = num_factors(c, a, b)
    y = num_factors(d, a, b)
    z = num_factors(cd, a, b)

    return b - a + 1 - x - y + z

def num_factors(x, a, b):
    return b // x - (a - 1) // x

main()

Solution (Python):



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


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

cb = b//c
ca = (a-1)//c
cc = cb-ca



db = b//d
da = (a-1)//d
dd = db-da



cd = c*d // gcd(c, d)
cdb = b//cd
cda = (a-1)//cd
cdcd = cdb-cda



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

Solution (Python):
import math
import sys
from decimal import *

fin = sys.stdin.readline
A, B, C, D = [Decimal(elem) for elem in fin().split()]



def warikiru(A, B, C):
    return (B // C) - math.ceil(A / C) + 1



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



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


warikiru_num = warikiru(A, B, C) + warikiru(A, B, D) - warikiru(A, B, lcm(C, D))

print(int(B - A + 1 - warikiru_num))

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 divisorNum(cd):
  global a, b
  res = a%cd
  length = b-a+1
  if res!=0:
    first = cd - res
    return (length - (first+1))//cd +1
  else:
    return (length - 1)//cd +1
  
e = lcm(c, d)
ans  = (b-a+1) - divisorNum(c) - divisorNum(d) + divisorNum(e)
print(ans)

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, input().split())
cnt = b - a + 1
li = []


li.append(b//c)
cnt -= (b // c)

li.append((a-1) // c)
cnt += ((a-1) // c)


li.append(b//d)
cnt -= (b // d)

li.append((a-1) // d)
cnt += ((a-1) // d)


div = (c * d) // gcd(c, d)
li.append(b // div)
cnt += (b // div)

li.append((a-1) // div)
cnt -= ((a-1) // div)

print(cnt)

Solution (Python):
a,b,c,d = map(int,input().split())
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
lcm = int(c*d / gcd(c,d))
ans = (b-a+1 - (b//c - (a-1)//c) - (b//d - (a-1)//d) + (b//lcm - (a-1)//lcm))
print(int(ans))

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

A -= 1

Cnum = B // C - A // C
Dnum = B // D - A // D

Cnum = B - A - Cnum
Dnum = B - A - Dnum

if(C < D):
    C, D = C, D

x = C*D

r = C % D

while(r != 0):
    C = D
    D = r
    r = C % D

LCM_CD = x // D

CDnum = B // LCM_CD - A // LCM_CD

CDnum = B - A - CDnum

ans = Cnum + Dnum - CDnum

print(ans)

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


def acinput():
    return list(map(int, input().split(" ")))
def lcm(x, y):
    return x*y //gcd(x, y)

a, b, c, d = acinput()
l = lcm(c, d)
CD = b//l-(a-1)//l
nC = b//c-(a-1)//c
nD = b//d-(a-1)//d

res = b-a+1-(nC+nD-CD)
print(int(res))

Solution (Python):
import sys
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  



s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))


def check(A, B, X):
    x = (A-1) // X
    y = (B-0) // X
    return y - x

import fractions

def main():
    A, B, C, D = i2nn()
    
    n = B - A + 1
    
    n -= check(A, B, C)
    
    n -= check(A, B, D)
    
    E = C * D // fractions.gcd(C, D)
    n += check(A, B, E)
    n = int(n)
    print(n)

main()

Solution (Python):

from fractions import gcd


def main():
    a, b, c, d = list(map(int, input().rstrip().split()))
    total = b - a + 1
    a -= 1
    divisible = 0
    div_c = b // c - a // c
    divisible += div_c
    div_d = b // d - a // d
    divisible += div_d
    lcm = c * d // gcd(c, d)
    div_lcm = b // lcm - a // lcm
    divisible -= div_lcm
    print(total - divisible)


if __name__ == "__main__":
    main()

Solution (Python):
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return int(gcd(b, c))

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

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

l = lcm(C, D)

A = A-1
x = (B-A)
y = (B//C - A//C)
z = (B//D - A//D)
a = (B//l - A//l)
print(x+a - (y+z))

Solution (Python):
a,b,c,d=map(int,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)
    
a=a-1
b=b-b//c-b//d+b//lcm(c,d)
a=a-a//c-a//d+a//lcm(c,d)
print(b-a)

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())
a = B//C-(A-1)//C
b = B//D-(A-1)//D
c  =lcm(C,D)
d = B//c-(A-1)//c
e = B - A +1 - a -b + d 
print(e)

Solution (Python):
import fractions

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

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



c_div1 = int((a-1) // c)
c_div2 = int(b // c)
d_div1 = int((a-1) // d) 
d_div2 = int(b // d)
lcm_div1 = int((a-1) // lcm)
lcm_div2 = int(b // lcm)

ans = int(c_div2 - c_div1) + int(d_div2 - d_div1) - int(lcm_div2 - lcm_div1)

print(int(b - a - ans + 1))

Solution (Python):
def lcm(x, y):
    import fractions
    return (x * y) // fractions.gcd(x, y)
a,b,c,d = map(int, input().split())
import math
res = b - a + 1

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


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


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

print(res)

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 =  list(map(int,input().split(" ")))
CD = lcm(C,D)
 
divisibleC = B // C - (A-1)//C
divisibleD = B // D - (A-1) // D
divisibleCD = B//CD - (A-1)//CD 
divisible = divisibleC +divisibleD - divisibleCD
numbers = B-A + 1
print(numbers -divisible )

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

e=b//c
f=-(-a//c)

ans=e-f+1

g=b//d
h=-(-a//d)

ans=ans+g-h+1

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

j=b//i
k=-(-a//i)

ans=ans-(j-k+1)

print(b-a+1-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)

a,b,c,d=map(int,input().split())
z=b-a+1
ak=(a-1)-((a-1)//c)-((a-1)//d)+((a-1)//(lcm(c,d)))
bk=b-b//c-b//d+b//(lcm(c,d))
print(bk-ak)

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

cd=lcm(c,d)

nac=a//c
nbc=b//c
nad=a//d
nbd=b//d
nacd=a//(cd)
nbcd=b//(cd)


nc=nbc-nac
if a%c==0: nc+=1

nd=nbd-nad
if a%d==0: nd+=1

ncd=nbcd-nacd
if a%(cd)==0: ncd+=1


ans=b-a+1-(nc+nd-ncd)

print(ans)

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

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

a, b, c, d = list(map(int, input().split()))
ttt = lcm(c, d)
tt = b-a+1
ta = b//c - (a-1)//c
tb = b//d - (a-1)//d
tc = b//ttt - (a-1)//ttt
print(tt-ta-tb+tc)

Solution (Python):
a, b, c, d = (int(i) for i in input().split())

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

cmax=b//c
cmin=(a-1)//c
dmax=b//d
dmin=(a-1)//d
cd=c*d//gcd(c, d)
cdmax=b//cd
cdmin=(a-1)//cd

yakuc=cmax-cmin
yakud=dmax-dmin
yakucd=cdmax-cdmin

yaku=yakuc+yakud-yakucd
ans=b-a+1-yaku
print(ans)

Solution (Python):
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return int(gcd(b, c))

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

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

l = lcm(C, D)

A = A-1
x = (B-A)
y = (B//C - A//C)
z = (B//D - A//D)
a = (B//l - A//l)
print(x+a - (y+z))

Solution (Python):
import fractions

A, B, C, D = map(int, input().split())
E = (C * D) // fractions.gcd(C, D)
num_c = B // C - A // C + (1 if A % C == 0 else 0)
num_d = B // D - A // D + (1 if A % D == 0 else 0)
num_e = B // E - A // E + (1 if A % E == 0 else 0)
num = B - A + 1 - (num_c + num_d - num_e)
print(num)

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())
e=lcm(c,d)

cc=(b//c)-((a-1)//c)
dd=(b//d)-((a-1)//d)
cd=(b//e)-((a-1)//e)
print((b-a+1)-(cc+dd-cd))

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(" "))

lcm_c_d = lcm(C, D)

N = B - A + 1

count_C = (B // C) - ((A - 1) // C)
count_D = (B // D) - ((A - 1) // D)
count_CD = (B // lcm_c_d) - ((A - 1) // lcm_c_d)


ans = N - count_C - count_D + count_CD

print(ans)

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


A,B,C,D = map(int,input().rstrip().split(' '))
x = (B // C) - ((A-1) // C)
y =  (B // D) - ((A-1) // D)
E = (C * D) // gcd(C,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

print((B-(B//C+B//D-B//lcm(C,D)))-(A-(A//C+A//D-A//lcm(C,D))))

Solution (Python):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

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

A -= 1
z = (C*D) // fractions.gcd(C, D)

b_num = B - (B // C + B // D - B // (C*D))
a_num = A - (A // C + A // D - A // (C*D))

b_num2 = B // z - B // (C*D)
a_num2 = A // z - A // (C*D)



print(b_num - a_num + (b_num2 - a_num2))

Solution (Python):
import os, sys, re, math

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

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

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

ret = B // C - (A-1) // C
ret += B // D - (A-1) // D
ret -= B // lcm - (A-1) // lcm
print(B-A+1-ret)

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

Solution (Python):
from fractions import gcd



a, b, c, d = map(int, input().split())
cd_gcd = gcd(c, d)
cd_lcm = c*d // cd_gcd
b_div_d = b // d
a_div_d = a // d
if a % d == 0:
    b_div_d += 1
b_div_c = b // c
a_div_c = a // c
if a % c == 0:
    b_div_c += 1
b_div_c_d = b // cd_lcm
a_div_c_d = a // cd_lcm
if a % cd_lcm == 0:
    b_div_c_d += 1
hoshugo_num = b_div_d - a_div_d + b_div_c - a_div_c - (b_div_c_d - a_div_c_d)
print(int(b - a + 1 - hoshugo_num))

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

def lcm(x, y):
    return (x * y) // fra.gcd(x, y)
  
def wari(max, k):
  return (max // k)

lcmCD = lcm(C, D)
bot = A - (wari(A, C) + wari(A, D) - wari(A, lcmCD))
top = B - (wari(B, C) + wari(B, D) - wari(B, lcmCD))

if A % C != 0:
  if A % D != 0:
    bot -= 1
  
print(top - bot)

Solution (Python):
a, b, c, d = (int(_) for _ in input().split())
def gcd(x, y):
   while y:
       x, y = y, x % y
   return x

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

div = lcm(c,d)

if a % c == 0:
    start = a //c
else:
    start = a // c + 1
stop = b // c 
diff = b - a + 1
diff -= (stop - start + 1)

if a % d == 0:
    start = a // d
else:
    start = a // d + 1
stop = b // d
diff -= (stop - start + 1)

if a % div == 0:
    start = a//div
else:
    start = a // div + 1
stop = b // div
diff += (stop - start + 1)

print(diff)

Solution (Python):
import fractions
s=input().split()
a,b,c,d=int(s[0]),int(s[1]),int(s[2]),int(s[3])
g=fractions.gcd(c,d)
l=(d*c)//g
e=-((a-1)//c)+b//c-((a-1)//d)+b//d+(a-1)//l-(b//l)
print(b-a+1-e)

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

Solution (Python):
a, b, c, d = map(int, input().split(' '))
res1 = a // c
res2 = a // d
res3 = b // c
res4 = b // d
tmp1 = c
tmp2 = d
if tmp1 < tmp2:
    while tmp1:
        tmp = tmp1
        tmp1 = tmp2 % tmp1
        tmp2 = tmp
    result = tmp2
else:
    if tmp1 >= tmp2:
        while tmp2:
            tmp = tmp2
            tmp2 = tmp1 % tmp2
            tmp1 = tmp
        result = tmp1
result = c * d // result
res5 = a // result
res6 = b // result
i = res3 - res1
j = res4 - res2
k = res6 - res5
if a % c == 0:
    i += 1
if a % d == 0:
    j += 1
if a % result == 0:
    k += 1

print(b - a - i - j + k + 1)

Solution (Python):
A, B, C, D = map(int, input().split())
def count(A, B, div):
    if B % div >= A % div and A % div > 0:
        return (B - A) // div
    else:
        return (B - A) // div + 1

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

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

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)

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

Solution (Python):
from fractions import gcd

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

def count_num(num):
  a_1 = num//c
  a_2 = num//d
  a_3 = num//(c * d // gcd(c, d))
  return num - (a_1 + a_2 - a_3)

print(int(count_num(b) - count_num(a-1)))

Solution (Python):
def main():
     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
     ans-=(b)//c-(a-1)//c
     ans-=(b)//d-(a-1)//d
     cd=lcm(c,d)
     ans+=(b)//(cd)-(a-1)//(cd)
     print(ans)
     
if __name__=='__main__':
     main()

Solution (Python):
from fractions import gcd

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

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

Solution (Python):
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)

all = b-a+1
c_cnt = b//c-(a-1)//c
d_cnt = b//d-(a-1)//d
lcm_cnt = b//lcm(c,d)-(a-1)//lcm(c,d)

print(all-(c_cnt+d_cnt)+lcm_cnt)

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)

round=lambda x:(x*2+1)//2

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

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

n = b - a + 1
x0 = (a - 1)// c
y0 = b // c
x1 = (a - 1) // d
y1 = b // d
x2 = (a - 1) // (c * d // gcd(c, d))
y2 = b // (c * d // gcd(c, d))

print(n - ((y0 - x0) + (y1 - x1) - (y2 - x2)))

Solution (Python):
from fractions import gcd

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

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

AdivisibleC = (A - 1) // C
AdivisibleD = (A - 1) // D
AdivisibleCD = (A - 1) // lcm(C, D)
AindivisibleCD = (A - 1) - (AdivisibleC + AdivisibleD - AdivisibleCD)

BdivisibleC = B // C
BdivisibleD = B // D
BdivisibleCD = B // lcm(C, D)
BindivisibleCD = B - (BdivisibleC + BdivisibleD - BdivisibleCD)

print(BindivisibleCD - AindivisibleCD)

Solution (Python):

import fractions

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

ans = b - a + 1 

if d%c == 0:
    ans -= b//c - a//c + (0 if a%c != 0 else 1)

else:
    x = b // c - a//c + (0 if a%c != 0 else 1)
    y = b // d - a//d + (0 if a%d != 0 else 1)

    lcm = c*d // fractions.gcd(c,d)
    t = b //lcm - a//lcm + (1 if a >= lcm and a%lcm ==0 else 0)

    ans = ans -x-y+t





print(ans)

Solution (Python):
import fractions

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



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

lcm_cd = lcm_base(c, d)

print((b-a+1)-(b//c + b//d - b//lcm_cd) + ((a-1)//c + (a-1)//d - (a-1)//lcm_cd))

Solution (Python):
import numpy as np
import math
import sys
sys.setrecursionlimit(10000000)

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

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

def main():
    A, B, C, D = [int(c) for c in input().split()]
    N_U = (B) - (A - 1)
    N_C = B // C - (A - 1) // C
    N_D = B // D - (A - 1) // D
    N_C_D = int(B // lcm(C, D) - (A - 1) // lcm(C, D))
    excluded_N_C_D = N_C + N_D - N_C_D
    print(int(N_U - excluded_N_C_D))


if __name__ == '__main__':
    main()

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

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

def f(a,b,c):
    return (b - a + 1) // c + (1 if b % c <= a % c else 0)

if c != 1 and d != 1:
    print ((b-a+1) - (b // c - ceil(a / c) + 1) - (b // d - ceil(a / d) + 1) + (b // lcm(c, d) - ceil(a / lcm(c, d)) + 1))
else:
    print (0)

Solution (Python):
import fractions


class AntiDivision:
    def __init__(self):
        self.A, self.B, self.C, self.D = map(int, input().split())

    def cal(self):
        a_b_count = self.B - self.A + 1
        lcm = (self.C * self.D) // fractions.gcd(self.C, self.D)
        c_count = self.B // self.C - (self.A - 1) // self.C
        d_count = self.B // self.D - (self.A - 1) // self.D
        lcm_count = self.B // lcm - (self.A - 1) // lcm

        print(a_b_count - c_count - d_count + lcm_count)


antiDivision = AntiDivision()
antiDivision.cal()

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)


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

a_c = A // C
a_c_mod = A % C
b_c = B // C
a_d = A // D
a_d_mod = A % D
b_d = B // D

l = lcm(C, D)

a_cd = A // l
a_cd_mod = A % l
b_cd = B // l

if a_c_mod == 0:
    a_c -= 1

if a_d_mod == 0:
    a_d -= 1

if a_cd_mod == 0:
    a_cd -= 1

print((B - A + 1) - (b_c - a_c) - (b_d - a_d) + (b_cd - a_cd))

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

lcm_cd = lcm(C,D)

tmp_all_c=B//C
tmp_all_d=B//D
tmp_all_cd=B//lcm_cd
tmp_all=tmp_all_c+tmp_all_d-tmp_all_cd

tmp_untilA_c=(A-1)//C
tmp_untilA_d=(A-1)//D
tmp_untilA_cd=(A-1)//lcm_cd
tmp_untilA=tmp_untilA_c+tmp_untilA_d-tmp_untilA_cd

print((B-A+1)-(tmp_all-tmp_untilA))

Solution (Python):
from sys import stdin

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

def count_div(a, b, div):
    ac = a // div
    if ac * div < a:
        ac += 1
    bc = b // div
    if bc * div > b:
        bc -= 1
    return bc - ac + 1

if __name__ == '__main__':
    A, B, C, D = [int(x) for x in stdin.readline().rstrip().split()]

    nc = count_div(A, B, C)
    
    nd = count_div(A, B, D)
    
    CD = int(C * D / gcd(C, D))
    ncd = count_div(A, B, CD)
    
    n = B - A + 1
    
    ans = n - (nc + nd - ncd)
    print(ans)

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

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

if c > d:
	tmp = c
	c = d
	d = tmp
lcm = (c * d) // gcd(c, d)

Cstart = a - (a % c) + c if a % c != 0 else a
Cend = b - (b % c)
Dstart = a - (a % d) + d if a % d != 0 else a
Dend = b - (b % d)
LCMstart = a - (a % lcm) + lcm if a % lcm != 0 else a
LCMend = b - (b % lcm)






Ccnt, Dcnt, LCMcnt = 0, 0, 0
if Cstart <= Cend:
	Ccnt = (Cend - Cstart) // c + 1
if Dstart <= Dend:
	Dcnt = (Dend - Dstart) // d + 1
if LCMstart <= LCMend:
	LCMcnt = (LCMend - LCMstart) // lcm + 1




print((b-a+1) - Ccnt - Dcnt + LCMcnt)

Solution (Python):
from fractions import gcd

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

A, B, C, D = map(int, input().split())
if A%C == 0:
    wid = A
else:
    wid = (A//C + 1) * C
if wid > B:
    c_num = 0
else:
    c_num = (B-wid) // C + 1

if A%D == 0:
    wid = A
else:
    wid = (A//D + 1) * D
if wid > B:
    d_num = 0
else:
    d_num = (B-wid) // D + 1

E = lcm(C, D)
if A%E == 0:
    wid = A
else:
    wid = (A//E + 1) * E
if wid > B:
    e_num = 0
else:
    e_num = (B-wid) // E + 1

print(B - A - (c_num + d_num - e_num) + 1)

Solution (Python):
import fractions 

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

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

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

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

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)
  
E = lcm(C,D)

C_min = int(A-1) // C
C_max = B // C
D_min = int(A-1) // D
D_max = B // D
E_min = int(A-1) // E
E_max = B // E
min_num = C_min + D_min - E_min
max_num = C_max + D_max - E_max
ans = B - (A-1) - (max_num - min_num)
print(ans)

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

saishou=(c*d)//fractions.gcd(c, d)
warikiri_a=[]
warikiri_b=[]
amari_a=[]
amari_b=[]
warikiri_a.append(a//c)
warikiri_a.append(a//d)
warikiri_a.append(a//saishou)
warikiri_b.append(b//c)
warikiri_b.append(b//d)
warikiri_b.append(b//saishou)
amari_a.append(a%c)
amari_a.append(a%d)
amari_a.append(a%saishou)
amari_b.append(b%c)
amari_b.append(b%d)
amari_b.append(b%saishou)
list=[]
for i in range(3):
    if amari_a[i]==0:
        list.append(warikiri_b[i]-warikiri_a[i]+1)
    else:
        list.append(warikiri_b[i]-warikiri_a[i])

for i in range(3):
    if list[i]<0:
        list[i]=0
print((b-a+1)-(list[0]+list[1]-list[2]))

Solution (Python):
from fractions import gcd


def main():
    A, B, C, D = map(int, input().split())
    Q_AC, R_AC = A // C, A % C
    Q_AD, R_AD = A // D, A % D
    Q_BC, R_BC = B // C, B % C
    Q_BD, R_BD = B // D, B % D
    Q_ACD, R_ACD = A // lcm(C, D), A % lcm(C, D)
    Q_BCD, R_BCD = B // lcm(C, D), B % lcm(C, D)
    N_C = num_baisu(Q_AC, Q_BC, R_AC, R_BC)
    N_D = num_baisu(Q_AD, Q_BD, R_AD, R_BD)
    N_CD = num_baisu(Q_ACD, Q_BCD, R_ACD, R_BCD)
    print(B - A + 1 - (N_C + N_D - N_CD))
    return


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


def num_baisu(Q_A, Q_B, R_A, R_B):
    if R_A == 0 and R_B == 0:
        return Q_B - Q_A + 1
    elif R_A == 0 and R_B != 0:
        return Q_B - Q_A + 1
    elif R_A != 0 and R_B == 0:
        return Q_B - (Q_A + 1) + 1
    else:
        return Q_B - (Q_A + 1) + 1


if __name__ == "__main__":
    main()

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


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

g = gcd(C, D)

print((B - A + 1) - (B // C - (A - 1) // C) - (B // D - (A - 1) // D) + (B // (C * D // g) - (A - 1) // (C * D // g)))

Solution (Python):

import fractions

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


a, b, c, d = map(int, input().split())
ac = (a - 1) // c
ad = (a - 1) // d
acd = (a - 1) // lcm(c, d)
bc = b // c
bd = b // d
bcd = b // lcm(c, d)
print((b - a + 1) - ((bc + bd - bcd) - (ac + ad - acd)))