Problem:
 Score : 300 points    Problem Statement You are given four integers A , B , C , and D . Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D .      Constraints  1\leq A\leq B\leq 10^{18}  1\leq C,D\leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  A  B  C  D       Output Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D .        Sample Input 1 4 9 2 3      Sample Output 1 2  5 and 7 satisfy the condition.       Sample Input 2 10 40 6 8      Sample Output 2 23       Sample Input 3 314159265358979323 846264338327950288 419716939 937510582      Sample Output 3 532105071133627368

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

def lcm(m, n):
    return gcd(m, n) * m // gcd(m, n) * n // gcd(m, n)

A, B, C, D = map(int, input().split())
all_nums = B - A + 1
c_divisible = B // C - (A // C if A % C != 0 else A // C - 1) 
d_divisible = B // D - (A // D if A % D != 0 else A // D - 1)
cd_divisible = B // lcm(C, D) - (A // lcm(C, D) if A % lcm(C, D) != 0 else A // lcm(C, D) - 1)
print(all_nums - c_divisible - d_divisible + cd_divisible)

Solution (Python):
import fractions

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

b = B//C + B//D - B//((C*D)//fractions.gcd(C, D))

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

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

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

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

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

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




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

Solution (Python):
import fractions


def solve(a,b,c,d):
    a -= 1
    bc = b // c
    bd = b // d
    bcd = b // (c*d//fractions.gcd(c, d))

    ac = a // c
    ad = a // d
    acd = a //  (c*d//fractions.gcd(c, d))

    return b - bc - bd + bcd - (a - ac - ad + acd)
    
if __name__ == "__main__":

    assert solve(4,9,2,3) == 2
    assert solve(10, 40, 6,8) == 23
    assert solve(314159265358979323, 846264338327950288, 419716939, 937510582) == 532105071133627368

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

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

def f(x, y, z):
    return z // x - (y - 1) // x


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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20

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

def count(L,R,d):
    if L%d == 0:
        left = L
    else:
        left = L - L%d + d
    if R%d ==0:
        right = R
    else:
        right = R - R%d
    v = max((right-left)//d + 1, 0)
    return(v)

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

multiple = count(A,B,C) + count(A,B,D) - count(A,B,lcm)


ans = B-A+1 - multiple
print(ans)

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

Solution (Python):
def gcd(a,b):
  while b!=0:
    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())
l = lcm(c, d)
bad = (b // c) - ((a-1) // c) + (b // d) - ((a-1) // d) - (b // l) + ((a-1) // l)
ans = b - a + 1 - bad
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main(): 
    A, B, C, D = LI()

    Cで割り切れる個数 = B // C - (A-1) // C
    Dで割り切れる個数 = B // D - (A-1) // D
    from fractions import gcd
    def lcm(x, y):
        return x * y // gcd(x, y)
    CDの最小公倍数 = lcm(C,D)
    CでもDでも割り切れる個数 = B // CDの最小公倍数 - (A-1) // CDの最小公倍数

    print(B-(A-1) - Cで割り切れる個数 - Dで割り切れる個数 + CでもDでも割り切れる個数)


main()

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())
min_div = (a-1)//c + (a-1)//d - (a-1)//lcm(c, d)
max_div = b//c + b//d - b//lcm(c, d)
print(b-a+1-(max_div-min_div))

Solution (Python):
from fractions import gcd


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


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

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

CD = lcm_base(C, D)
countCD = B//CD - (A+CD-1)//CD + 1

ans = (B-A+1) - count_C - count_D + countCD
print(ans)

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


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

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


ac = a // c
ad = a // d
acd = a // lcm(c, d)

bc = b // c
bd = b // d
bcd = b // lcm(c, d)

bx = b - (bc + bd) + bcd
ax = a - (ac + ad) + acd
print(bx - ax)

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


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


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


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

x = B // C - (A - 1) // C
y = B // D - (A - 1) // D
E = lcm(C, D)
z = B // E - (A - 1) // E
print(B - A + 1 - (x + y - z))

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

