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

c_n=b//c-(a-1)//c
d_n=b//d-(a-1)//d
gc_n=b//gc-(a-1)//gc

res=b-(a-1)-(c_n+d_n-gc_n)

print(int(res))

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())
print(B-A+1-B//C+(A-1)//C-B//D+(A-1)//D+B//lcm(C,D)-(A-1)//lcm(C,D))

Solution (Python):
A, B, C, D = map(int, input().split())
E = A - 1
def func(a,b):
    if a%b == 0:
        return b
    return func(b,a%b)

c = min(C,D)
d = max(C,D)
G = func(d,c)
L = (C*D) // G

x = B - ((B//C)+(B//D)-(B//L))
y = E - ((E//C)+(E//D)-(E//L))
print(x-y)

Solution (Python):
def gcd(n, m):
    x = max(n, m)
    y = min(n, m)

    while y != 0:
        x, y = y, x % y

    return x


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

    
    fclb = b // c
    
    fdlb = b // d

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

    duplicated_b = b // lcm
    count_b = b - fclb - fdlb + duplicated_b

    fcla = (a - 1) // c
    fdla = (a - 1) // d
    duplicated_a = (a - 1) // lcm
    count_a = (a - 1) - fcla - fdla + duplicated_a

    score = count_b - count_a
    print(score)


main()

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

def gcd(v, w):
  while w:
    v, w = w, v % w
  return v

def calc(l, m, n):
  return l//m + l//n - l//lcm(m,n)

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

tmp = b-a+1
aa = calc((a-1), c, d)
bb = calc(b, c, d)
print(tmp - (bb - aa))

Solution (Python):

A, B, C, D = map(int, input().split())
ans = 0
Am = 0
Bm = 0
df = 0
t = 0

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

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

t = B - A + 1
Am = (A - 1) // C + (A - 1) // D - (A - 1)//lcm(C, D)
Bm = B // C + B // D - B // lcm(C, D)
ans = t - (Bm - Am)

print(ans)

Solution (Python):

A, B, C, D = map(int, input().split())
ans = 0
Am = 0
Bm = 0
df = 0
t = 0

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

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

t = B - A + 1
Am = B // C - (A - 1) // C
Bm = B // D - (A - 1) // D
df = B // lcm(C, D) - (A - 1)//lcm(C, D)
ans = t - (Am + Bm) + df
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
def lcm(x, y):
    return (x * y) // gcd(x, y)
  
d=B//D-(A-1)//D
c=B//C-(A-1)//C
tmp=lcm(C,D)
cd=B//(tmp)-(A-1)//(tmp)
print(B-A+1-c-d+cd)

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

s = b - a + 1
x = b // c - (a - 1) // c
y = b // d - (a - 1) // d
z = b // lcm(c, d) - (a - 1) // lcm(c, d)

print(s - (x + y) + z)

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

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

def diff(i, j, k):
	return i - (i//j) - (i//k) + (i//lcm(j,k))

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

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

Solution (Python):
def gcd(x, y): 
  
   while(y): 
       x, y = y, x % y 
  
   return x 
[a,b,c,d]=[int(x) for x in input().split()]
k=(c*d)//gcd(c,d)
count=(b-a+1)-((b//c-(a-1)//c)+(b//d-(a-1)//d)-(b//k-(a-1)//k))
print(count)

Solution (Python):
a,b,c,d=map(int,input().split())
B=b
A=a-1
def gcd(x,y):
    while y!=0:
        x,y=y,x%y
    return x
def mm(x,y):
    return int(x*y/gcd(x,y))
m_B=B-B//c-B//d+B//mm(c,d)
m_A=A-A//c-A//d+A//mm(c,d)
print(m_B-m_A)

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

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

def cnt(n,x,y):
    return n-n//x-n//y+n//lcm(x,y)
  
print(cnt(b,c,d)-cnt(a-1,c,d))

Solution (Python):
import fractions

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

a-=1

buf_1=a-(a//c+a//d)+a//((c*d)//fractions.gcd(c,d))
buf_2=b-(b//c+b//d)+b//((c*d)//fractions.gcd(c,d))

print(buf_2-buf_1)

Solution (Python):

import fractions

def count_antidivision(n, x, y):
    """
    returns count of numbers from 1 to n which are not dividable by x nor y.
    """
    return n - n // x - n // y + n // (x * y // fractions.gcd(x, y))    

def main(A, B, C, D):
    return count_antidivision(B, C, D) - count_antidivision(A-1, C, D)

if __name__ == '__main__':
    A, B, C, D = list(map(int, input().split()))
    print(main(A, B, C, D))

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

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

A -= 1
tmp1 = A - A//C - A//D + A//((C*D)//fractions.gcd(C,D))
tmp2 = B - B//C - B//D + B//((C*D)//fractions.gcd(C,D))
answer = tmp2-tmp1

print(answer)

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

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



def cnt_num(cri):
  l = cri // c
  m = cri // d
  cd = c * d // gcd(c,d)
  lm = cri // cd
  return cri - (l + m - lm)
  
ans = cnt_num(b) - cnt_num(a-1)
print(ans)

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


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


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


def calc(t, x, y, z):
    return t // x + t // y - t // z


g = lcm(C, D)
bcnt = calc(B, C, D, g)
acnt = calc(A - 1, C, D, g)
print(B - bcnt - (A - 1 - acnt))

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


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


def calc(l, r, x):
    tmp = r // x - l // x
    if l % x == 0:
        tmp += 1
    return tmp


ccnt = calc(A, B, C)
dcnt = calc(A, B, D)
g = gcd(C, D)
cdcnt = calc(A, B, (C // g) * (D // g) * g)
print(B - A + 1 - (ccnt + dcnt - cdcnt))

Solution (Python):
import sys,fractions,math
A,B,C,D = map(int,input().split())
lcm = C * D // fractions.gcd(C,D)
def d_num(x):
    X = B//x
    Y = (A-1)//x
    return X - Y
print(B - A + 1 - (d_num(C)+d_num(D)-d_num(lcm)))

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

b_num = B//C + B//D - B//lcm(C, D)
a_num = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)

print((B-A+1) -(b_num - a_num))

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


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

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

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

Solution (Python):
import fractions

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

cc=b//c-(a-1)//c
dd=b//d-(a-1)//d
ee=b//(c * d // fractions.gcd(c, d))-(a-1)//(c * d // fractions.gcd(c, d))
    
print((b-a+1)-(cc+dd-ee))

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

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

print(ans)

Solution (Python):
from fractions import gcd
a,b,c,d = map(int,input().split())
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):
def g_c_d(x, y):
    if y == 0:
        return x
    else:
        return g_c_d(y, x % y)


def l_c_d(x, y):
    return x * y // g_c_d(x, y)


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

Solution (Python):
import fractions

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

c_min = (a-1) // c
c_max = b // c
c_count = c_max - c_min

d_min = (a-1) // d
d_max = b // d
d_count = d_max - d_min

cd_min = (a-1) // (c * d // fractions.gcd(c, d))
cd_max = b // (c * d // fractions.gcd(c, d))
cd_count = cd_max - cd_min

print(b - (a-1) - c_count - d_count + cd_count)

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

Solution (Python):
import math
import fractions
l, r, p1, p2 = list(map(int, input().split()))
lcm = p1*p2 // fractions.gcd(p1, p2)
ans = (r-l+1) - (r//p1-(l-1)//p1) - (r//p2-(l-1)//p2) + (r//lcm)-(l-1)//lcm
print(ans)

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

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

E = C * D // gcd(C, D)
ac = (A-1) // C
ad = (A-1) // D
acd = (A-1) // E
bc = B // C
bd = B // D
bcd = B // E

print(B-A+1-(bc-ac)-(bd-ad)+(bcd-acd))

Solution (Python):
from fractions import gcd

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

lcmcd = c * d / gcd(c, d)
lcmcd = int(lcmcd)

numb = b - (b//c) - (b//d) + (b//lcmcd)
numa = a-1 - ((a-1)//c) - ((a-1)//d) + ((a-1)//lcmcd)

print(numb-numa)

Solution (Python):

import sys
import math
from fractions import gcd

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

def nr_divide(a: int, b: int, x: int):
    count = b//x - a//x + 1
    if a % x != 0:
        count -= 1
    return count

def solve(A: int, B: int, C: int, D: int):
    E = my_lcm(C, D)
    N = B - A + 1
    nrC = nr_divide(A, B, C)
    nrD = nr_divide(A, B, D)
    nrE = nr_divide(A, B, E)
    count = N - nrC - nrD + nrE
    print(count)
    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)
    return

if __name__ == '__main__':
    main()

Solution (Python):

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

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

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

res_b = b - b // c - b // d + b // cd
a -= 1
res_a = a - a // c - a // d + a // cd

print(res_b - res_a)

Solution (Python):
import fractions

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

a_c = (a - 1) // c
a_d = (a - 1) // d
a_cd = (a - 1) // (c * d // fractions.gcd(c, d))

b_c = b // c
b_d = b // d
b_cd = b // (c * d // fractions.gcd(c, d))

print((b - b_c - b_d + b_cd) - ((a - 1) - a_c - a_d + a_cd))

Solution (Python):
import math

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()[:4])

total=B-A+1

C_div=B//C-(A-1)//C
D_div=B//D-(A-1)//D

CD=lcm(C,D)

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

print(total-C_div-D_div+CD_div)

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

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


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


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

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

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


u = b - a + 1


ans = u - nc - nd + ncd

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)

temp=input().split()
A=int(temp[0])
B=int(temp[1])
C=int(temp[2])
D=int(temp[3])

x=B//C-(A-1)//C
y=B//D-(A-1)//D
s=lcm(C, D)
z=B//s-(A-1)//s

print(int(B-A+1-x-y+z))

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

C, D = min(C,D), max(C,D)

dividable_C = B//C - (A-1)//C
dividable_D = B//D - (A-1)//D

x = C*D
r = D%C
while r!=0:
    D = C
    C = r
    r = D%C
saisho_ko_baisu = x//C

dividable_CD = (B//saisho_ko_baisu) - ((A-1)//saisho_ko_baisu)

print(B - A + 1 - (dividable_C + dividable_D - dividable_CD))

Solution (Python):
import fractions

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

c_n = (b // c) - ((a - 1) // c)
d_n = (b // d) - ((a - 1) // d)

lcm = (c * d) // fractions.gcd(c, d)
lcm_n = (b // lcm) - ((a - 1) // lcm)

ans = (b - a + 1) - (c_n + d_n - lcm_n)
print(ans)

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

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

b_num = B//C + B//D - B//lcm(C, D)
a_num = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)
print((B-(A-1))-(b_num-a_num))

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)  

def cal(n):
  if a%n ==0 and b%n ==0:
    return b//n - a//n + 1
  else:
    return b//n - a//n
    
cd = lcm(c, d)
ans = (b-a+1) - (b//c - (a-1)//c) - (b//d - (a-1)//d) + (b//cd - (a-1)//cd)
print(ans)

Solution (Python):
from fractions import gcd

lcm = lambda x, y: x*y//gcd(x, y)
calc = lambda mod, a, b: (b//mod-a//mod) if a%mod != 0 else (b//mod-a//mod+1) 

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

print(b-a+1-calc(c, a, b)-calc(d, a, b)+calc(lcm(c, d), a, b))

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

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

def f(a, b, n):
  return b // n - (a - 1) // n

acc = 0
acc += f(a, b, c)
acc += f(a, b, d)
acc -= f(a, b, c * d // gcd(c, d))
print(b - a + 1 - acc)

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

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

def f(a, b, n):
  x = (a // n) * n
  if x < a:
    x += n
  if x > b:
    return 0
  result = 0
  if a <= x <= b:
    result += 1
  result += (b - x) // n
  return result

acc = 0
acc += f(a, b, c)
acc += f(a, b, d)
acc -= f(a, b, c * d // gcd(c, d))
print(b - a + 1 - acc)

Solution (Python):

import fractions

A, B, C, D = map(int, input().split())
def ad(X):
    XmodC = int(X // C)
    XmodD = int(X // D)
    CD_lcm = int(C * D / fractions.gcd(C, D))
    XmodCD = int(X // CD_lcm)
    return X - (XmodC + XmodD - XmodCD)
ans = ad(B) - ad(A - 1)
print(ans)

Solution (Python):
a, b, c, d = map(int, input().split())
import fractions
f = fractions.gcd(c, d)
e = c * d // f

c_a, c_b = (a-1)//c, b//c
c_ab = c_b - c_a 

d_a, d_b = (a-1)//d, b//d
d_ab = d_b - d_a 

e_a, e_b = (a-1)//e, b//e
e_ab = e_b - e_a

ans = (b - a + 1) - (c_ab + d_ab - e_ab)
print(ans)

Solution (Python):

def gcd(m, n):
    if(n == 0):
        return m
    return gcd(n, m % n)


(a, b, c, d) = [int(i) for i in input().split()]
c_d_lcm = (c * d) // gcd(c, d)
able_c_div = b // c - (a - 1) // c
able_d_div = b // d - (a - 1) // d
able_c_d_div = b // c_d_lcm - (a - 1) // c_d_lcm
diff = b - (a - 1)
print(diff - (able_c_div + able_d_div - able_c_d_div))

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


def l_c_m(x, y):
    return x * y // g_c_d(x, y)


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

Solution (Python):
import fractions

def lcm(p,q):
    return (p*q) // fractions.gcd(p,q)

def g(x,k):
    return x // k

def f(x,c,d):
    return x - (g(x,c)+g(x,d)-g(x,lcm(c,d)))

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

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

Solution (Python):
import numpy as np
from fractions import gcd

def lcm(x, y):
    return x * y // gcd(x, y)
A, B, C, D = [int(x) for x in input().split(" ")]
n_targets = B - A + 1

n_compC = n_targets - (B // C - (A - 1) // C)
n_compD = n_targets - (B // D - (A - 1) // D)
n_compCandD = n_targets - (B // lcm(C, D) - (A - 1) // lcm(C, D))

print(n_compC + n_compD - n_compCandD)

Solution (Python):


import sys

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

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

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
a=hoge[0]
b=hoge[1]
c=hoge[2]
d=hoge[3]

total=b-a+1

csum=(b//c-(a-1)//c)
total-=csum

dsum=(b//d-(a-1)//d)
total-=dsum

cdlcm=lcm(c,d)
cdsum=(b//cdlcm-(a-1)//cdlcm)
total+=cdsum

print(total)

Solution (Python):
def main():
    A, B, C, D = list(map(int, input().split()))
    candiv_C = num_can_div(A, B, C)
    candiv_D = num_can_div(A, B, D)
    candiv_lcm = num_can_div(A, B, lcm(C, D))

    print(B-A+1 - candiv_C - candiv_D + candiv_lcm)

def num_can_div(A, B, x):
    return B//x - (A-1)//x

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

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

main()

Solution (Python):


def gcd(x, y):
  if x > y:
    return gcd(y, x)
  while x != 0:
    x, y = y % x, x
  return y
def lcm(x, y):
  return (x*y)//gcd(x, y)

a, b, c, d = map(int, input().split())
bx = b - (b//c + b//d - b//lcm(c, d))
ax = (a-1) - ((a-1)//c + (a-1)//d - (a-1)//lcm(c, d))
print(bx-ax)

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

Solution (Python):
from sys import stdin
import fractions


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

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

    z = B//C_D - A//C_D

    if A % C == 0:
        x = (B//C) - (A//C) + 1
    else:
        x = (B//C) - (A//C)

    y = B//D - A//D

    print((B-A+1)-(x+y-z))


if __name__ == "__main__":
    main()

Solution (Python):
from copy import *
from sys import *
import math
import queue
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


def main():
	a,b,c,d = map(int,input().split())
	n = c * d // gcd(c,d)
	a-=1
	ans_b = b - (b//c + b//d - b//n)
	ans_a = a - (a//c + a//d - a//n)
	print(ans_b - ans_a)


def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

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

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Solution (Python):
def eratosthenes(a, b):
    if a < b:
        return eratosthenes(b, a)
    while a%b != 0:
        temp = b
        b = a%b
        a = temp

    return b


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


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

count = B - A + 1
count -= B // C - (A-1) // C
count -= B // D - (A-1) // D
L = lcm(C, D)
count += B // L - (A-1) // L

print(count)

Solution (Python):






 
def gcd (p, q):
    temp = max(p, q)
    q = min(p, q)
    p = temp
    if q == 0:
        return p
    else:
        return gcd(q, p%q)
    
def lcm(p, q):
    return p * q // gcd(p, q)
 
def g(x, k):
    return x//k
 
def f(x, c, d):
    return x - (g(x, c) + g(x, d) - g(x, lcm(c,d)))
 
a,b,c,d = list(map(lambda x:int(x) ,input().split()))
 
print(f(b, c, d) - f(a-1, c, d))

Solution (Python):
A, B, C, D = [int(_) for _ 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)

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

ans = dvi_num2(B, C, D) - dvi_num2(A - 1, C, D)
print(int(ans))

Solution (Python):
from fractions import gcd


def space():
    return list(map(int, input().split()))


A, B, C, D = space()
E = int(C * D / gcd(C, D))

c = B // C - (A-1) // C
d = B // D - (A-1) // D
e = B // E - (A-1) // E

print(1 + B - A - (c + d - e))

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

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

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

def f(a, b, c):
	leftc = a + (c - a % c) % c
	if leftc > b:
		s = 0
	else:
		rightc = b - b % c
		s = (rightc - leftc) // c + 1
	return s

def F(x, c, d):
	res = x
	res -= x // c
	res -= x // d
	res += x // lcs(c, d)
	return res


def main():
	a, b, c, d = map(int, input().strip().split())
	print(F(b, c, d) - F(a-1, c, d))
	
	
	
	
	

if __name__ == '__main__':
	main()

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

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

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

def f(a, b, c):
	leftc = a + (c - a % c) % c
	if leftc > b:
		s = 0
	else:
		rightc = b - b % c
		s = (rightc - leftc) // c + 1
	return s

def main():
	a, b, c, d = map(int, input().strip().split())
	n = b-a+1
	s = f(a, b, c)
	t = f(a, b, d)
	u = f(a, b, lcs(c, d))
	
	print(n - (s + t - u))

if __name__ == '__main__':
	main()

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

def f(x,y,p):
    res = 0
    if x%p == 0:
        start = x+1
        res += 1
    else:
        start = (x//p+1)*p
        if start > y:
            return 0
        if start == y:
            return 1
        start += 1
        res += 1
    return res+(y-start+1)//p


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


def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = gcd(a,l[i])
    return a


def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    
    return [w[0],w[1]]


def extgcd_2(a,b):    
    g = gcd(a,b)
    a = a//g
    b = b//g
    return extgcd(a,b)


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

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

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

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

def div(a, b, x):
    if a%x!=0:
        return b//x-a//x
    else:
        return b//x-(a//x-1)

print(int(b-a+1-(div(a,b,c)+div(a,b,d)-div(a,b,lcm(c,d)))))

Solution (Python):
import fractions

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

mc = b // c
md = b // d
mcd = b // ((c * d) // (fractions.gcd(c, d)))

mb = b - mc - md + mcd

amc = (a - 1) // c
amd = (a - 1) // d
amcd = (a - 1) // ((c * d) // fractions.gcd(c, d))

ma = (a - 1) - amc - amd + amcd

print(int(mb - ma))

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

Solution (Python):
import fractions

a,b,c,d = map(int, input().split())
nc = b//c-(a-1)//c
nd = b//d-(a-1)//d


e = c*d//fractions.gcd(c,d)
nc_d = b//e-(a-1)//e
print(b-a+1-nc-nd+nc_d)

Solution (Python):
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 div_num(a,x,y):
    return a//x + a//y - a//lcm(x,y)

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

ans = (b-a+1) - ( div_num(b,c,d) - div_num((a-1),c,d) )

print(ans)

Solution (Python):

from fractions import gcd

def lcm(a, b):
    """ aとbの最小公倍数を返す """
    return a*b // gcd(a, b)

def f(a, x, y):
    """ a以下で、xでもyでも割り切れないものの数"""
    return a - (a//x + a//y - a//lcm(x,y))

def solve():
    A,B,C,D = list(map(int, input().split()))
    ans = f(B, C, D) - f(A-1, C, D)
    print(ans)

if __name__ == "__main__":
    solve()

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

C_mod0 = B//C - (A-1)//C
D_mod0 = B//D - (A-1)//D
i = D
j = C % D
while j > 0:
    k = j
    j = i % j
    i = k

CD_mod0 = B*i // (C*D) - (A-1)*i // (C*D)
Total = B - A + 1
ans = Total - C_mod0 - D_mod0 + CD_mod0

print(ans)

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

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

Solution (Python):
a, b, c, d  = map(int,input().split())
def divbyk(k):
    return    b//k-(a-1)//k

def gcd(x,y):
    n = min(x,y)
    m = max(x,y)
    if n == 0:
        return m
    else:
        return round(gcd(n,m-(m//n)*n))

ans = b-(a-1) - divbyk(c) - divbyk(d) + divbyk(c*d//gcd(c,d))
print(int(ans))

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

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

cc=b//c-(a-1)//c
dd=b//d-(a-1)//d
s=b//(c*d//fractions.gcd(c,d))-(a-1)//(c*d//fractions.gcd(c,d))


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

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
lcm = C * D // gcd(C, D)
cd = B // lcm - (A - 1) // lcm
ans = (B - A + 1) - cc - dd + cd
print(ans)

Solution (Python):
import fractions

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

a, b, c, d = map(int, input().split())
c_ = b//c - (a-1)//c
d_ = b//d - (a-1)//d
cd_ = b//lcm(c, d) - (a-1)//lcm(c, d)
print(b - a + 1 - (c_ + d_ - cd_))

Solution (Python):



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

cer=b//c-a//c
der=b//d-a//d
if(a%c==0):
	cer+=1
if(a%d==0):
	der+=1
def gcd(a,b):
	if(b==0):
		return a
	else:
		return gcd(b,a%b)
newno=int((c*d)/gcd(c,d))
newer=b//newno-a//newno
if(a%newno==0):
	newer+=1
print(b-a+1-cer-der+newer)

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

def calc(n):
    return (B // n) - ((A - 1) // n)

def lcm(c, d):
    a, b = c, d
    if c < d:
        a, b = d, c
    while b != 0:
        a, b = b, a % b

    return c * d // a

E = lcm(C, D)
print(calc(1) -calc(C)-calc(D)+calc(E) )

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


com=lcm(c, d)
tmp=(b-b//com)-(a-a//com)
print(count-tmp)

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

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

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

print(f(b, c, d) - f(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

lcm = C * D // gcd(C, D)
total = B - A + 1
ans = total - ((B // C - (A - 1) // C) + (B // D - (A - 1) // D) - (B // lcm - (A - 1) // lcm))
print(ans)

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

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

lcm = c*d//gcd(c,d)
cc = b//c - (a-1)//c
dd = b//d - (a-1)//d
cd = b//lcm - (a-1)//lcm
print(b-a+1-cc-dd+cd)

Solution (Python):

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

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

i = a - 1


x = b - (b // c + b // d - b // z)

y = i - (i // c + i // d - i // z)

print(x - y)

Solution (Python):
def C_AntiDivision():
    from sys import version_info
    try:
        from math import gcd
    except:
        from fractions import gcd
    a,b,c,d = map(int, input().split())
    total = b - a + 1
    total_c = (b // c) + (-a // c) + 1
    total_d = (b // d) + (-a // d) + 1
    gcd_cd = c*d // gcd(c,d)
    total_cd = (b // gcd_cd) + (-a // gcd_cd) + 1
    
    print(int(total - total_c - total_d + total_cd))

if __name__ == "__main__":
    C_AntiDivision()

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

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

def F(a, c, d):
    x = a - a//c - a//d + a//lcm(c, d)
    return x

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

Solution (Python):
from fractions import gcd


def solve(string):
    a, b, c, d = map(int, string.split())
    _gcd = gcd(c, d)
    lcm = (c // _gcd) * (d // _gcd) * _gcd
    return str(b - a + 1 - (b // c - (a - 1) // c + b // d - (a - 1) // d - b // lcm +
               (a - 1) // lcm))


if __name__ == '__main__':
    print(solve(input()))

Solution (Python):
from fractions import gcd
A, B, C, D = map(int, input().split())
if(A % C == 0):
    c_div = (B // C) - (A // C) + 1
else:
    c_div = (B // C) - (A // C + 1) + 1
if(A % D == 0):
    d_div = (B // D) - (A // D) + 1
else:
    d_div = (B // D) - (A // D + 1) + 1
cd_lcm = (C * D) // gcd(C, D)
if(A % cd_lcm == 0):
    cd_div = (B // cd_lcm) - (A // cd_lcm) + 1
else:
    cd_div = (B // cd_lcm) - (A // cd_lcm + 1) + 1
print((B - A) - (c_div + d_div - cd_div) + 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 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
def lcm(x, y):
    return (x * y) // 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):
import fractions 


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


def func1(n):
    return n - n//C - n//D + n//(C*D//fractions.gcd(C, D))


print(func1(B) - func1(A-1))

Solution (Python):
A,B,C,D = map(int,input().split())
pBC = B//C
pAC = (A-1)//C
pBD = B//D
pAD = (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)
CDlcm = lcm(C,D)
pBCD = B//CDlcm
pACD = (A-1)//CDlcm
ans =(B - A + 1) - ((pBC - pAC) + (pBD - pAD) - (pBCD - pACD))
print(ans)

Solution (Python):
import fractions

def x(k, c, d):
    rl1 = k
    lcm = c*d // fractions.gcd(c,d)
    ccount = rl1 // c
    dcount = rl1 // d
    cdcount = rl1 // lcm
    
    
    
    

    return rl1 - ccount - dcount + cdcount

a, b, C, D = map(int, input().split())
print(x(b, C, D) - x(a-1, C, D))