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

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


from fractions import gcd





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


a=A//C
b=B//C

c=0
for i in range(a,b+1):
	if A<=(i*C)<=B:
		c = b-i+1
		break
c=max(0,c)



a=A//D
b=B//D
d=0
for i in range(a,b+1):
	if A<=(i*D)<=B:
		d = b-i+1
		break
d=max(0,d)



E=lcm(C,D)

a=A//E
b=B//E
e=0
for i in range(a,b+1):
	if A<=(i*E)<=B:
		e = b-i+1
		break
e=max(0,e)



ans= B-A+1-(c + d - e)

print(ans)

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

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

cd = lcm(c, d)



ca = ((a-1) // c)

cb = (b // c)

tmp_c = cb - ca

da = (a // d)

db = (b // d)

tmp_d = db - da

cda = (a // cd)

cdb = (b // cd)

tmp_cd = cdb - cda
ans = b - a + 1 - (tmp_c + tmp_d - tmp_cd)

print(ans)

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

Solution (Python):










import math
import sys
import itertools
import queue
from functools import reduce
from fractions import gcd

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

mod = 1000000007

if __name__ == "__main__":
    A, B, C, D = map(int, sys.stdin.readline().split())
    A = A - 1
    c_div = int(B // C)
    d_div = int(B // D)
    cd_div = int(B // lcm(C, D))

    not_div1 = B - (c_div + d_div -cd_div)

    c_div = int(A // C)
    d_div = int(A // D)
    cd_div = int(A // lcm(C, D))

    not_div2 = A - (c_div + d_div -cd_div)

    print(not_div1 - not_div2)

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

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

g = gcd(c, d)
print(s - (dev(c) + dev(d) - dev(c * d // g)))

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

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

ac = (a-1) // c
ad = (a-1) // d
ae = (a-1) // e
bc = b // c
bd = b // d
be = b // e
print((b-a+1) - ((bc + bd - be) - (ac + ad - ae)))

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

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

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


countC = B // C - A // C
if A % C == 0:
    countC += 1
countD = B // D - A // D
if A % D == 0:
    countD += 1
s = gcd(C, D)
countCD = B // C // (D // s) - A // C // (D // s)
temp = A / C / (D // s)
if temp == int(temp):
    countCD += 1

res = (B - A + 1 - countC - countD + countCD)
if res < 0:
    res = 0
print(res)

Solution (Python):
import numpy as np
import math

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

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
























size = b - a + 1
cd = int(c * d // gcd(c, d))
c_num_a = a // c
d_num_a = a // d
cd_num_a = a // cd
c_num_b = b // c
d_num_b = b // d
cd_num_b = b // cd

c_num = c_num_b - c_num_a
d_num = d_num_b - d_num_a
cd_num = cd_num_b - cd_num_a

if a % c == 0:
    c_num += 1
if a % d == 0:
    d_num += 1
if a % cd == 0:
    cd_num += 1

print(size - c_num - d_num + cd_num)

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

Solution (Python):
import sys,collections,math,random,fractions;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())

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

a,b,c,d = Is()
mc = b//c - (a-1)//c
md = b//d - (a-1)//d
mincom = lcm(c,d)
mcd = b//(mincom) - (a-1)//(mincom)
ans = b-a+1 - (mc + md - mcd)

print(ans)

Solution (Python):
import fractions

def calcA(a, b):
    ans = a / b
    if ans.is_integer():
        return a // b
    else:
        return a // b + 1 


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

a1 = calcA(a,c)

a2 = b // c
a3 = a2 - a1 + 1

b1 = calcA(a,d)

b2 = b // d
b3 = b2 - b1 + 1

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

x1 = calcA(a,x)
x2 = b // x
x3 = x2 - x1 + 1

p = b - a + 1
q = a3 + b3 - x3

print(p - q)

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

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

def func(num, A, B):
    if A % num == 0:
        return B // num - A // num + 1
    else:
        return B // num - A // num

first = func(C, A, B)
second = func(D, A, B)
third = func(LCM, A, B)

total = first + second - third

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

Solution (Python):
import sys
import fractions

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

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

count = b - a + 1 -1* (b // c) + (a-1) // c  -1*( b // d )+ (a-1) // d  + b // lcm -1*( (a-1) // lcm)

if count < 0 :
    count = 0

print(count)

Solution (Python):
import fractions
RangeMin, RangeMax, valA, valB = map(int, input().split())

RangeMin -= 1
valC = int(valA * valB / fractions.gcd(valA, valB))
ansList = []



for Range in [RangeMin, RangeMax] :
  ans1 = 0
  ans1 += Range // valA
  ans1 += Range // valB
  ans1 -= Range // valC
  ansList.append(ans1)

ans = ansList[1] - ansList[0]
ans = RangeMax - RangeMin - ans

print(ans)

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


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


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

e=lcm(c,d)


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


ans_h=k(b,c)+k(b,d)-k(b,e)-(k(a-1,c)+k(a-1,d)-k(a-1,e))

print(b-a+1-ans_h)

Solution (Python):
from fractions import gcd

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

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

alpha_c = a % c
count_c = (b - a + alpha_c) // c
if alpha_c == 0:
    count_c += 1
alpha_d = a % d
count_d = (b - a + alpha_d) // d
if alpha_d == 0:
    count_d += 1
alpha_g = a % g
count_g = (b - a + alpha_g) // g
if alpha_g == 0:
    count_g += 1

s = b - a + 1 - count_c - count_d + count_g

print(s)

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 = [int(x) for x in input().split(' ')]
a = (B - A + 1) 
b = ((B // C) - ((A-1) // C))
c = ((B // D) - ((A-1) // D))
CD = lcm(C,D)
d = ((B // (CD)) - ((A-1) // (CD)))
q = a - b - c + d
print(q)

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

Solution (Python):
import fractions
def amari(a):
    return a - a // C - a // D + a // L
A, B, C, D = map(int,input().split())
L = (C * D) // fractions.gcd(C, D)
E = amari(L)
K = B - A + 1
counter = amari(B - K // L * L) - amari(A - 1)
print((K // L) * E + counter)

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

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

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

print(nums-(c_num+d_num-cd_num))

Solution (Python):
import fractions
a,b,c,d = map(int,input().split())
lcm_cd = int(c*d / fractions.gcd(c,d))
if a%c==0:
  tc = b//c - a//c + 1
else:
  tc = b//c - a//c
if a%d==0:
  td = b//d - a//d + 1
else:
  td = b//d - a//d
if a%(lcm_cd)==0:
  tcd = b//(lcm_cd) - a//(lcm_cd) + 1
else:
  tcd = b//(lcm_cd) - a//(lcm_cd)

print(b-a+1-tc-td+tcd)

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

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
  
x = (b // c) - (a // c)
if a % c == 0:
  x += 1
y = (b // d) - (a // d)
if a % d == 0:
  y += 1

lcm = c * d // gcd(c, d)  
z = (b // lcm) - (a // lcm)
if a % lcm == 0:
  z += 1

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

Solution (Python):
import fractions


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

A, B, C, D = map(int, input().split())
AC = (A - 1) // C
AD = (A - 1) // D
BC = B // C
BD = B // D
BCD = B // lcm(C, D)
ACD = (A - 1) // lcm(C, D)
tmp = (BC + BD) - (AC + AD) - (BCD - ACD)
ans = (B - A + 1) - tmp
print(ans)

Solution (Python):
A,B,C,D = map(int, input().split())
ma=B
mi=A-1
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a
def lcm(a,b):
  return a*b//gcd(a,b)

E=lcm(C,D)

ta=ma//C
tb=mi//C
h=ta-tb

tc=ma//D
td=mi//D
i=tc-td

te=ma//E
tf=mi//E
j=te-tf

wareru=h+i-j
kotae=ma-mi-wareru
print(kotae)

Solution (Python):
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
a,b,c,d = map(int,input().split())
cnt = 0
cc = c
dd = d
cd = lcm(c,d)
cdcd = lcm(c,d)

if a % c == 0:
  minc = a // c
else:
  minc = a // c + 1
maxc = b // c
if a < c:
  lenc = maxc
else:
  lenc = maxc - minc + 1
  
if a % d == 0:
  mind = a // d
else:
  mind = a // d + 1
maxd = b // d
if a < d:
  lend = maxd
else:
  lend = maxd - mind + 1

if a % cd == 0:
  mincd = a // cd
else:
  mincd = a // cd + 1
maxcd = b // cd
if a < cd:
  lencd = maxcd
else:
  lencd = maxcd - mincd + 1



print(b-a+1-lenc-lend+lencd)

Solution (Python):
import sys
import fractions

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

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

if __name__ == "__main__":
    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))

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

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

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

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

def getDivNum(n, c, d):
  return n // c + n // d - (n // lcm(c, d))

print(B - A - getDivNum(B, C, D) + getDivNum(A-1, C, D) + 1)

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

if c!=d:
    
    
    sma=min(c,d)
    big=max(c,d)
    
    while sma!=0 :
        cal=big%sma
        big=sma
        sma=cal
    
    euc=big
    ggg=c*d//euc
    
    
    
    def  cnt(n,ggg,c,d):
        exc=ggg//c + ggg//d
        if c<ggg or d<ggg:
            exc-=1
        ret=exc*(n//ggg)
        
        remain=n%ggg
        ret+= remain//c + remain//d
        return ret
    
    
    
    
    jog=cnt(b,ggg,c,d)-cnt(a-1,ggg,c,d)
    
    ans=(b-a+1)-jog
    
    print(ans)
    
else:
    jog=(b//c) -((a-1)//c)
    print(b-a+1-jog)

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

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

def EA(X,Y):
    if X%Y==0:
        return Y
    
    return EA(Y,X%Y)

if C>D:
    CD=int(C/EA(C,D))*D
else:
    CD=int(C/EA(D,C))*D

A-=1

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

Solution (Python):
import platform
ver = platform.python_version_tuple()
if int(ver[1]) < 5:
    from fractions import gcd
else:
    from math import gcd
from sys import stdin

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

    lcm = lambda x, y: x * y // gcd(x, y)
    n = 0
    for a, b, x in ((A, B, C), (A, B, D)):
        if a % x:
            n += b // x - a // x
        else:
            n += b // x - (a // x - 1)

    lcm_ = lcm(C, D)
    if a % lcm_:
        n -= b // lcm_ - a // lcm_
    else:
        n -= b // lcm_ - (a // lcm_ - 1)

    print(B - A + 1 - n)

input = lambda: stdin.readline()
main()

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)
def func(start,end,n):
    ans = (end-start+1)//n
    bonus = (end-start+1)%n
    
    
    if ((start // n + 1) * n)  <= (start + bonus - 1):
        ans += 1
    
    return ans
cnt_c = func(a,b,c)
cnt_d = func(a,b,d)
cnt_cd = func(a,b,lcm(c,d))
print (b-a-(cnt_c + cnt_d - cnt_cd)+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())

sub = B - A + 1
cDivNum = (B//C) - ((A-1)//C)
dDivNum = (B//D) - ((A-1)//D)
cdDivNum = (B//lcm(C,D)) - ((A-1)//lcm(C,D))


print(sub - (cDivNum+dDivNum-cdDivNum))

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

A, B, C, D = map(int, input().split())
divC = B // C - (A-1) // C
divD = B // D - (A-1) // D
divCD = B // ((C*D)//euc(C, D)) - (A-1) // ((C*D)//euc(C, D))
print((B-A+1) - divC - divD + divCD)

Solution (Python):
A, B, C, D = map(int, input().split())
def gcd(x,y):
    while y!=0:
        x,y=y,x%y
    return x

NBC = B//C
NBD = B//D
CD_lcm = (C*D)//gcd(C,D)   
NBCD = B//CD_lcm 
B_0 = (NBC+NBD)-NBCD  
ans_B = B-B_0
NAC = (A)//C
NAD = (A)//D
NACD = (A)//CD_lcm  
A_0 = (NAC+NAD)-NACD 
ans_A = A-A_0
if A%C==0 or A%D==0:
    ans = ans_B-ans_A
else:
    ans = ans_B-ans_A+1
print(ans)

Solution (Python):
from fractions import gcd
from decimal import *

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

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)


CD = lcm(C, D)



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





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

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

Solution (Python):

a,b,c,d=map(int,input().split())
def gcd(x , y):
    if y == 0:
        return x 
    else:
        return gcd(y, x%y)

def gys(a,b):
    remainder = gcd(a,b)
    return a*b//remainder

lcmm=gys(c,d)
def ans(n,k):
    return n//k
def x(s):
    return ans(s,c)+ans(s,d)-ans(s,lcmm)


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

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
a,b,c,d=[int(i) for i in input().split()]
bac=(b//c)-((a-1)//c)
bad=(b//d)-((a-1)//d)
bacd=(b//lcm(c,d))-((a-1)//lcm(c,d))
print(b+1-a-bac-bad+bacd)

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

c_BC = B // C *(C - 1) + B % C
c_BD = B // D * (D - 1) + B % D
c_BCD = B // CD * (CD - 1) + B % CD
c_AC = A // C * (C - 1) + A % C
c_AD = A // D * (D - 1)+ A % D
c_ACD = A // CD * (CD - 1) + A % CD

ans = c_BC + c_BD - c_BCD - (c_AC + c_AD - c_ACD)
print(int(ans))

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

Solution (Python):

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

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

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

s = -1
c = C * (A // C + 1)
if A % C == 0:
    c = A
if c <= B:
    s += (B - c) // C + 1
        
d = D * (A // D + 1)
if A % D == 0:
    d = A
if d <= B:
    s += (B - d) // D + 1
    
cd_lcm = lcm(C, D)
cdl = cd_lcm * (A // cd_lcm + 1)
if A % cd_lcm == 0:
    cdl = A
if cdl <= B:
    s -= (B - cdl) // cd_lcm + 1

print(B - A - s)

Solution (Python):
import fractions
A,B,C,D=map(int,input().split())
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
'''
def get_factors(x,retlist):
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            if i not in retlist:
              retlist.append(i)
            x = x // i
    if x!=1:
      retlist.append(x)
    return retlist
  
prime=[]
prime=get_factors(C,prime)
prime=get_factors(D,prime)
'''
AmodC=A%C
AmodD=A%D
BmodC=B%C
BmodD=B%D
CD=lcm(C,D)
AmodCD=A%CD
BmodCD=B%CD

BA=B-A+1
Cbaisu=BA//C
Dbaisu=BA//D
CDbaisu=BA//CD


if BA-(Cbaisu*C)>=C-((A+(Cbaisu*C))%C)+1:
  Cbaisu+=1
if BA-(Dbaisu*D)>=D-((A+(Dbaisu*D))%D)+1:
  Dbaisu+=1
if BA-(CDbaisu*CD)>=CD-((A+(CDbaisu*CD))%CD)+1: 
  CDbaisu+=1



print(BA-Cbaisu-Dbaisu+CDbaisu)

Solution (Python):
def gcd(a,b):
	while b:
		a, b = b, a % b
	return a
a,b,c,d=map(int,input().split())
n=b-a+1
dc=(a%c==0)*1
dd=(a%d==0)*1
div_c=b//c-a//c+dc
div_d=b//d-a//d+dd
lcm=(c*d)//gcd(c,d)
dcd=(a%lcm==0)*1
div_cd=b//lcm-a//lcm+dcd
print(n-div_c-div_d+div_cd)

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

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

Solution (Python):
from fractions import gcd

def calc(A, B, x):
    n1 = (A - 1) // x
    n2 = B // x
    return n2 - n1

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

Solution (Python):
import fractions

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

num = B - A + 1




if A % C == 0:
    first_c = A
else:
    first_c = A + (C - A % C)
if A % D == 0:
    first_d = A
else:
    first_d = A + (D - A % D)
if A % lcm == 0:
    first_e = A
else:
    first_e = A + (lcm - A % lcm)

c = (B - first_c) // C + 1
d = (B - first_d) // D + 1
e = (B - first_e) // lcm + 1

print(num - c - d + e)

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

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




Cnum=int((B-A+1)//C)+ ((B-A+1)%C >= (C-(A-1)%C))
Dnum=int((B-A+1)//D)+ ((B-A+1)%D >= (D-(A-1)%D))

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

CDnum=int((B-A+1)//CD)+ ((B-A+1)%CD >= (CD-(A-1)%CD))

print(B-A+1-Cnum-Dnum+CDnum)

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

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


div_C = B//C - (A//C + (-1 if A%C==0 else 0))  
div_D = B//D - (A//D + (-1 if A%D==0 else 0))  
div_CD = B//(CD) - max(0, (A//(CD) + (-1 if A%(CD)==0 else 0)))
print(B-A+1 - div_C - div_D + div_CD)

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

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

Solution (Python):
import sys
import fractions
input = sys.stdin.readline
A, B, C, D = map(int, input().split())
root_count = B - A + 1
c = B//C - (A - 1)//C
d = B//D - (A - 1)//D
cd_lcm = C * D // fractions.gcd(C, D)
e = B//cd_lcm - (A - 1)//cd_lcm
print(root_count - c - d + e)

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())
ac_can = int((a-1)//c)
ad_can = int((a-1)//d)
acd_can = int((a-1)//lcm(c,d))
bc_can = int(b//c)
bd_can = int(b//d)
bcd_can = int(b//lcm(c,d))
print(b-a+1-(bc_can+bd_can-bcd_can-(ac_can+ad_can-acd_can)))

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

CSeries = B//C -(A-1)//C
DSeries = B //D - (A-1) //D

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

LCM = int(C*D/ gcd(C,D))

CDSeries = B//LCM - (A-1)//LCM

Answer2 = B-A +1 - CSeries - DSeries + CDSeries

print(Answer2)

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

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

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

CD = C * D // gcd(C, D)
cd = B//CD - (A-1)//CD

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

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

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

a,b,c,d = map(int,input().split())
ac_can = int((a-1)//c)
ad_can = int((a-1)//d)
acd_can = int((a-1)//lcm(c,d))
bc_can = int(b//c)
bd_can = int(b//d)
bcd_can = int(b//lcm(c,d))
print(b-a+1-(bc_can+bd_can-bcd_can-(ac_can+ad_can-acd_can)))

Solution (Python):
import sys
import fractions
sys.setrecursionlimit(1000000)

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


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

c_w = (b)//c - (a+c-1)//c + 1
d_w = (b)//d - (a+d-1)//d + 1





cd = lcm(c,d)

cd_w = (b)//(cd) - (a+cd-1)//(cd) + 1


print(int(b-a+1 - c_w - d_w + cd_w))

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

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

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

lcmnum = lcm(c,d)
cs=0
ds=0
cds=0

acs = a // c
ads = a // d
acds =  a // lcmnum
aa = a-(acs+ads-acds)

bcs = b // c

bds = b // d
bcds =  b // lcmnum
bb = b-(bcs+bds-bcds)
uo = 1

if a % c==0 or a % d ==0:
	uo-=1

print(bb-aa+uo)

Solution (Python):

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

temp=list(map(lambda x:int(x),input().split()))
A,B,C,D=temp[0],temp[1],temp[2],temp[3]
public_cd=gcd(C,D)

c_n_full=B//C
d_n_full=B//D
cd_n_full=B//((C*D)//public_cd)

c_n_par=(A-1)//C
d_n_par=(A-1)//D
cd_n_par=(A-1)//((C*D)//public_cd)

c_n=c_n_full-c_n_par
d_n=d_n_full-d_n_par
cd_n=cd_n_full-cd_n_par
counter=(B-A+1)-(c_n+d_n)+cd_n

print(counter)

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

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

def lcm(a, b):
    ori_a, ori_b = a, b
    if a > b: a, b = b, a
    while True:
        a, b = b%a, a
        if a == 0:
            g = b
            break
        elif a == 1:
            g = 1
            break

    return ori_a // g * ori_b

S1 = A - ( A//C + A//D - A//lcm(C, D) ) - (1 if A%C and A%D else 0)
S2 = B - ( B//C + B//D - B//lcm(C, D) )

print(S2 - S1)

Solution (Python):
import sys
from fractions import gcd

input = sys.stdin.readline
A, B, C, D = map(int, input().split(' '))
num = B - A + 1
div_c = B // C - (A - 1) // C
div_d = B // D - (A - 1) // D
lcd = C * D // gcd(C, D)
div_cd = B // lcd - (A - 1) // lcd
print(num - div_c - div_d + div_cd)

Solution (Python):
import fractions

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

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

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

Solution (Python):
def test(a,b,u):
    ret = 0
    x,y = divmod(a,u)
    if y>0:
        x = x+1
    z = b//u
    if (u*x)>=a and (u*x)<=b:
        ret = ret + 1
        if z>x:
            ret = ret + z-x
    return ret
def Gcd(x,y):
    if( y>x ): 
        r = x
        x = y
        y = r
    if( y==0 ):
        return x
    return Gcd(y,x%y)
def lcm(x,y):
    return (x*y)//Gcd(x,y)

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

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

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

Solution (Python):
import fractions
s = input().split()
A = int(s[0])
B = int(s[1])
C = int(s[2])
D = int(s[3])
def lcm(c, d):
  return (c*d)//fractions.gcd(c, d)
count = 0
C_value = B//C -(A-1)//C
D_value = B//D -(A-1)//D
value = B//(lcm(C, D)) - (A-1)//(lcm(C, D))
print(B-(A-1)-(C_value + D_value - value))

Solution (Python):
def gcd(a, b):
    if a < b:
        tmp = a
        a = b
        b = tmp

    r = a % b
    while r!=0:
        a = b
        b = r
        r = a % b
    return b

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

A, B, C, D = list(map(int, input().split()))
A_ = A - 1
lcm_CD = lcm(C, D)
B_res = B - (B//C + B//D - B//lcm_CD)
A_res = A_ - (A_//C + A_//D - A_//lcm_CD)
print(B_res - A_res)

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

def ABC(x,y,z):
    return z-(z//x+z//y-z//lcm(x,y))

a,b,c,d=map(int,input().split())
print(ABC(c,d,b)-ABC(c,d,a-1))

Solution (Python):
from fractions import gcd

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

Solution (Python):
from decimal import Decimal, ROUND_FLOOR


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 divisible_count(a, b, d):
    a = Decimal(str(a))
    b = Decimal(str(b))
    d = Decimal(str(d))

    x = (b / d).quantize(Decimal("0"), rounding=ROUND_FLOOR)
    y = ((a - 1) / d).quantize(Decimal("0"), rounding=ROUND_FLOOR)
    return int(x - y)


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

x = divisible_count(A, B, C)
y = divisible_count(A, B, D)
z = divisible_count(A, B, lcm(C, D))

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

Solution (Python):
import fractions

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

cc = (B//C)-((A-1)//C)
dd = (B//D)-((A-1)//D)

x= (C * D // fractions.gcd(C, D))
ee = (B//x)-((A-1)//x)

ans = (B-A+1)-(cc + dd - ee)
print(int(ans))

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())
ans = set()
if A%C == 0:
    c = (B//C) - (A//C) + 1
else:
    c = (B//C) - (A//C)

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

CD = lcm(C, D)

if A%CD == 0:
    cd = (B//CD) - (A//CD) + 1
else:
    cd = (B//CD) - (A//CD)

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

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

Solution (Python):
from fractions import gcd

def count_n(mini, maxi, n):
    min_n = n*(mini//n) if mini%n == 0 else n*(mini//n+1)
    max_n = n * (maxi//n)
    return (max_n-min_n)//n+1

a,b,c,d = [int(i) for i in input().split()]
cd = c * d // gcd (c, d)
count_1 = count_n(a,b,c)
count_2 = count_n(a,b,d)
count_3 = count_n(a,b,cd)
count = b-a+1 - (count_1 + count_2 - count_3) 
print(count)

Solution (Python):
import fractions

inp = [int(i) for i in input().split()]
c1 = (inp[0]-1) // inp[2]
c2 = inp[1] // inp[2]
c = c2 - c1

d1 = (inp[0]-1) // inp[3]
d2 = inp[1] // inp[3]
d = d2 -d1

e1 = (inp[0]-1) // int(inp[3] * inp[2]/fractions.gcd(inp[3],inp[2]))
e2 = inp[1] // int(inp[3] * inp[2]/fractions.gcd(inp[3],inp[2]))
e = e2 - e1

print(int(inp[1]-inp[0]+1-(c+d-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())

tmp = b - a + 1

target_value=lcm(c,d)


c_a_under=(a-1)//c
c_b_under=b//c

c_a2b=c_b_under-c_a_under


d_a_under=(a-1)//d
d_b_under=b//d

d_a2b=d_b_under-d_a_under


cd_a_under=(a-1)//target_value
cd_b_under=b//target_value

cd_a2b=cd_b_under-cd_a_under

mayans = c_a2b+d_a2b-cd_a2b

print(tmp-mayans)

Solution (Python):
import math
import sys
input=sys.stdin.readline
from collections import deque
from heapq import heappush,heappop
import re

def int_raw():
    return int(input())
 
def ss_raw():
    return input().split()
 
def ints_raw():
    return tuple(map(int, ss_raw()))


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


DIV = 10**9+7

def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret
    
def mod_modify(a,mod):
    return (a+mod)%mod

def cal_ABC(A,B,C):
    AC = A//C + (1 if A%C>0 else 0)
    BC = (B//C)
    return max(BC-AC+1,0)


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

def main():
    ans =0
    A,B,C,D = ints_raw()
    total = B-A+1
    dC = cal_ABC(A,B,C)
    dD = cal_ABC(A,B,D)
    dCD = cal_ABC(A,B,C*D//gcd(max(C,D),min(C,D)))
    return total-dC-dD+dCD

print(main())

Solution (Python):
import sys

def solve(a, b, c, d):
    c_num = b / c - a / c
    if a % c == 0:
        c_num += 1
    d_num = b / d - a / d
    if a % d == 0:
        d_num += 1

    v = c * d / gcd(c, d)
    v_num = b / v - a / v
    if a % v == 0:
        v_num += 1

    return (b - a + 1) - (c_num + d_num - v_num)


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


if __name__ == '__main__':

    a, b, c, d = map(int, sys.stdin.readline().strip().split(" "))
    print solve(a, b, c, d)

Solution (Python):
a,b,c,d=map(int,input().split())
import fractions
h=int(c*d/fractions.gcd(c,d))
if(a%c!=0):
    e=(b//c)-(a//c)
else:
    e=(b//c)-(a//c)+1
if(a%d!=0):
    f=(b//d)-(a//d)
else:
    f=(b//d)-(a//d)+1
if(a%(h)!=0):
    g=(b//(h))-(a//(h))
else:
    g=(b//(h))-(a//(h))+1
print('{0}'.format(b-a+1-e-f+g))

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


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


A, B, C, D = [int(n) for n in input().split()]
intnum = B-A  + 1
CmulA = A // C
CmulB = B // C
Cmulnum = CmulB - CmulA
if A % C == 0:
    Cmulnum += 1
DmulA = A // D
DmulB = B // D
Dmulnum = DmulB - DmulA
if A % D == 0:
    Dmulnum += 1
CD = lcm(C,D)
CDmulA = A // CD
CDmulB = B // CD
CDmulnum = CDmulB - CDmulA
if A % CD == 0:
    CDmulnum += 1
print(intnum - (Cmulnum + Dmulnum) + CDmulnum)

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

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

lcm = (C * D) // gcd(C, D)
Am = A - 1

min1 = Am // C
min2 = Am // D
min3 = Am // lcm

max1 = B // C
max2 = B // D
max3 = B // lcm

n = B - max1 - max2 + max3 - (Am - min1 - min2 + min3)
print(n)

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

if a % kou == 0:
    hajime = a
else :
    hajime = (kou - a % kou) + a
owari = b - (b % kou)
if a % c == 0:
    chajime = a
else :
    chajime = (c - a % c) + a
cowari = b - (b % c)
if a % d == 0:
    dhajime = a
else :
    dhajime = (d - a % d) + a
dowari = b - (b % d)


kousuu = ((cowari - chajime) // c + 1) + ((dowari - dhajime) // d + 1) - ((owari - hajime) // kou + 1)
sa = b - a + 1
print(sa - (kousuu))

Solution (Python):
import fractions
A,B,C,D = map(int, input().split())
c_d = C * D // fractions.gcd(C, D)
Cd = B // C - (A - 1) // C
Dd = B // D - (A - 1) // D
C_D = max(B // c_d - (A - 1) // c_d,0)
print((B + 1 - A) - (Cd + Dd - C_D))

Solution (Python):
def computeGCD(x, y):
    while (y):
        x, y = y, x % y
    return x

def computeLCM(x, y):
    return x * y // computeGCD(x, y)

a, b, c, d = map(int, input().split())
ways1 = b // c - ((a - 1) // c)
ways2 = b // d - ((a - 1) // d)
lcm = computeLCM(c, d)
ways = ways1 + ways2 - (b // lcm - ((a - 1) // lcm))
print(b - a + 1 - ways)