def aaa(a, b):
    return a // b


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


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


c1 = aaa(A-1, C)
c2 = aaa(B, C)
c = c2 - c1

d1 = aaa(A-1, D)
d2 = aaa(B, D)
d = d2 - d1

cd1 = aaa(A-1, lcm(C, D))
cd2 = aaa(B, lcm(C, D))
cd = cd2 - cd1

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

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 fractions
A, B, C, D = map(int, input().split())
m = C*D//fractions.gcd(C, D)
ans = B-A+1-((B//C-(A-1)//C)+(B//D-(A-1)//D)-(B//m-(A-1)//m))
print(ans)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]


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


a,b,c,d = get_nums_l()


t = a - (a%c-1)
cx = (b-t+1)//c + (1 if a%c==0 else 0)

t = a - (a%d-1)
dx = (b-t+1)//d + (1 if a%d==0 else 0)

cd = c*d //(gcd(c,d))
t = a - (a%cd-1)
cdx = (b-t+1)//cd + (1 if a%cd==0 else 0)

all = b-a+1
print(all - cx - dx + cdx)

Solution (Python):
from fractions import gcd
A,B,C,D = map(int,input().split())
def f(n):
    x = n//C
    y = n//D
    z = n//(C*D//gcd(C,D))
    return n - x - y + z

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

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

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 = (int(i) for i in input().split())

hogeb = B // D
hogec = B // C
hogebc = B // lcm(D,C)

hoge = B-(hogeb + hogec -hogebc)

ononb = (A-1) // D
ononc = (A-1) // C
ononbc = (A-1) // lcm(D,C)

onon = A-1-(ononb + ononc - ononbc)

print(hoge-onon)

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

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

g = gcd(c,d)
m = (c//g) * (d//g) * g

dc_a = (a-1)//c
dd_a = (a-1)//d

dc_b = b//c
dd_b = b//d

dm_a = (a-1)//m
dm_b = b//m

res = b-a+1

alll = (dc_b - dc_a) + (dd_b - dd_a) - (dm_b - dm_a)

res = res - alll
print(res)

Solution (Python):
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
CD = (C * D) // gcd(C, D)
CD_num = B // CD - (A - 1) // CD
print(int(B - A + 1 - C_num - D_num + CD_num))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, d=DVSR): return pow(x, d - 2, d)
def DIV(x, y, d=DVSR): return (x * INV(y, d)) % d
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

def gcd(x, y):
    if x < y:
        x = x ^ y
        y = x ^ y
        x = x ^ y
    div = x % y
    while div != 0:
        x = y
        y = div
        div = x % y
    return y

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


A,B,C,D=LI()

Cnum = B//C - A//C + (not A % C)
Dnum = B//D - A//D + (not A % D)


LC = lcm(C,D)
LCnum = B//LC - A//LC + (not A % LC)

BAISUU = ((Cnum + Dnum) - LCnum)
print((B-A+1) - ((Cnum + Dnum) - LCnum))

Solution (Python):
import fractions

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

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

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

C_D=(C*D)//fractions.gcd(C,D)
C_D_both=(B//C_D)-((A-1)//C_D)

print(B-A+1-C_only-D_only+C_D_both)

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)


E = lcm(C, D)

res_b = B // C + B // D - B // E
res_a = (A - 1) // C + (A - 1) // D - (A - 1) // E

print((B - res_b) - (A - 1 - res_a))

Solution (Python):
import fractions

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

A, B, C, D = list(map(int, input().split()))
NC = B // C - A // C
if A % C == 0:
  NC += 1
ND = B // D - A // D
if A % D == 0:
  ND += 1
LCM = lcm(C, D)
NCA = B // LCM - A // LCM
if A % LCM == 0:
  NCA += 1
TOTAL = B - A + 1
NOTN = NC+ND-NCA
print (TOTAL - NOTN)

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())
l = lcm(c,d)
ans = b-a+1
ans-=b//c
ans-=b//d
ans+=b//l
ans+=(a-1)//c
ans+=(a-1)//d
ans-=(a-1)//l
print(ans)

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

