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 math





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





ac = (A-1)//C
bc = B//C
ad = (A-1)//D
bd = B//D
a_both = (A-1)//(C*D//math.gcd(C,D))
b_both = B//(C*D//math.gcd(C,D))
print((B-A+1) - (bc-ac) - (bd-ad) + (b_both-a_both))

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

import math
LCM=c * d //math.gcd(c, d)

tmp1=b//c + b//d - b//LCM 
tmp2=(a-1)//c + (a-1)//d - (a-1)//LCM 
tmp=tmp1-tmp2 
ans=(b-a+1)-tmp

print(ans)

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

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

x = b - a + 1
cx = b // c - (a-1) // c
dx = b // d - (a-1) // d
cdx = b // (c*d // gcd(c, d)) - (a-1) // (c*d // gcd(c, d))

print(x - cx - dx + cdx)

Solution (Python):

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


def dev(n):
    return b // n - -(-a // n) + 1

print(dev(1) - dev(c) - dev(d) + dev(c * d//gcd(c,d)))

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

c = (B//C) - ((A+C-1)//C) + 1
d = (B//D) - ((A+D-1)//D) + 1
lcm = int(C * D / math.gcd(C, D))
m = (B//lcm) - ((A+lcm-1)//lcm) + 1
ans = (B-A+1) - (c+d-m)
print(int(ans))

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

t = b-a+1
b1 = b//c
b2 = b//d
if a%c==0:
  a1 = a//c - 1
else:
  a1 = a//c
if a%d==0:
  a2 = a//d - 1
else:
  a2 = a//d

lcm = c*d//gcd(c,d)
e1 = b//lcm
if a%(lcm)==0:
  e2 = a//(lcm) - 1
else:
  e2 = a//(lcm)
  
print(t-((b1-a1)+(b2-a2)-(e1-e2)))

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

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

def main():
    a, b, c, d = map(int, input().split())
    a_cnt, b_cnt = 0, 0
    a -= 1
    a_cnt = a - ((a//c + a//d) - a // lcm(c, d))
    b_cnt = b - ((b//c + b//d) - b // lcm(c, d))
    print(b_cnt - a_cnt)
    
if __name__ == '__main__':
    main()

Solution (Python):
import math

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

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

cntA = (A - 1) // C + (A - 1) // D - (A - 1) // lcm
cntB = B // C + B // D - B // lcm
ans = B - A + 1 - (cntB - cntA)
print(ans)

Solution (Python):
def resolve():
    import math
    A, B, C, D = [int(i) for i in input().split()]
    A -= 1
    LCMCD = C * D // math.gcd(C, D)
    ans = (B - B // C - B // D + B // LCMCD) - \
          (A - A // C - A // D + A // LCMCD)
    print(ans)


resolve()

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

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


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

lcm_cd = lcm(c, d)

under_b = b - b//c - b//d + b//lcm_cd
over_a = (a-1) - (a-1)//c - (a-1)//d + (a-1)//lcm_cd

print(under_b - over_a)

Solution (Python):
import math

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

b = B - ((B // C) + (B // D) - (B // (C*D // math.gcd(C,D)))) 
a = A-1 - (((A-1) // C) + ((A-1) // D) - ((A-1) // (C*D // math.gcd(C,D))))
res = b - a
print(res)

Solution (Python):
import math

import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    a,b,c,d = map(int,ipt().split())
    ans = b-a+1
    ans -= b//c-(a-1)//c
    ans -= b//d-(a-1)//d
    g = c*d//math.gcd(c,d)
    ans += b//g-(a-1)//g
    print(ans)
    return

if __name__ == '__main__':
    main()

Solution (Python):
import math

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

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

g = math.gcd(c, d)
l = lcm(c, d)

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

Solution (Python):
from math import floor, ceil, gcd

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

if a%c == 0: c_m_min = a
else: c_m_min = a + c - a%c
c_m_max = b - b%c
c_cnt = (c_m_max - c_m_min)//c + 1
if c_m_min > b or c_m_max < a : c_cnt = 0

if a%d == 0: d_m_min = a
else: d_m_min = a + d - a%d
d_m_max = b - b%d
d_cnt = (d_m_max - d_m_min)//d + 1
if d_m_min > b or d_m_max < a : d_cnt = 0

cd = c*d//gcd(c,d)
if a%cd == 0: cd_m_min = a
else: cd_m_min = a + cd - a%cd
cd_m_max = b - b%cd
cd_cnt = (cd_m_max - cd_m_min)//cd + 1
if cd_m_min > b or cd_m_max < a : cd_cnt = 0


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

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

s = gcd(c, d)
p = c * d // s
divide_c = b // c - (a - 1) // c
divide_d = b // d - (a - 1) // d
divide_cd = b // p - (a - 1) // p
print(int(b - a + 1 - divide_c - divide_d + divide_cd))

Solution (Python):
from fractions import gcd
from functools import reduce
from collections import deque

import itertools
import collections
import math
import sys
sys.setrecursionlimit(2000000)









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










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



cb = B//C
db = B//D
cdb = B//(lcm(C, D))
total_b = cb+db-cdb


A -= 1
ca = A//C
da = A//D
cda = A//(lcm(C, D))
total_a = ca+da-cda




t = B-A
s = total_b-total_a
print(t-s)

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

def lcm(x, y):
    return (x * y) // math.gcd(x, y)
a-=1
e=lcm(c,d)

ansb=0
ansb += b//c
ansb += b//d
ansb -= b//e



ansa=0
ansa += a//c
ansa += a//d
ansa -= a//e
print((b-a)-(ansb-ansa))

Solution (Python):
a, b, c, d = map(int, input().split())
import math
c_div = 0
d_div = 0
if a % c == 0:
    c_div = max(0, 1 + ((b - (b % c)) - a) // c )
else:
    c_div = max(0, 1 + ((b - (b % c)) - (a + (c - a % c))) // c )
if a % d == 0:
    d_div = max(0, 1 + ((b - (b % d)) - a) // d )
else:
    d_div = max(0, 1 + ((b - (b % d)) - (a + (d - a % d))) // d )
gcd_input = c * d // math.gcd(c, d)
if a % gcd_input == 0:
    c_d_div = max(0, 1 + ((b - (b % gcd_input)) - a) // gcd_input )
else: 
    c_d_div = max(0, 1 + ((b - (b % gcd_input)) - (a + (gcd_input - a % gcd_input))) // gcd_input )
print(b - a + 1 - d_div - c_div + c_d_div)

Solution (Python):
from math import gcd

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

Solution (Python):
from math import gcd
def main():
    a, b, c, d = map(int, input().split())
    ans = b - a + 1
    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(ans - x - y + z)

if __name__ == "__main__":
    main()

Solution (Python):
import math
A,B,C,D = (int(x) for x in input().split())
Num = B-A+1
Dup = C*D//math.gcd(C,D)
MinC  = (A+(C-A%C)%C)//C
MinD  = (A+(D-A%D)%D)//D
MinCD = (A+(Dup-A%Dup)%Dup)//Dup
OvrC  = (B+(C-B%C)%C)//C + (B%C==0)
OvrD  = (B+(D-B%D)%D)//D + (B%D==0)
OvrCD = (B+(Dup-B%Dup)%Dup)//Dup + (B%Dup==0)
CDNum = (OvrC-MinC)+(OvrD-MinD)-(OvrCD-MinCD)
print(Num-CDNum)

Solution (Python):
import math
a, b, c, d = map(int, input().split())
e = c*d//math.gcd(c,d)
def ko(a,b,c):
  x = b//c-a//c
  if a%c==0:
    x+=1
  return(x)
print(b-a+1-ko(a,b,c)-ko(a,b,d)+ko(a,b,e))

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

import math
p=math.gcd(c,d)
q=(c*d)//p

ncd=(b//q)-((a-1)//q)
r=nc+nd-ncd
ans=(b-a+1)-r
print(ans)

Solution (Python):
import sys
from io import StringIO
import unittest
import os
import math


sys.setrecursionlimit(999999999)


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



def resolve(test_def_name=""):
    a, b, c, d = map(int, input().split())

    ans = b - a + 1

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

    print(ans + all_cnt - out_cnt)



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """4 9 2 3"""
        output = """2"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """10 40 6 8"""
        output = """23"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """314159265358979323 846264338327950288 419716939 937510582"""
        output = """532105071133627368"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

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

A,B,C,D = list(map(int,input().split()))
if A%C == 0:
    num_C_bai = (B-(B%C) - A)//C + 1
else:
    num_C_bai = ((B-(B%C) - (A-(A%C)+C)))//C + 1
    
if A%D == 0:
    num_D_bai = (B-(B%D) - A)//D + 1
else:
    num_D_bai = ((B-(B%D) - (A-(A%D)+D)))//D + 1

E = lcm(C,D)

if A%E == 0:
    num_E_bai = (B-(B%E) - A)//E + 1
else:
    num_E_bai = ((B-(B%E) - (A-(A%E)+E)))//E + 1

print(B + 1 - A - num_C_bai - num_D_bai + num_E_bai)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
import sys
def li(): return map(int,sys.stdin.readline().rstrip().split())
def lf(): return map(float,sys.stdin.readline().rstrip().split())
def ls(): return sys.stdin.readline().rstrip().split()

a, b, c, d = li()
cn = b//c - (a-1)//c
dn = b//d - (a-1)//d
lcm = c*d//math.gcd(c,d)
cdn = b//lcm - (a-1)//lcm
print(b-a+1-cn-dn+cdn)

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

Solution (Python):
from fractions import gcd

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

Solution (Python):
import math
a,b,c,d=map(int,input().split())
lcm=c*d//math.gcd(c,d)
a1,a2,a3=(a-1)//c,(a-1)//d,(a-1)//lcm
b1,b2,b3=b//c,b//d,b//lcm
ans=b-a+1-(b1+b2-b3-a1-a2+a3)
print(ans)

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

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

x = [(A-1)//C,(A-1)//D,(A-1)//lcm(C,D)]
y = [B // C,B//D,B//lcm(C,D)]

an = (A-1) - (x[0]+x[1]-x[2])
bn =  B - (y[0]+y[1]-y[2])

print(bn-an)

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

def gcd(a,b):
    x=max(a,b)
    y=min(a,b)
    
    while True:

        y_ = x % y
        if y_ == 0:
            return y
        x = y
        y = y_

def lcm(a,b):
    ans=Decimal(a*b)/Decimal(gcd(a,b))
    return ans.as_integer_ratio()[0]

CD=lcm(C,D)
len_all = B-A+1
len_c = B//C-(A-1)//C
len_d = B//D-(A-1)//D
len_c_and_d = B//CD-(A-1)//CD

ans=len_all-len_c-len_d+len_c_and_d 
print(int(ans))

Solution (Python):
import sys

def S(): return sys.stdin.readline().rstrip()

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

from math import gcd

def f(n):  
    return n-n//C-n//D+n//(C*D//gcd(C,D))

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

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

print(int(x-y))

Solution (Python):
from math import gcd


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

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


x = [(A - 1) // C, (A - 1) // D, (A - 1) // lcm(C, D)]

y = [B // C, B // D, B // lcm(C, D)]


an = (A - 1) - (x[0] + x[1] - x[2])


bn = B - (y[0] + y[1] - y[2])

print(bn - an)

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

c1 = (B//C)-((A+C-1)//C)+1
c2 = (B//D)-((A+D-1)//D)+1
c3 = (B//E)-((A+E-1)//E)+1
ans = B-A+1-c1-c2+c3

print(ans)

Solution (Python):
import math
a,b,c,d=map(int, input().split())
def gcd(a,b):
    if(a%b!=0):
        return gcd(b,a%b)
    else:
        return b
def lcm(a,b):
    return a*b//gcd(a,b)

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

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

A, B, C, D = map(int,input().split())
As = [(A-1)//C, (A-1)//D, (A-1)//lcm(C,D)]
Bs = [B//C, B//D, B//lcm(C,D)]

aa = (A-1) - (As[0]+As[1]-As[2])
bb = B - (Bs[0]+Bs[1]-Bs[2])

print(bb-aa)

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

from math import gcd

def calc(a, b, n):
    return b//n - (a//n+bool(a%n))

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

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

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

nC = B//C - -(-A//C) + 1
nD = B//D - -(-A//D) + 1
nCD = B//lcm(C,D) - -(-A//lcm(C,D)) + 1

print((B-A+1)-(nC+nD-nCD))

Solution (Python):
import math

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

def f(n, c, d):
    sum = n
    sum -= n // c
    sum -= n // d
    lcm = c * d // math.gcd(c, d)
    sum += n // lcm
    return sum

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

Solution (Python):
import math


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

if C == 1 or D == 1:
    print(0)
    exit()

count_all = B - A + 1
count_c = (B // C) - math.ceil(A / C) + 1
count_d = (B // D) - math.ceil(A / D) + 1
lcm_cd = C * D // math.gcd(C, D)
count_cd = (B // lcm_cd) - math.ceil(A / lcm_cd) + 1

ans = count_all - (count_c + count_d - count_cd)
print(ans)

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

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

a,b,c,d = map(int,input().split())
total =b-a+1
cnum= b//c-(a-1)//c
dnum =b//d-(a-1)//d
m = lcm(c,d)
mnum = b//m -(a-1)//m

print(total-(cnum+dnum-mnum))

Solution (Python):
import math

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

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

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

print(calc(B)-calc(A-1))

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

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

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

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

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

print(y-x)

Solution (Python):

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

e = int(c*d/math.gcd(c,d))

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


ans -= b//e-(a-1)//e
print(int(b-a+1-ans))

Solution (Python):
import math

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


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

CL=(A-1)//C
CH=B//C
DL=(A-1)//D
DH=B//D
ACD=(A-1)//lcm(C,D)
BCD=B//lcm(C,D)



ans=(B-A+1)-((CH-CL)+(DH-DL)-(BCD-ACD))
print(ans)

Solution (Python):

import fractions
from functools import reduce

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

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

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

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

A,B,C,D=map(int,input().split())
BA=B-A
CD=lcm(C,D)
AC=A%C
AD=A%D
ACD=A%CD
ans=BA+1

ans-=max((BA-(C-AC)%C)//C,0)
ans-=max((BA-(D-AD)%D)//D,0)
ans+=max((BA-(CD-ACD)%CD)//CD,0)


if (BA-(C-AC)%C)>=0:
    ans-=1

if (BA-(D-AD)%D)>=0:
    ans-=1

if (BA-(CD-ACD)%CD)>=0:
    ans+=1

print(ans)

Solution (Python):
from math import gcd

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

Solution (Python):
a, b, c, d = map(int, input().split())
import numpy as np
cmin = (a - 1) // c + 1
cmax = b // c
dmin = (a - 1) // d + 1
dmax = b // d
lcmcd = np.lcm(c, d)
cdmin = (a - 1) // lcmcd + 1
cdmax = b // lcmcd
csum = cmax - cmin + 1
dsum = dmax - dmin + 1
cdsum = cdmax - cdmin + 1
print((b - a + 1) - (csum + dsum - cdsum))

Solution (Python):
from math import *
a,b,c,d = map(int,input().split())
f =(c*d)//gcd(c,d)
r = f
rock = 2
compteur= (b//f)-(a//f)
if a%f==0:
  compteur +=1
div1 = ((b//c)-(a//c))
div2 = ((b//d)-(a//d))
if  a%c == 0:
  div1+=1
if a%d ==0:
  div2 +=1
roger =(div1+div2)-compteur
mm = (b-a)+1
print(mm-roger)

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

import math

bc = b//c
bd = b//d
bcd = b//(c*d//math.gcd(c,d))

ac = (a-1)//c
ad = (a-1)//d
acd = (a-1)//(c*d//math.gcd(c,d))

ans = (b-bc-bd+bcd)-((a-1)-ac-ad+acd)

print(ans)

Solution (Python):
import math

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

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

x = b//c-a//c 

x += b//d-a//d

x -= b//e-a//e 

if a%c == 0:
    x += 1
    
if a%d == 0:
    x += 1
    
if a%e == 0:
    x -= 1
    
print(b-a-x+1)

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

from math import*
upto_B=B-(B//C+B//D-B//(C*D//gcd(C,D)))
upto_A=A-1-((A-1)//C+(A-1)//D-(A-1)//(C*D//gcd(C,D)))

print(upto_B-upto_A*(A>2))

Solution (Python):
import math

def lcm(c,d): return c*d // math.gcd(c,d)

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

q11 = (a-1)//c
q12 = (a-1)//d

q21 = b//c
q22 = b//d

q3 = (a-1)//lcm(c,d)
q4 = b//lcm(c,d)
print(b-(a-1) - (q21-q11) - (q22-q12) + (q4-q3))

Solution (Python):
a,b,c,d=map(int,input().split())
def lcm(a,b):
    ori_a=a
    ori_b=b
    while b!=0:
        a,b=b,a%b
    return (ori_a*ori_b)//a

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


b_div=b//lcm(c,d)
bc=b//c
bd=b//d
a=a-1
a_div=a//lcm(c,d)

ac=a//c
ad=a//d
print((b-bc-bd+b_div)-(a-ac-ad+a_div))

Solution (Python):
import math

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

a -= 1

x = a // c
y = a // d
z = a // lcm

ans1 = a - (x + y - z)

x = b // c
y = b // d
z = b // lcm

ans2 = b - (x + y - z)

print(ans2 - ans1)

Solution (Python):
import math


A, B, C, D = [int(_) for _ in input().split()]
X = C * D // math.gcd(C, D)



def f(n):
    c = n // C
    d = n // D
    x = n // X
    res = n - c - d + x
    
    return res


ans = f(B) - f(A) + 1
if A % C == 0 or A % D == 0:
    ans -= 1

print(ans)

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

s1 = (a-1) // c
s2 = (a-1) // d
s3 = (a-1) // (c*d//math.gcd(c,d))
S = (a-1) - (s1 + s2 -s3)

l1 = b // c
l2 = b // d
l3 = b // (c*d//math.gcd(c,d))
L = b - (l1 + l2 - l3)

print(L-S)

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

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

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

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

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

c=((B-B%C)-(A-1+C-(A-1)%C))//C+1
d=((B-B%D)-(A-1+D-(A-1)%D))//D+1
lcm=C*D//math.gcd(C,D)
candd=((B-B%lcm)-(A-1+lcm-(A-1)%lcm))//lcm+1
print(int(B-A+1-c-d+candd))

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

def f(s, x, y):
    lcm = x * y // gcd(x, y)
    ret = s // x + s // y - s // lcm
    return ret

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

Solution (Python):
A, B, C, D = map(int, input().split())
A -= 1
import math
g = C*D//math.gcd(C, D)
a = A//C + A//D - A//g
b = B//C + B//D - B//g
ans = B - A - b + a
print(ans)

Solution (Python):
import math

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

a,b,c,d = map(int,input().split())
z = lcm(c,d)
ac = a // c *c
if a % c > 0:
    ac += c
ad = a // d * d
if a % d > 0:
    ad += d
az = a // z * z
if a % z > 0:
    az += z
bc = b // c * c
bd = b // d * d
bz = b // z * z
uc = (bc - ac) // c
ud = (bd - ad) // d
uz = (bz - az) // z
print((b-a)-(uc+ud)+uz)

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

Solution (Python):
a,b,c,d = map(int,input().split())
cc = b//c -(a-1)//c
dd = b//d -(a-1)//d

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


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

Solution (Python):
import sys
from math import gcd

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    L = C // gcd(C, D) * D
    c1, c2 = (A + C - 1) // C, B // C
    d1, d2 = (A + D - 1) // D, B // D
    l1, l2 = (A + L - 1) // L, B // L

    ans = (B - A + 1) - ((c2 - c1 + 1) + (d2 - d1 + 1) - (l2 - l1 + 1))
    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from math import gcd

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    G = C // gcd(C, D) * D
    c1, c2 = (A + C - 1) // C, B // C
    d1, d2 = (A + D - 1) // D, B // D
    g1, g2 = (A + G - 1) // G, B // G

    ans = (B - A + 1) - ((c2 - c1 + 1) + (d2 - d1 + 1) - (g2 - g1 + 1))
    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
from math import gcd
from decimal import Decimal as dec 
a, b, c, d = map(dec,input().split())
if a % c ==0:
    c_explicit = b // c - a // c + 1
else:
    c_explicit = b // c - a // c 
if a % d ==0:
    d_explicit = b // d - a // d + 1
else:
    d_explicit = b // d - a // d 
e = gcd(int(c), int(d))
e = dec(e)
gcl = (c / e) * (d / e) * e
if a % gcl ==0:
    e_explicit = b // gcl - a // gcl + 1
else:
    e_explicit = b // gcl - a // gcl 
g = b - a + 1 - c_explicit - d_explicit + e_explicit
print(g)

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

num = B - A + 1

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

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

CandD =  (C * D) // math.gcd(C, D)
CandD_div = B // CandD - (A - 1) // CandD

CorD_div = C_div + D_div - CandD_div
res = num - CorD_div
print(res)

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


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

n=(b//c)-((a-1)//c)
m=(b//d)-((a-1)//d)
o=(b//e)-((a-1)//e)



print(b-a+1-n-m+o)

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

def num_fact_in(begin, end):
    def f(div):
        return end//div - (begin-1)//div
    return f

import math
lcm = c * d // math.gcd(c, d)

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

Solution (Python):
import math

def lec(a, b):
    
    return a * b // math.gcd(a, b)

def cal_num(a, b, x):
    
    if b >= x:
        x_cum = b // x - (a-1) // x
    else:
        x_cum = 0
    return x_cum

if __name__ == "__main__":
    A, B, C, D = map(int, input().split())

    CD = lec(C, D)
    C_num = cal_num(A, B, C)
    D_num = cal_num(A, B, D)
    CD_num = cal_num(A, B, CD)
    answer = B - A + 1 - C_num - D_num + CD_num

    print(answer)

Solution (Python):

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



import math

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

cnum=b//c-(a-1)//c
dnum=b//d-(a-1)//d
cdnum=b//lcm-(a-1)//lcm

ans=b-a+1-(cnum+dnum)+cdnum
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

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


from fractions import gcd

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

rem_ans = b//c - (a-1)//c + b//d - (a-1)//d - (b//f2 - (a-1)//f2)
ans = b-a+1-rem_ans
print(ans)

Solution (Python):
def maxcount(x,y):
    return x//y
def mincount(x,y):
    if x%y==0:
        return x//y -1
    else:
        return x//y
from fractions import gcd
a,b,c,d=map(int,input().split())

tot = b-a+1

div_c = maxcount(b,c)-mincount(a,c)

div_d = maxcount(b,d)-mincount(a,d)

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


div_both = maxcount(b,lcm)-mincount(a,lcm)

ans=tot-(div_c+div_d-div_both)
print(ans)

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

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

ee=b//lcm-a//lcm
if a%c==0:
  cc+=1
if a%d==0:
  dd+=1
if a%lcm==0:
  ee+=1
print(allab-cc-dd+ee)

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

Solution (Python):
def euclid(a, b):
  if b == 0:
    return a
  else:
    return euclid(b, a%b)
a, b, c, d=map(int,input().split())
if a%c==0:
  ac = a//c
else:
  ac = (a//c)+1
bc=b//c
n=bc-ac+1
if a%d == 0:
  ad = a//d
else:
  ad = (a//d)+1
bd = b//d
m = bd-ad+1
new = (c*d)//euclid(c, d)
if a%(c*d)==0:
  cnew=a//new
else:
  cnew=(a//new)+1
dnew=(b//new)
r=dnew-cnew+1
s=n+m-r
print(b-a+1-s)

Solution (Python):
def euclid(a, b):
  if b == 0:
    return a
  else:
    return euclid(b, a%b)
A,B,C,D=map(int,input().split())
if A%C==0:
  k=A//C
else:
  k=(A//C)+1
if B%C==0:
  t=B//C
else:
  t=(B//C)
n=t-k+1
if A%D==0:
  a=A//D
else:
  a=(A//D)+1
b=B//D
m=b-a+1
mi=(C*D)//euclid(C, D)
if A%(C*D)==0:
  c=A//mi
else:
  c=(A//mi)+1
d=(B//mi)
r=d-c+1
s=n+m-r
print(B-A+1-s)

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

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


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


def solve(a, b, c, d):
    lcm_c_and_d = lcm(c, d)
    a -= 1
    p = a - a//c - a//d + a//lcm_c_and_d
    q = b - b//c - b//d + b//lcm_c_and_d

    return q-p


if __name__ == '__main__':
    a, b, c, d = map(int, input().split())

    print(solve(a, b, c, d))

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

Solution (Python):
from fractions import gcd
a,b,c,d = map(int, input().split())
def f(x):
  p = b//x
  q = a//x
  if a%x == 0:
    return(p-q+1)
  else:
    return(p-q)
  
k = f(c)+f(d)-f((c*d)//gcd(c,d))
print(b-a+1-k)

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

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

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

Solution (Python):
def gcd(x, y):
    while (y):
        x, y = y, x % y
    return x
a,b,c,d=map(int,input().split())
total=b-a+1
f1=(b//c)-(a//c)
if a%c==0:
    f1+=1
f2=(b//d)-(a//d)
if a%d==0:
    f2+=1
gcd=gcd(c,d)
lcm=(c*d)//gcd
f3=(b//lcm)-(a//lcm)
if a%lcm==0:
    f3+=1
f=f1+f2-f3
print(total-f)

Solution (Python):


from fractions import gcd


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

C_ = B // C
D_ = B // D

C_a = (A-1) // C
D_a = (A-1) // D


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

CD_ = (B // CD)
CD_a = ((A-1) // CD)

ans = B - (A-1) - (C_ - C_a) - (D_ - D_a) + (CD_ - CD_a)

print(int(ans))

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


def check(p):
    if a%p==0:
        e = a//p
    else:
        e = (a//p) +1

    if b%p==0:
        f = b//p
    else:
        f = (b//p)
        
    return(f-e+1)
        

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

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

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

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

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

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

Solution (Python):
import math
 
A, B, C, D = list(map(int,input().split()))
 
def gcd_(a, b):
    if a < b:  a, b = b, a
    if b == 0:  return a
    return gcd_(b, a % b)
 
def lcm_(x,y):
    return (x * y) // gcd_(x, y)
 
def f(x):
    if A % x:
        a = A + (x - A % x)
    else:
        a = A
    if B - a >= 0:
        temp = (B - a + 1) // x
        if (B - a + 1) % x:
            return temp + 1
        return temp
    else:
        return 0
    
ans = (B - A + 1) - f(C) - f(D) + f(lcm_(C, D))
print(ans)

Solution (Python):
import math

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

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

def lcm_(x,y):
    return (x * y) // gcd_(x, y)

def f(x, y):
    return x // y

lcm = lcm_(C, D)
ret1 = A - 1 - f(A - 1, C) - f(A - 1, D) + f(A - 1, lcm)
ret2 = B - f(B, C) - f(B, D) + f(B, lcm)
ans = ret2 - ret1
print(ans)

Solution (Python):

from fractions import gcd

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


aa = a // c
if (a % c == 0):
    aa -= 1
bb = b // c
ans = bb - aa


aa = a // d
if (a % d == 0):
    aa -= 1
bb = b // d
ans += bb - aa

'''
Python3.5
>>
最大公約数
import math
z = math.gcd(a,b)
最小公倍数
z = (a * b)//math.gcd(a,b)

Python3.4
from fractions import gcd
'''

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

aa = a // z
if (a % z == 0):
    aa -= 1
bb = b // z
ans -= bb - aa

tar = b - a + 1 - ans
print(tar)

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

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

print(B-A+1-((B//C+B//D-B//(num(C, D)))-((A-1)//C+(A-1)//D-(A-1)//(num(C, D)))))

Solution (Python):
from fractions import gcd

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


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

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

print(count)

Solution (Python):
import fractions
a,b,c,d = map(int,input().split())
AdivC = (a-1)//c
BdivC = b//c
AdivD = (a-1)//d
BdivD = b//d
gc = fractions.gcd(c,d)
div = (c*d)//gc
AdivCandD = (a-1)//div
BdivCandD = b//div

divC = BdivC - AdivC
divD = BdivD - AdivD
divCandD = BdivCandD - AdivCandD
ans = b-a+1
ans = ans -(divC+divD-divCandD)
print(ans)

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