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

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)

def main():
    a, b, c, d = [int(x) for x in sys.stdin.readline().split()]
    divisors = 0
    divisors += b // c - (a - 1) // c
    divisors += b // d - (a - 1) // d
    l = lcm(c, d)
    divisors -= b // l - (a - 1) // l
    ans = b - (a - 1) - divisors
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):

import sys

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)

def main():
    a, b, c, d = [int(x) for x in sys.stdin.readline().split()]
    res1 = b - (b // c + b // d - b // lcm(c, d))
    res2 = (a - 1) - ((a - 1) // c + (a - 1) // d - (a - 1) // lcm(c, d))
    ans = res1 - res2
    print(ans) 

if __name__ == "__main__":
    main()

Solution (Python):
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a


a, b, c, d = map(int, input().split())
t0 = b//c - a//c + (a%c==0)
t1 = b//d - a//d + (a%d==0)
z = c*d//xgcd(c,d)
t2 = b//z - a//z + (a%z==0)

print(b-a+1-t0-t1+t2)

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

c_cnt = b//c - (a-1)//c
d_cnt = b//d - (a-1)//d
cd = (c * d) // fractions.gcd(c, d)
cd_cnt= b//cd - (a-1)//cd

ans = b-(a-1) - (c_cnt + d_cnt - cd_cnt)
print(ans)

Solution (Python):
import fractions
a,b,c,d=map(int,input().split())
CD=fractions.gcd(c,d)
cd=(c*d)//CD
width=b-a
cnt=0
if width%c==0:
  if a%c==0:
    cnt+=(width//c)+1
  else:
    cnt+=(width//c)
else:
  if c-(a%c)+b%c>=c:
    if a%c==0 or b%c==0:
      cnt+=(width//c)+1
    else:
      cnt+=(width//c)
  else:
    cnt+=(width//c)+1

if width%d==0:
  if a%d==0:
    cnt+=(width//d)+1
  else:
    cnt+=(width//d)
else:
  if d-(a%d)+b%d>=d:
    if a%d==0 or b%d==0:
      cnt+=(width//d)+1
    else:
      cnt+=(width//d)
  else:
    cnt+=(width//d)+1

if width%cd==0:
  if a%cd==0:
    cnt-=(width//cd)+1
  else:
    cnt-=(width//cd)
else:
  if cd-(a%cd)+b%cd>=cd:
    if a%cd==0 or b%cd==0:
      cnt-=(width//cd)+1
    else:
      cnt-=(width//cd)
  else:
    cnt-=(width//cd)+1

print(b-a+1-cnt)

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 notDiv(a, c, d):
    return a - a//c - a//d + a//lcm(c, d)

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

Solution (Python):
import fractions
A,B,C,D=map(int,input().split())
A=A-1
AS=A//(C*D//fractions.gcd(C,D))
BS=B//(C*D//fractions.gcd(C,D))
AC=A//C
AD=A//D
BC=B//C
BD=B//D
print(B-A-(BC+BD-BS)+(AC+AD-AS))

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

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

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

def gcd(x,y):
  if x==0:
    return y
  else:
    return gcd(y%x,x)
CD=C*D//gcd(C,D)

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

Solution (Python):

import sys
def input():
    return sys.stdin.readline().rstrip()

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

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

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

    dividable = (B//C + B//D - B//d) - (A-1)//C - (A-1)//D + (A-1)//d
    print(B-A+1 - dividable)




if __name__ == '__main__':
    main()

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


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

b_c_cnt = b // c
b_d_cnt = b // d
b_lcm_cnt = b // lcm

b_cnt = b - (b_c_cnt + b_d_cnt) + b_lcm_cnt

a_c_cnt = a // c
a_d_cnt = a // d
a_lcm_cnt = a // lcm

a_cnt = a - (a_c_cnt + a_d_cnt) + a_lcm_cnt

if (a % c != 0 and a % d != 0):
    print(b_cnt - a_cnt + 1)
else:
    print(b_cnt - a_cnt)

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

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

    A_num = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)
    B_num = B//C + B//D - B//lcm(C, D)
    return (B-(A-1))-(B_num - A_num)


if __name__ == '__main__':
    print(main())

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

Solution (Python):
import fractions

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

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

print(ans)

Solution (Python):
import fractions
a,b,c,d = map(int,input().split())
N = b-a+1
s = fractions.gcd(c,d)
t = c*d // s
lcm_div = b//t - (a-1)//t
divc = b//c - (a-1)//c
divd = b//d - (a-1)//d
if c%d==0 or d%c==0:
    w = min(c,d)
    print(N-(b//w-(a-1)//w))
else:
    print(N-(divc+divd-lcm_div))

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

Solution (Python):
import sys
input = sys.stdin.readline
 
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())
e = lcm(c, d)
count_c = b // c - (a - 1) // c
count_d = b // d - (a - 1) // d
count_e = b // e - (a - 1) // e
print(b - a + 1 - (count_c + count_d - count_e))

Solution (Python):
import sys
import math

input = sys.stdin.readline
 
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())
e = lcm(c, d)




count_c = b // c - (a - 1) // c
count_d = b // d - (a - 1) // d
count_e = b // e - (a - 1) // e




print(b - a + 1 - (count_c + count_d - count_e))

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

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)    
a, b, c, d = map(int, input().split())
e = lcm(c, d)




count_c = b // c - (a - 1) // c
count_d = b // d - (a - 1) // d
count_e = b // e - (a - 1) // e




print(b - a + 1 - (count_c + count_d - count_e))

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

MOD = 10 ** 9 + 7



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


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

    c_count = B // C - (A - 1) // C
    d_count = B // D - (A - 1) // D
    e_count = B // E - (A - 1) // E
    print((B - A + 1) - (c_count + d_count - e_count))


if __name__ == '__main__':
    main()

Solution (Python):
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)    
a, b, c, d = map(int, input().split())
e = lcm(c, d)




count_c = b // c - (a - 1) // c
count_d = b // d - (a - 1) // d
count_e = b // e - (a - 1) // e




print(b - a + 1 - (count_c + count_d - count_e))

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

c_num = b // c - (a-1) // c
d_num = b // d - (a-1) // d


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

e_num = b // e - (a-1) // e

print((b-a+1)-(c_num+d_num-e_num))

Solution (Python):
import fractions as fc

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

tmp = (c * d) // fc.gcd(c, d)

all_num = b//c + b//d - b//tmp
a_num = (a-1)//c + (a-1)//d - (a-1)//tmp

ans = (b-a+1) - (all_num - a_num)
print(ans)

Solution (Python):
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think(4, 9, 2, 3), 2)

    def test_2(self):
        self.assertEqual(think(10, 40, 6, 8), 23)

    def test_3(self):
        self.assertEqual(think(314159265358979323, 846264338327950288, 419716939, 937510582), 532105071133627368)

    def test_4(self):
        self.assertEqual(count_multiple_between(4, 9, 2), 3)

    def test_5(self):
        self.assertEqual(count_multiple_between(4, 9, 3), 2)

    def test_6(self):
        self.assertEqual(count_multiple_between(4, 9, 6), 1)

    def test_7(self):
        self.assertEqual(count_multiple_between(10, 40, 6), 5)

    def test_8(self):
        self.assertEqual(count_multiple_between(10, 40, 8), 4)

    def test_9(self):
        self.assertEqual(count_multiple_between(10, 40, 24), 1)


def solve():
    a, b, c, d = read()
    result = think(a, b, c, d)
    write(result)


def read():
    return read_int(4)


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a, b, c, d):
    if c > d:
        return think(a, b, d, c)

    if c == 1:
        return 0

    if d % c == 0:
        num_of_multiple = count_multiple_between(a, b, c)
        return b - a + 1 - num_of_multiple

    lcm = least_common_multiple(c, d)

    num_of_multiple_c = count_multiple_between(a, b, c)
    num_of_multiple_d = count_multiple_between(a, b, d)
    num_of_multiple_lcm = count_multiple_between(a, b, lcm)

    return b - a + 1 - (num_of_multiple_c + num_of_multiple_d - num_of_multiple_lcm)


def count_multiple_between(a, b, c):
    minimum_multiple = a if a % c == 0 else a + (c - (a % c))
    maximum_mulptile = b if b % c == 0 else b - (b % c)

    if maximum_mulptile < minimum_multiple:
        return 0

    return (maximum_mulptile - minimum_multiple) // c + 1


def least_common_multiple(a, b):
    return (a * b) // greatest_common_divisor(a, b)


def greatest_common_divisor(a, b):
    if a < b:
        return greatest_common_divisor(b, a)

    if a % b == 0:
        return b
    return greatest_common_divisor(b, a % b)


def write(result):
    print(result)


if __name__ == '__main__':
    
    solve()

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

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

can_c_under_a = (a-1)//c
can_d_under_a = (a-1)//d
can_cd_under_a = (a-1)//lcm(c, d)
cant_under_a = (a-1) + can_cd_under_a - can_c_under_a - can_d_under_a

can_c_all = b//c
can_d_all = b//d
can_cd_all = b//lcm(c, d)
cant_all = b + can_cd_all - can_c_all - can_d_all

print(cant_all - cant_under_a)

Solution (Python):
from fractions import gcd
def f(c):
  if a%c!=0:
    if B>=(c-a%c)+a:
      l=(c-a%c)+a
    else:
      l=B+c
  else:
    l=a
  return(1+(B-l)//c)
  
  

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

Solution (Python):

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

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

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

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

def l(a, x):
    if a // x == 0:
        return 1
    if a % x != 0:
        t = a // x + 1
        return t
    t = a // x
    return t

cl = l(A, C)
dl = l(A, D)
cdl = l(A, CD)
cr = B // C
dr = B // D
cdr = B // (CD)

ans = B - A + 1 - (cr - cl + 1) - (dr - dl + 1) + (cdr - cdl + 1)
        
print(ans)

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

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

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

ans = 0
tmp0 = b // c
tmp1 = b // d
tmp2 = b // lcm(c,d)
Tmp0 = tmp1 +tmp0 -tmp2

tmp3 = a // c
tmp4 = a // d
tmp5 = a // lcm(c,d)
Tmp1 = tmp3 + tmp4 - tmp5

ans = b-a - (Tmp0 - Tmp1)

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    a,b,c,d=map(int,input().split())
    from fractions import gcd
    L=c*d//gcd(c,d)
    M=b-(b//c)-(b//d)+(b//L)
    m=a-1-((a-1)//c)-((a-1)//d)+((a-1)//L)
    print(M-m)
resolve()

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

first_c = ( A - 1) // C
first_d = ( A - 1) // D
first_cd = ( A - 1) // lcm 
first = ( A - 1 ) - ( first_c + first_d - first_cd )

end_c = B // C
end_d = B // D
end_cd = B // lcm 
end_ = B - ( end_c + end_d - end_cd )

print(end_ - first)

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

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

lcmv = lcm(C, D)
bc = B // C
bd = B // D
bl = B // lcmv
ac = (A-1) // C
ad = (A-1) // D
al = (A-1) // lcmv
print(B - A + 1 - ((bc + bd - bl) - (ac + ad - al)))

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

from fractions import gcd

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

ans = div(c)+div(d)-div(int(c*d//gcd(c,d)))
print(ans)

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

large = np.max([C, D])
small = np.min([C, D])

g = large
while small != 0:
    large, small = small, large % small

g = large

l = C * D // g

num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // l
num_B = B - (num_B_1 + num_B_2 - num_B_3)

num_A_1 = (A - 1) // C
num_A_2 = (A - 1) // D
num_A_3 = (A - 1) // l
num_A = (A - 1) - (num_A_1 + num_A_2 - num_A_3)

print(num_B - num_A)

Solution (Python):
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])




g = large
while small != 0:
    large, small = small, large % small


 
g = large

 
l = C * D // g
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // l
num_B = B - (num_B_1 + num_B_2 - num_B_3)

num_A_1 = (A - 1) // C
num_A_2 = (A - 1) // D
num_A_3 = (A - 1) // l
num_A = (A - 1) - (num_A_1 + num_A_2 - num_A_3)
 
print(num_B - num_A)

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

large = np.max([C, D])
small = np.min([C, D])

g = large
while small != 0:
    large, small = small, large % small

g = large

l = C * D // g

num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // l
num_B = B - (num_B_1 + num_B_2 - num_B_3)

num_A_1 = (A - 1) // C
num_A_2 = (A - 1) // D
num_A_3 = (A - 1) // l
num_A = (A - 1) - (num_A_1 + num_A_2 - num_A_3)

print(num_B - num_A)

Solution (Python):
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])
 
while small != 0:
    large, small = small, large % small
gcd = large

large = np.max([C, D])
small = np.min([C, D])

lcm = C * D // gcd
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm
 
num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm
 
numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3
 
print((B-A+1) - (numB - numA))

Solution (Python):
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])
 




while small != 0:
    large, small = small, large % small
gcd = large

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

large = np.max([C, D])
small = np.min([C, D])
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm(large, small, gcd)
 
num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm(large, small, gcd)
 
numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3
 
print((B-A+1) - (numB - numA))

Solution (Python):
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])
 
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
 
def lcm(a, b):
    return a * b // gcd (a, b)
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm(large, small)
 
num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm(large, small)
 
numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3
 
print((B-A+1) - (numB - numA))

Solution (Python):
import numpy as np
import math
A, B, C, D = [int(i) for i in input().split()]
 
large = np.max([C, D])
small = np.min([C, D])
 
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
 
def lcm(a, b):
    return a * b // gcd (a, b)
 
num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm(large, small)
 
num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm(large, small)
 
numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3
 
print((B-A+1) - (numB - numA))

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

large = np.max([C, D])
small = np.min([C, D])

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

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

num_B_1 = B // C
num_B_2 = B // D
num_B_3 = B // lcm(large, small)

num_A_1 = (A-1) // C
num_A_2 = (A-1) // D
num_A_3 = (A-1) // lcm(large, small)

numB = num_B_1 + num_B_2 - num_B_3
numA = num_A_1 + num_A_2 - num_A_3

print((B-A+1) - (numB - numA))

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

def multiple(X, Y, Z):
    
    if X % Z != 0:
        min_z = X // Z + 1
    else:
        min_z = X / Z
    max_z = Y // Z
    num_z = max_z - min_z + 1
    return int(num_z)

def gcd(X, Y):
    while Y!= 0:
        X, Y = Y, X % Y
    return X

if C == 1 or D == 1:
    print(0)
else:
    num_c = multiple(A, B, C)
    num_d = multiple(A, B, D)
    lcm = int((C * D) / gcd(C, D)) 
    num_lcm = multiple(A, B, lcm)
    print(B - A + 1 - num_c - num_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):
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):
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


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


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

Solution (Python):
a,b,c,d=map(int,input().split())
def gcd(x,y):
  if x%y==0:
    return y
  else:
    return gcd(y,x%y)
    
r=int((c*d)/gcd(c,d)) 
s=(b//c)-((a-1)//c)
t=(b//d)-((a-1)//d)
u=(b//r)-((a-1)//r)

print(int(b-a+1-s-t+u))

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

Solution (Python):

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

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

lcm_CD = lcm(C, D)

div_AB_C = (B // C) - ((A - 1) // C)
div_AB_D = (B // D) - ((A - 1) // D)
div_AB_CD = (B // lcm_CD) - ((A - 1) // lcm_CD)
print(B - A + 1 - (div_AB_C + div_AB_D - div_AB_CD))

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


def p_c():
    A, B, C, D = map(int, input().split())
    A -= 1
    gcd_value = gcd(max(C, D), min(C, D))
    lcm_value = C * D // gcd_value
    print(B - A - B // C + A // C - B // D + A // D + B // lcm_value - A // lcm_value)
    
p_c()

Solution (Python):
def gcd(x,y):
  if(y==0):
  	return x
  if(x<y):
    x,y=y,x
  return gcd(y,x % y)
def lllen(a,b,c):
  at=a//c
  at=at*c
  b=b//c *c
  ansc=0
  while(at<a):
    at+=c
  ansc=b-at
  if ansc<0 :
  	ansc=0
  else:
  	ansc=ansc//c + 1
  return ansc

a,b,c,d=map(int , input().strip().split() )
tl=b-a+1

ansc=lllen(a,b,c)
ansd=lllen(a,b,d)
ansl=lllen(a,b,(c*d)//gcd(c,d))

print(b-a+1 -ansc -ansd + ansl)

Solution (Python):
import fractions

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

divc = b // c - (a-1) // c
divd = b // d - (a-1) // d

lcmcd = c * d // fractions.gcd(c,d)
divcd = b // lcmcd - (a-1) // lcmcd

print(b - a + 1 - divc - divd + divcd)

Solution (Python):
def gcd(a, b):
  if a%b == 0:
    return b
  else:
    return gcd(b, a%b)
a, b, c, d = map(int, input().split())
n = b - a + 1
mod_c = (n + a%c - 1)//c + 1 if a%c == 0 else (n + a%c - 1)//c
mod_d = (n + a%d - 1)//d + 1 if a%d == 0 else (n + a%d - 1)//d
mod_cd = (n + a%(int(c*d/gcd(c, d))) - 1)//(int(c*d/gcd(c,d))) + 1 if a%(c*d/gcd(c,d)) == 0 else (n + a%(int(c*d/gcd(c,d))) - 1)//(int(c*d/gcd(c,d)))
ans = n - mod_c - mod_d + mod_cd
print(int(ans))

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

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

mul = cnt_c + cnt_d - cnt_cd

print(b - a + 1 - mul)

Solution (Python):
from fractions import gcd

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

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

num1 = B // C - (A - 1) // C
num2 = B // D - (A - 1) // D
num3 = B // E - (A - 1) // E
print(B - A + 1 - num1 - num2 + num3)

Solution (Python):
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    return gcd(b, a % b)
def resolve():
    a, b, c, d = list(map(int, input().split()))
    lcm = int(c * d / gcd(c, d))
    c_b = b - (b//c + b//d - b//(lcm))
    c_a = (a-1) - ((a-1)//c + (a-1)//d - (a-1)//(lcm))
    print(c_b - c_a)
    
resolve()

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)

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

ub = B - B // C - B // D + B // lcm(C, D)

A -= 1
ua = A - A // C - A // D + A // lcm(C, D)

print(ub - ua)

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

Solution (Python):
import fractions
A,B,C,D=map(int,input().split())
A-=1
f=fractions.gcd(C,D)
f2=C*D//f
ans = B-A-(B//C-A//C+B//D-A//D-B//f2+A//f2)
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(a, b):
    return a * b // gcd (a, b)

C_ok = (B // C) - ((A-1) // C) 
D_ok = (B // D) - ((A-1) // D)
C_D_ok = (B // lcm(C, D)) - ((A-1) // lcm(C, D))
print((B-A+1)-C_ok-D_ok+C_D_ok)

Solution (Python):
from fractions import gcd

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

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

div_A_C = (A-1)//C
div_A_D = (A-1)//D

div_B_C = B//C
div_B_D = B//D

CD = lcm(C, D)

div_A_CD = (A-1)//CD
div_B_CD = B // CD
C_or_D = (div_B_C+div_B_D-div_B_CD)-(div_A_C+div_A_D-div_A_CD) 
print(B-A+1-C_or_D)

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

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

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

x1=(b//z)
x2=(b//c)
x3=(b//d)
x=x2+x3-x1
y1=(a-1)//z
y2=(a-1)//c
y3=(a-1)//d
y=y2+y3-y1

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

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

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

Solution (Python):
import fractions

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


def main():
    a,b,c,d = map(int,input().split())
    c_mul_count = b//c - (a-1)//c
    d_mul_count = b//d - (a-1)//d
    cd_lcm = lcm(c, d)
    lcm_mul_count = b//cd_lcm - (a-1)//cd_lcm
    s = b-a+1
    print(s-c_mul_count-d_mul_count+lcm_mul_count)



if __name__ == '__main__':
    main()

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

for i in D:
  f.append((b//i)-((a-1)//i))
print(b-a+1-f[0]-f[1]+f[2])

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

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

import fractions

g = fractions.gcd(C, D)
E = C*D//g
num = (B//C-(A-1)//C) + (B//D-(A-1)//D) - (B//E-(A-1)//E)
print(B-A+1-num)

Solution (Python):
A,B,C,D = map(int,input().split())
def gcd(x,y):
  if x<y:
    l = y
    k = x
    x = y
    y = k
  else:
    k = y
    l = x
  z = x%y
  while z != 0:
    x = y
    y = z
    z = x%y
  return (k//y)*(l//y)*y

if A%C ==0:
  i = (A//C)-1
else:
  i = A//C

j = B//C

if A%D ==0:
  k = (A//D)-1
else:
  k = A//D

l = B//D

if A%(gcd(C,D)) ==0:
  m = (A//(gcd(C,D)))-1
else:
  m = A//gcd(C,D)
  
n = B//gcd(C,D)



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

Solution (Python):
def gcd(a,b):
    if(a<b):
        tmp=b
        b=a
        a=tmp
    while(a%b!=0):
        tmp_1=b
        tmp_2=a%b
        a=tmp_1
        b=tmp_2
    return b
    
def lcm(x,y):
    return int(x*y/gcd(x,y))

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

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

if(A%D==0):
    n_D=B//D-A//D+1
else:
    n_D=B//D-A//D

if(A%lcm(C,D)==0):
    n_CD=B//lcm(C,D)-A//lcm(C,D)+1
else:
    n_CD=B//lcm(C,D)-A//lcm(C,D)
    
print(S-n_C-n_D+n_CD)

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

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

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

def candiv(l,u,x):
	return u // x - (l - 1) // x

a,b,c,d = map(int,input().split())
print(b - a + 1 - candiv(a,b,c) - candiv(a,b,d) + candiv(a,b,lcm(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())
l = lcm(c,d)
 
def xika(x,v1,v2,vl):
  c1 = x//v1
  c2=x//v2
  c3 = x//vl
  return c1+c2-c3

print((b-a+1)-(xika(b,c,d,l)-xika(a-1,c,d,l)))

Solution (Python):
from fractions import gcd
a,b,c,d = (int(x) for x in input().split())
val = gcd(c,d)
lcm = (c*d) // val
div_by_c = (b // c) - (a // c) + (1 if a % c == 0 else 0)
div_by_d = (b // d) - (a // d) + (1 if a % d == 0 else 0)
div_by_lcm = (b // lcm) - (a // lcm) + (1 if a % lcm == 0 else 0)

print(b-a+1 - div_by_c - div_by_d + div_by_lcm)

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


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

def lcm(m, n):
	if ( ( 0 == m ) or ( 0 == n ) ):
		return 0;
	return ((m // gcd(m, n)) * n); 


ans = (B-A+1)
ans = ans - (B//C - (A-1)//C)
ans = ans - (B//D - (A-1)//D)
lcmnum = lcm(C, D)
ans = ans + (B//lcmnum - (A-1)//lcmnum)
print(ans)

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

Solution (Python):
import fractions
a, b, c, d = map(int, input().split())
cr = b // c - (a - 1) // c
dr = b // d - (a - 1) // d

cdr = b // ((c * d) // fractions.gcd(c, d)) - (a - 1) // ((c * d) // fractions.gcd(c, d))
print(b - a + 1 - cr - dr + cdr)

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


def euclid(C,D):
    return C if D==0 else euclid(D,C%D)

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

ac=A//C
ad=A//D
acd=A//E
bc=B//C
bd=B//D
bcd=B//E

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

if A%C==0 or A%D==0:
    ans-=1
print(ans)

Solution (Python):


import fractions

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

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

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


LCM=lcm(C,D)

Ac=(A-1)//C

div_C=B//C-Ac

Ad=(A-1)//D

div_D=B//D-Ad

Alcm=(A-1)//LCM

div_CD=B//LCM-Alcm

Ans=(B-A+1)-div_C-div_D+div_CD
print(Ans)

Solution (Python):



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


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

total = b-a+1
sub_c = total//c
if total % c != 0 and (a % c > b % c or (a % c == 0 and b % c == 0)):
    sub_c += 1

sub_d = total//d
if total % d != 0 and (a % d > b % d or (a % d == 0 and b % d == 0)):
    sub_d += 1

cd = c*d//gcd(c, d)
sub_cd = total//cd
if total % cd != 0 and (a % cd > b % cd or (a % cd == 0 and b % cd == 0)):
    sub_cd += 1

ans = total - sub_c - sub_d + sub_cd
print(ans)

Solution (Python):
import fractions


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

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

def get_num(x):
    ans = x - x // c - x // d + x // lcm(c, d)
    return ans


print(get_num(b) - get_num(a -1))

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

def f(x):
    ans = x
    ans -= x // C
    ans -= x // D
    ans += x // (C*D//gcd(C, D))
    return ans

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

Solution (Python):
import fractions

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

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


if a == 0:
    print(b - (b // c + b // d - b // lcm(c,d)))
else:
    c_wari = b // c - (a-1) // c
    
    d_wari = b // d - (a-1) // d
    
    cd_wari = b // lcm(c,d) - (a-1) // lcm(c,d)
    
    print((b-a+1) - (c_wari+d_wari-cd_wari))

Solution (Python):
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):
    x = a * b // gcd(a, b)
    return x


a, b, c, d = map(int, input().split())
lcm_cd = lcm(c, d)
a -= 1

x = b - ((b // c + b // d) - (b // lcm_cd))
y = a - ((a // c + a // d) - (a // lcm_cd))
print(x - y)

Solution (Python):
import fractions

def solve():
    A, B, C, D = map(int, input().split())
    gcd = (C * D) // fractions.gcd(C, D)
    ans_sub = A-1 - ((A-1) // C + (A-1) // D - (A-1) // gcd)
    ans = B - (B // C + B // D - B // gcd) - ans_sub
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
def gcd(m,n):
    if m < n:
        m,n=n,m
    if n==0:
        return m
    if m % n == 0:
        return n
    else:
        return gcd(n,m%n)

A,B,C,D=map(int,input().split())
X=B-(B//C)-(B//D)+ B//(C*D//gcd(C,D))
Y=(A-1)-((A-1)//C)-((A-1)//D)+(A-1)//(C*D//gcd(C,D))
print(X-Y)