def gcd(x, y):
  b = max(x, y)
  s = min(x, y)
  r = b % s
  while r != 0:
    b = s
    s = r
    r = b % s
  return s

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

E = lcm(C, D)

def div(X):
  return X - ((X // C) + (X // D)) + (X // E)

print(div(B) - div(A - 1))

Solution (Python):
def gcd(x,y):
    while not y == 0:
        x,y = y,x%y
    return x  
A,B,C,D = map(int,input().split())
A -= 1
ans = (B-(B//C)-(B//D)+(B//(C*D//gcd(C,D))))-(A-(A//C)-(A//D)+(A//(C*D//gcd(C,D))))
print(ans)

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


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


ANS=0
ANS+=(B//C)

ANS-=((A-1)//C)

ANS+=(B//D)

ANS-=((A-1)//D)

ANS-=(B//(C*D//gcd(C,D)))
ANS+=((A-1)//(C*D//gcd(C,D)))

print(B-A+1-ANS)

Solution (Python):
from fractions import gcd
inpl = lambda: list(map(int,input().split()))
def lcm(a,b): return a*b//gcd(a,b)
A, B, C, D = inpl()
CD = lcm(C,D)
XB = B//C + B//D - B//CD
XA = (A-1)//C + (A-1)//D - (A-1)//CD
X = XB - XA
Z = (B - A + 1) - X
print(Z)

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

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

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

def gcd(n, m):
    
    a = max(n,m)
    b = min(n,m)
    while b:
        a, b = b, a % b
    return a


def num(l,r,n):
    nr = r//n
    nl = l//n
    if l%n == 0:
        return nr-nl+1
    else:
        return nr-nl
saishou = int(c*d/(gcd(c,d)))
n = b-a+1
n1 = num(a,b,c)
n2 = num(a,b,d)
n3 = num(a,b,saishou)
print(n-(n1+n2-n3))

Solution (Python):

import sys


from fractions import gcd


















input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())



def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    tokens = iterate_tokens()
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    D = int(next(tokens))  

    
    X = (B // C) - ((A-1) // C)
    Y = (B // D) - ((A - 1) // D)
    Z = (B // (C * D // gcd(C, D))) - ((A - 1) // (C * D // gcd(C, D)))
    print(int(B-A+1-X-Y+Z))

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

Solution (Python):

from fractions import gcd

def solve():
    A, B, C, D = map(int, input().split())
    E = C*D//gcd(C,D)

    return B-A+1 - ((B//C + B//D - B//E)-((A-1)//C + (A-1)//D - (A-1)//E))

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

Solution (Python):
A, B, C, D = map(int, input().split())
sb = B // C
sa = (A - 1) // C
tb = B // D
ta = (A - 1) // D


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


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


mb = B // lcm
ma = (A - 1) // lcm

L = sb - sa + tb - ta - (mb - ma)
print(B - A - L + 1)

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

tmp_minL = b // minL
tmp_c = b // c
tmp_d = b // d


tma_minL = (a - 1) // minL
tma_c = (a - 1) // c
tma_d = (a - 1) // d


left = tmp_c + tmp_d - tmp_minL
right = tma_c + tma_d - tma_minL


print(b - a + 1 - (left - right))

Solution (Python):
a,b,c,d=map(int,input().split())
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a  
nc=b//c-(a-1)//c
nd=b//d-(a-1)//d
x=c*d//gcd(c,d)
ncd=b//x-(a-1)//x

print(b-a+1-nc-nd+ncd)

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

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

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

t1 = b // c
t2 = (a - 1) // c
u1 = b // d
u2 = (a - 1) // d
v1 = b // x
v2 = (a - 1) // x

ans = (b - a + 1) - (t1 - t2) - (u1 - u2) + (v1 - v2)
print(ans)

Solution (Python):


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


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

cd = int(c*d/gcd(c,d))
cd_ok = b//cd - (a-1)//cd

print(int(b-a+1-c_ok-d_ok+cd_ok))

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

main()

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

a,b,c,d=map(int,input().split())
lcm=(c*d)//gcd(c,d)
num1=(b-a+1)
num2=b//c-(a-1)//c
num3=b//d-(a-1)//d
num4=b//lcm-(a-1)//lcm
ans=num1-num2-num3+num4
print(ans)

Solution (Python):
import collections as c
from fractions import gcd
ip = lambda : map(int, input().split())

mod = 1e9+7
A,B,C,D = ip()
cd = C*D//gcd(C,D)

ans = B-A+1 - ( B//C - (A+C-1)//C + B//D - (A+D-1)//D - B//cd + (A+cd-1)//cd + 1)



print(ans)

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


def m(a, b):
    if a < b:
        a, b = b, a

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

def g(a, b):
    return a * b // m(a, b)

num_c = B // C - (A - 1) // C
num_d = B // D - (A - 1) // D
cd = g(D, C)
num_cd = B // cd - (A - 1) // cd

print(B - A + 1 - (num_c + num_d - num_cd))

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

def n_div(N, a, b):
    return N - (N//a + N//b - N//(a*b//gcd(a,b)))

A,B,C,D = (int(x) for x in input().split())

ans = n_div(B, C, D)-n_div(A-1, C, D)

print(ans)

Solution (Python):


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

  return a


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


def count(v, x, y):
  v_x = v // x
  v_y = v // y
  v_xy = v // lcm(x, y)

  return v_x + v_y - v_xy


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

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


if __name__ == '__main__':
  main()

Solution (Python):

def gcd(x,y):
    if x % y == 0:
        return y
    else:
        return  gcd(y, x%y)
A,B,C,D = list(map(int,input().split()))
LCD = C * D // gcd(C,D)

Bc = B // C
Bd = B // D
Blcd = B // LCD
Bans = B - Bc - Bd + Blcd 

A = A - 1
Ac = A // C
Ad = A // D
Alcd = A // LCD
Aans = A - Ac - Ad + Alcd

print(Bans - Aans)

Solution (Python):
def gcd(n,m):
    while n:
        m,n = n,m%n
    return m


def a(n,m):
    return n//m

A,B,C,D = map(int,input().split())
E = C*D//gcd(C,D)
b = A-1 - (a(A-1,C)+a(A-1,D)-a(A-1,E))
c = B - (a(B,C)+a(B,D)-a(B,E))
print(c-b)

Solution (Python):
import fractions

f = lambda n, m : n / m
lcm = lambda a, b : a * b / fractions.gcd(a, b)

a, b, c, d = map(int, raw_input().split())
l = lcm(c, d)

a -= 1
a1 = f(a, c) + f(a, d) - f(a, l)
a2 = f(b, c) + f(b, d) - f(b, l)

print (b - a) - (a2 - a1)

Solution (Python):
try:
    from math import gcd
except:
    from fractions import gcd

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

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

Solution (Python):
from fractions import gcd

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

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

ans = (B - A + 1) - (B // C - (A - 1) // C) - (B // D - (A - 1) // D) + (B // M - (A - 1) // M)

print(ans)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

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


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


def func(n, c, d):
    cdlcm = lcm(c,d)
    return n//c + n//d - n//cdlcm

a,b,c,d = li()

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

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

Solution (Python):
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op



inf = float('inf')
input = stdin.readline


def main():
    a, b, c, d = get_li()
    c_min = (a + c - 1) // c
    c_max = b // c
    if c == d:
        print(b - a + 1 - (c_max - c_min + 1))
        return
    d_min = (a + d - 1) // d
    d_max = b // d
    e = c * d // gcd(c, d)
    if c == e:
        print(b - a + 1 - (d_max - d_min + 1))
        return
    if d == e:
        print(b - a + 1 - (c_max - c_min + 1))
        return
    e_min = (a + e - 1) // e
    e_max = b // e
    print(b - a + 1 - (c_max - c_min + 1) - (d_max - d_min + 1) + (e_max - e_min + 1))
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    setrecursionlimit(4100000)
    main()

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

from fractions import gcd

g = gcd(C,D)
l = C*D //g
k = l - (l//C) - (l//D) + 1 

def n_not_devided_c_or_d(n):
  if n//l:
    return n_not_devided_c_or_d(n%l) + k * (n//l)
  return n - n//C - n//D




print(n_not_devided_c_or_d(B) - n_not_devided_c_or_d(A-1))

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

    return gcd(x,y%x)

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

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

ans1=(A-1)//C+(A-1)//D-(A-1)//(lcm(C,D))
ans2=(B)//C+(B)//D-(B)//(lcm(C,D))

ans3=(B-A+1)-(ans2-ans1)
print(ans3)

Solution (Python):
import sys
sys.setrecursionlimit(pow(10, 6))

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

def main():
  a, b, c, d = map(int, input().split())
  divc = b//c - (a-1)//c
  divd = b//d - (a-1)//d
  abgcd = gcd(c, d)
  maxcd = (max(c, d) // abgcd) * min(c, d)
  print(b-a+1-divc-divd+(b//maxcd - (a-1)//maxcd))


if __name__ == '__main__':
  main()

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

Solution (Python):
def GCD(a, b):
    if a < b:
        a, b = b, a
    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 = [int(i) for i in input().split()]

cnt_c = b//c - (a-1)//c
cnt_d = b//d - (a-1)//d
cd = LCM(c, d)
cnt_cd = b//cd - (a-1)//cd

print(b-a+1-(cnt_c + cnt_d - cnt_cd))

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

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


def all_calc(x):
    return x - (calc(x, c) + calc(x, d) - calc(x, c * d // gcd(c, d)))
print(all_calc(b) - all_calc(a - 1))

Solution (Python):
import fractions

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

C_count = B // C - (A - 1) // C
D_count = B // D - (A - 1) // D

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

CD_count = B // lcm - (A - 1) // lcm

can_divide = C_count + D_count - CD_count
print(B - A + 1 - can_divide)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    s = S()
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            print("Bad")
            quit()
    print("Good")
    return


def B():
    n,l = LI()
    a = [i+l for i in range(n)]
    ans = sum(a)
    m = n+l-1
    for i in a[::-1]:
        if i < 0:break
        m = min(m,i)
    ans -= m
    print(ans)
    return


def gcd(a,b):
    if a == 0:
        return b
    return gcd(b%a,a)
def C():
    a,b,c,d = LI()
    g = gcd(c,d)
    l = c*d//g
    sc = b//c-(a-1)//c
    sd = b//d-(a-1)//d
    sl = b//l-(a-1)//l
    print(b-a+1-sc-sd+sl)
    return


def D():
    n = I()

    return


def E():
    n = I()

    return


def F():
    n = I()

    return


if __name__ == "__main__":
    C()

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

Solution (Python):
from fractions import gcd

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

A = (a-1) // c + (a-1) // d - (a-1) // ((c * d) // gcd(c, d))
B = b // c + b // d - b // ((c * d ) // gcd(c, d))

print (b - B - (a - 1 - A))

Solution (Python):


import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

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

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

    CD = C * D // gcd(max(C, D), min(C, D))
    
    ac_div = (A - 1) // C
    ad_div = (A - 1) // D

    acd_div = (A - 1) // CD
    a_total = ac_div + ad_div - acd_div

    
    bc_div = B // C
    bd_div = B // D

    bcd_div = B // CD
    b_total = bc_div + bd_div - bcd_div

    
    ans = B - A + 1 - (b_total - a_total)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


from collections import deque
import itertools as ite
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

A, B, C, D = map(int, raw_input().split())
def GCD(a, b):
	dummy1 = max(a, b)
	dummy2 = min(a, b)
	while True:
		dummy = dummy1 % dummy2
		dummy1 = dummy2
		dummy2 = dummy
		if dummy == 0:
			break
	return dummy1

ans = 0

LCM = C * D / GCD(C, D)

ans += B
ans -= B / D
ans -= B / C
ans += B / LCM
A -= 1
ans -= A
ans += A / D
ans += A / C
ans -= A / LCM
print ans

Solution (Python):
class FutureMath:
    def gcd(self, a, b):
        if a < b:
            return self.gcd(b, a)
        if b == 0:
            return a
        while b:
            a, b = b, a % b
        return a

    def gcd_(self, list):
        result = list[0]
        for el in list[1:]:
            result = self.gcd(result, el)
        return result

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

    def lcm_(self, list):
        result = list[0]
        for el in list[1:]:
            result = self.lcm(result, el)
        return result

    def divisors(self, n):
        result = list()
        for i in range(1, n+1):
            if n % i == 0:
                result.append(i)
        return result


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

fm = FutureMath()


a, b, c, d = LI()
B = b - (b//c + b//d - b//fm.lcm(c, d))
A = a - (a//c + a//d - a//fm.lcm(c, d))
border = 1 - int((a % c == 0) or (a % d == 0))
print(B - A + border)

Solution (Python):
from fractions import gcd

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

lcm = c * d // gcd(c,d)
temp1 = b - b//c - b//d + b//lcm
temp2 = (a-1) - (a-1)//c - (a-1)//d + (a-1)//lcm

print(temp1 - temp2)

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)

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

s = A//C + A//D - A//N
t = B//C + B//D - B//N

print(B-t - (A-s))

Solution (Python):

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

def gcd(GCD1,GCD2):
    x = min(GCD1,GCD2)
    y = max(GCD1,GCD2)
    if y%x == 0:
        return x
    else:
        return gcd(x,y%x)
GCD = gcd(C,D)
per = C*D//GCD
def f(n,a,b):
    gab = gcd(a,b)
    perab = a*b//gab
    ret = n-(n//a)-(n//b)+(n//perab)
    return int(ret)
print(f(B,C,D)-f(A-1,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())
a -= 1
cnt0B = b // c + b // d - b // lcm(c, d)
cnt0A = a // c + a // d - a // lcm(c, d)

print(b - a - (cnt0B - cnt0A))

Solution (Python):
import fractions

def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft



def main():
    a,b,c,d=map(int, input().split())
    x0 = b // c - (a-1) // c
    x1 = b // d - (a-1) // d
    tmp = c*d//fractions.gcd(c, d)
    x2 = b // tmp - (a-1) // tmp
    print(b-a+1-x0-x1+x2)
main()

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(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)

A,B,C,D = inpl()
ans = B-A+1
cn = B//C - ((A-1)//C+1) + 1
dn = B//D - ((A-1)//D+1) + 1
cd = lcm(C,D)
cdn = B//cd - ((A-1)//cd+1) + 1

print(ans-cn-dn+cdn)

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


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


i=(B//C)-(-((-A)//C))+1
j=(B//D)-(-((-A)//D))+1
k=(B//L)-(-((-A)//L))+1
print(B-A+1-(i+j-k))

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

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

def lcm(a, b):
	return a * b // gcd (a, b)
  
R = lcm(C,D)

def chk(V):
  return V - (V//C) - (V//D) + (V//R)

print ( chk(B) - chk(A-1) )

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

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

def lcm(a, b):
    g = gcd(a, b)
    g_a = a // g
    g_b = b // g
    return g_a * g_b * g

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

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

Solution (Python):

import sys
INF = float("inf")


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


def solve(A: int, B: int, C: int, D: int):
    U = B-A+1
    cdivnum = B//C - (A-1)//C
    ddivnum = B//D - (A-1)//D
    cd = C*D//GCD(C, D)
    cddivnum = B//cd - (A-1)//cd
    print(U-cdivnum-ddivnum+cddivnum)
    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)


if __name__ == '__main__':
    main()

Solution (Python):

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

nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

A, B, C, D = nl()

c_total = B // C - (A - 1) // C
d_total = B // D - (A - 1) // D

CD = lcm(C, D)
cd_total = B // CD - (A - 1) // CD

print(B - A + 1 - (c_total + d_total - cd_total))