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(x,y):
    if x<y:
        x,y=y,x
    while x%y!=0:
        x,y=y,x%y
    return(y)

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

c_num  = (b//c)-(a//c)
d_num  = (b//d)-(a//d)
cd_num = (b//(cd))-(a//(cd))

if a%c == 0:
    c_num = c_num+1

if a%d == 0:
    d_num = d_num+1

if a%(cd) == 0:
    cd_num = cd_num+1

if c%d == 0:
    print(b-a+1-d_num)
elif d%c ==0:
    print(b-a+1-c_num)
else:
    print(b-a+1-c_num-d_num+cd_num)

Solution (Python):
import fractions
x = input().split()
a,b,c,d = int(x[0]),int(x[1]),int(x[2]),int(x[3])

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

cn = b // c - (a-1) // c
dn = b // d - (a-1) // d
cdn = b // cd - (a-1) // cd

print((b-a+1)-(cn+dn-cdn))

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 a*b//gcd(a,b)

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

cnum = B // C - (A-1) // C
dnum = B // D - (A-1) // D
xnum = B // lcm(C,D) - (A-1) // lcm(C,D)


print(B - A + 1 - (cnum + dnum - xnum))

Solution (Python):
import fractions

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

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

Solution (Python):
import fractions

a,b,c,d=map(int,input().split())
e=b-a+1
f=b//c
g=b//d
h=(a-1)//c
i=(a-1)//d
j=c*d//fractions.gcd(c,d)
k=b//j
l=(a-1)//j
m=e-(f-h)-(g-i)+(k-l)
print(m)

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

Solution (Python):
A, B, C, D = map(int, input().split())
if C > D:
  C, D = D, C
c, d = C, D
while True:
  c, d = d%c, c
  if c == 0:
    break
E = C*D//d

r = (B-A+1)-(B//C-(A-1)//C)-(B//D-(A-1)//D)+(B//E-(A-1)//E)
print(r)

Solution (Python):
from fractions import gcd

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

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

_ac = (a-1) // c 
_bc = b // c 

_ad = (a-1) // d 
_bd = b // d 

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

m = (b-a+1) - ((_bc-_ac) + (_bd-_ad) - (_bcd-_acd))
print(m)

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

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

def num(n, c, d):
    dc = n//c
    dd = n//d
    dcd = n//(lcm(c, d))
    return n-dc-dd+dcd

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

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

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

Solution (Python):
A,B,C,D=list(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)

E=lcm(C,D)

A-=1

Cmin=A-A%C
Cmax=B-B%C
Dmin=A-A%D
Dmax=B-B%D
Emin=A-A%E
Emax=B-B%E

num = ((Cmax-Cmin)//C)+((Dmax-Dmin)//D)-((Emax-Emin)//E)



print(int(B-A-num))

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

def lcm(C, D):
    g = gcd(C, D)
    G = D * C // g
    return G

def num(A, C, D):
    G = lcm(C, D)
    return A - A // C - A // D + A // G

print("{}".format(num(B, C, D) - num(A-1, C, D)))

Solution (Python):
def euclideanAlgo(a,b):
    cond = True
    while cond:
        mod = a%b
        if mod == 0:
            cond = False
            return(b)
        a = b
        b = mod

a,b,c,d = map(int, input().split())
e = (c*d)//euclideanAlgo(c,d)
ALL = b-a+1
c_kosuu = b//c-(a-1)//c
d_kosuu = b//d-(a-1)//d
e_kosuu = b//e-(a-1)//e
print(ALL -c_kosuu - d_kosuu +e_kosuu)

Solution (Python):
from fractions import gcd
a,b,c,d = map(int, input().split())
if a % c == 0:
    div_c = b//c - a//c +1
else:
    div_c = b//c - a//c 
if a % d == 0:
    div_d = b//d - a//d + 1
else:
    div_d = b//d - a//d
x = c*d // gcd(c,d)
if a % x == 0:
    div_cd = b//x - a//x + 1
else:
    div_cd = b//x - a//x
print(b-a+1 - div_c -div_d + div_cd)

Solution (Python):
A, B, C, D = 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)
    
lcm = C*D//gcd(C,D)

bDivC = B // C
bDivD = B // D
bDivCD = B // lcm
numBDividable = bDivC + bDivD - bDivCD
numB = B - numBDividable

aDivC = (A-1) // C
aDivD = (A-1) // D
aDivCD = (A-1) // lcm
numADividable = aDivC + aDivD - aDivCD
numA = A-1 - numADividable

answer = numB - numA
print(answer)

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

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

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

result= B//C+B//D-B//lsm(C,D) -((A-1)//C+(A-1)//D-(A-1)//lsm(C,D))
print(B-A+1-result)

Solution (Python):

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

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

large_c = b // c
large_d = b // d
large_cd = b // ((c * d) // gcd)
large = large_c + large_d - large_cd

small_c = (a - 1) // c
small_d = (a - 1) // d
small_cd = (a - 1) // ((c * d) // gcd)
small = small_c + small_d - small_cd

print(int((b - (a - 1)) - (large - small)))

Solution (Python):
import fractions

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

f = fractions.gcd(C,D)
f_1 = C*D//f

ans_1 = B-(B//C + B//D - B//f_1)
ans_2 = (A-1)-((A-1)//C + (A-1)//D - (A-1)//f_1)
print(ans_1-ans_2)

Solution (Python):
from fractions import gcd

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

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

print(ans)

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

Solution (Python):
import fractions

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


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


one_to_b = B - ((B // C) + (B // D) - (B // lcm(C, D)))
one_to_a = A - 1 - ((((A - 1) // C) +
                     ((A - 1) // D) -
                     (A - 1) // lcm(C, D)))

print(one_to_b - one_to_a)

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

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

g = gcd(C, D) if C < D else gcd(D, C)
ansB = B-B//C-B//D+B//(C*D//g)
ansA = A - 1 - (A-1)//C - (A-1)//D + (A-1)//(C*D//g)
print(ansB-ansA)

Solution (Python):
import fractions
A, B, C, D = map(int, input().split())
n = B-A+1
c1 = n//C
d1 = n//D
cd = C*D//fractions.gcd(C, D)
cd1 = n//(cd)
if ((A+c1*C-1)%C + (B-(A+c1*C-1))) // C == 1:
    c1 += 1
if ((A+d1*D-1)%D + (B-(A+d1*D-1))) // D == 1:
    d1 += 1
if ((A+cd1*cd-1)%(cd) + (B-(A+cd1*cd-1))) // cd == 1:
    cd1 += 1
print(n-(c1+d1-cd1))

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

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

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

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

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

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


a, b, c, d = map(int, input().split())
u = b - a + 1
x = b // c - (a - 1) // c
y = b // d - (a - 1) // d
xy = b // lcm(c, d) - (a - 1) // lcm(c, d)
ans = u - x - y + xy
print(ans)

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

def gcd(x,y):

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


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

Bika = B - B//C -B//D + B//lcm(C,D)
Aika = A-1 - (A-1)//C -(A-1)//D + (A-1)//lcm(C,D)

print(Bika-Aika)

Solution (Python):
import fractions

inputs = [int(i) for i in input().split()]

a = inputs[0]
b = inputs[1]
c = inputs[2]
d = inputs[3]

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

l = lcm(c, d)

c1 = b // c
c2 = (a-1) // c
c3 = c1 - c2

d1 = b // d
d2 = (a-1) // d
d3 = d1 - d2

l1 = b // l
l2 = (a-1) // l
l3 = l1 - l2

ans = (b-a+1) - (c3 + d3 - l3)
print(ans)

Solution (Python):
A,B,C,D=map(int,input().split(" "))
if A<1 or B<A or B >10**18 or C<1 or  D > 10**9:
	print ('Illegal')
else:
	a,b=C,D
	if a < b:
		a, b = b, a
	while a % b != 0:
		a, b = b, a % b
	t=int(C*D/b)
	n=B-A+1-(B//C-(A-1)//C)-(B//D-(A-1)//D)+(B//t-(A-1)//t)
	print(n)

Solution (Python):
import fractions

def divcount(a,b,x):
    return b//x - (a-1)//x

a,b,c,d = [int(i) for i in input().split()]
print(b-a+1-(divcount(a,b,c)+divcount(a,b,d)-divcount(a,b,c*d//fractions.gcd(c,d))))

Solution (Python):
from fractions import gcd

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

adivc = A//C
if A%C != 0:
    adivc = A//C + 1
bdivc = B//C
countC = bdivc - adivc + 1

adivd = A//D
if A%D != 0:
    adivd = A//D + 1
bdivd = B//D
countD = bdivd - adivd + 1

lcm = (int)(C * D / gcd(C, D))
adivlcm = A // lcm
if A%lcm != 0:
    adivlcm = A // lcm + 1
bdivlcm = B // lcm
countlcm = bdivlcm - adivlcm + 1

print(B-A+1-(countC+countD-countlcm))

Solution (Python):
def gcd(x, y):
    while y != 0:
        x, y = y, x % y
    return x


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


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

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)


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

print(ans)

Solution (Python):
def gcd(a,b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b
def main():
    A,B,C,D=map(int,input().split())
    if A%C!=0:
        C_min=A-A%C+C
    else:
        C_min=A
    C_max=B-B%C
    if A%D!=0:
        D_min=A-A%D+D
    else:
        D_min=A
    D_max=B-B%D
    mod=C*D//gcd(C,D)
    if A%mod!=0:
        CD_min=A-A%mod+mod
    else:
        CD_min=A
    CD_max=B-B%mod
    C_sum=(C_max-C_min)//C
    D_sum=(D_max-D_min)//D
    CD_sum=(CD_max-CD_min)//mod
    sum= B-A+1-int(C_sum+D_sum-CD_sum+1)
    return sum
print(int(main()))

Solution (Python):
def n_mul(m, l, u):
    return u // m - (l-1) // m

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 = [int(i) for i in input().split()]
C = n_mul(c, a, b)
D = n_mul(d, a, b)
CD = n_mul(lcm(c, d), a, b)
ans = (b+1-a) - (C+D-CD)
print(ans)

Solution (Python):

def gcd(x, y):
    """Greatest Common Divisor: O(min(x,y,))"""
    if y == 0:
        return x
    return gcd(y, x % y)


def lcm(x, y):
    """Lowest Common Multiple O(min(x,y,))"""
    return x * y // gcd(x, y)


def div_count(x, l, r):
    """Count Divisors"""
    return r // x - (l - 1) // x



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


div_cd = div_count(c, a, b) + div_count(d, a, b) - div_count(lcm(c, d), a, b)

print(b - a + 1 - div_cd)

Solution (Python):

a,b,c,d=map(int,input().split())
cntC=b//c-(a-1)//c
cntD=b//d-(a-1)//d
c1,d1=c,d
if c1<d1:
    c1,d1=d1,c1
while c1%d1!=0:
    c1,d1=d1,c1%d1
d1=int(c*d/d1)
cntCandD=b//d1-(a-1)//d1
ans=int(b - a + 1 - cntC - cntD + cntCandD)
print(ans)

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


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


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

div_c = (b // c) - ((a - 1) // c)
div_d = (b // d) - ((a - 1) // d)
div_c_and_d = (b // lcm(c, d)) - ((a - 1) // lcm(c, d))
div_c_or_d = div_c + div_d - div_c_and_d
print((b - a + 1) - div_c_or_d)

Solution (Python):
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))

A, B, C, D = read_int()

def lcm(a, b):
    pa, pb = a, b
    if a > b:
        a, b = b, a
    while b % a != 0:
        a, b = b % a, a
    return pa * pb // a

def divi(a, b, m):
    ans = 0
    former = a // m
    latter = b // m
    ans = latter - former
    if a % m == 0:
        ans += 1
    return ans

def solve():
    return (B - A + 1) - (divi(A, B, C) + divi(A, B, D) - divi(A, B, lcm(C, D)))

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

Solution (Python):

'''Snippets for lcm.
Available functions:
- lcm: Compute least common multiple of a and b.
'''


def lcm(a: int, b: int) -> int:
    '''Compute least common multiple of a and b.
    Args:
        a: Int of number (greater than 0).
        b: Int of number (greater than 0).
    Returns:
        least common multiple.
    Landau notation: O(log n)
    See:
    https://gist.github.com/endolith/114336/eff2dc13535f139d0d6a2db68597fad2826b53c3
    https://docs.python.org/3/library/sys.html
    '''

    from sys import version_info

    if version_info.major == 3 and version_info.minor >= 5:
        from math import gcd
    else:
        from fractions import gcd

    return a * b // gcd(a, b)


def main():
    a, b, c, d = map(int, input().split())
    l = lcm(c, d)
    x = b // c - (a - 1) // c
    y = b // d - (a - 1) // d
    z = b // l - (a - 1) // l
    w = b - a + 1

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


if __name__ == '__main__':
    main()

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

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


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


def cnt(u, x, y):
    return u - (u // x + u // y - u // lcm(x, y))


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

ans = 0


ans += cnt(B, C, D)


ans -= cnt(A - 1, C, D)

print(ans)

Solution (Python):
intxt = input().split()
a = int(intxt[0])
b = int(intxt[1])
c = int(intxt[2])
d = int(intxt[3])

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

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

devided_c = b // c
devided_c -= (a - 1) // c
devided_d = b // d
devided_d -= (a - 1) // d
devided_cd = b // lcd
devided_cd -= (a - 1) // lcd

ans = (b - a + 1) - (devided_c + devided_d - devided_cd)
print(ans)

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

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

betweenAandB = B-A+1

cA = (A-1)//C
cB = B//C
cDiv = cB - cA

dA = (A-1)//D
dB = B//D
dDiv = dB - dA

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

lcmA = (A-1)//LCM
lcmB = B//LCM
lcmDiv = lcmB - lcmA

ans = betweenAandB - cDiv - dDiv + lcmDiv
print(ans)

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

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

Solution (Python):
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
A,B,C,D = input().split()
A = int(A)-1
s = lcm(int(C),int(D))




numa = int(A)//int(C) + int(A)//int(D) - int(A)//s




numb = int(B)//int(C) + int(B)//int(D) - int(B)//s

print(int(B)-int(A)-(numb - numa))

Solution (Python):
import fractions


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


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

cntC = b // c - (a - 1) // c
cntD = b // d - (a - 1) // d
cntCandD = b // lcm(c, d) - (a - 1) // lcm(c, d)


print(b - a + 1 - cntC - cntD + cntCandD)

Solution (Python):

import sys
import fractions

def solve(A: int, B: int, C: int, D: int):
    def can_div_by(x: int):
        return (B // x) - (A - 1) // x

    c = can_div_by(C)
    d = can_div_by(D)
    cd = can_div_by(C*D // fractions.gcd(C, D))
    return B - A + 1 - (c + d - cd)


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))  
    print(solve(A, B, C, D))

if __name__ == '__main__':
    main()

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

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

A, B, C, D = map(int, input().split())
L = C * D // gcd(C, D)
c = B // C - (A - 1) // C
d = B // D - (A - 1) // D
l = B // L - (A - 1) // L

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

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

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

def gcd(a,b):
    while b:
        a,b=b,a%b
    return a
L=C*D//gcd(C,D)
l=B//L-(A-1)//L
print(int(B-A+1-c-d+l))

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

Solution (Python):
import fractions

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

def count(s, f, n):
  return f//n - (s-1)//n

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

print( B-A+1 - ( count(A,B,C) + count(A,B,D) - count(A, B, lcm(C, D)) ) )

Solution (Python):
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
a,b,c,d = map(int,input().split())
import math
lcm = c*d//gcd(c,d)
a_1 = a-1
panish = ((b//c)+(b//d)-(b//lcm))-(((a_1//c)+(a_1//d))-(a_1//lcm))
print(b-a_1-panish)

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())
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
E = (C*D)//gcd(C,D)
Z = B//E - (A-1)//E
print(B-A-X-Y+Z+1)

Solution (Python):


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

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

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

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):
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 = map(int,raw_input('').split())
a = int(A[0])
b = int(A[1])
c = int(A[2])
d = int(A[3])
l = lcm(c,d)
ans = (b-a+1) - ((b/c) - (a-1)/c) - ((b/d) - (a-1)/d) + ((b/l) - (a-1)/l)
print(ans)

Solution (Python):
import fractions

numA, numB, numC, numD = map(int, input().split())

AsplitC = int((numA - 1) // numC)
BsplitC = int(numB // numC)
AsplitD = int((numA - 1) // numD)
BsplitD = int(numB // numD)
AsplitCandD = int((numA - 1) // int(numC * numD // fractions.gcd(numC, numD)))
BsplitCandD = int(numB // int(numC * numD // fractions.gcd(numC, numD)))

count = (numB - numA + 1) - ((BsplitC - AsplitC) + (BsplitD - AsplitD) - (BsplitCandD - AsplitCandD))

print(count)

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)

def g(X):
  return X - X//C - X//D + X//lcm(C,D)

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

ans = g(B) - g(A-1)
print(ans)

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):
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
 
 
def lcd(a, b):
    return a*b // gcd(a, b)
  
a, b, c, d = map(int, input().split())
sum1 = (a - 1) // c + (a - 1) // d - (a - 1) // lcd(c, d)
sum2 = b // c + b // d - b // lcd(c, d)
print(b - a  + 1 - (sum2 - sum1))

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


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


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

higher = b//c + b//d - b//lcd(c, d)
lower = (a-1)//c + (a-1)//d - (a-1)//lcd(c, d)

print((b-a+1)-higher+lower)

Solution (Python):
def calc_gcd(m,n):
    a=max(m,n)
    b=min(m,n)
    r=a%b
    while r>0:
        a=b
        b=r
        r=a%b
    return b

def calc_lcm(m,n):
    return m*n//calc_gcd(m,n)
    
A,B,C,D=map(int,input().split())
c_div=B//C-(A-1)//C
d_div=B//D-(A-1)//D
cd_div=B//calc_lcm(C,D)-(A-1)//calc_lcm(C,D)
print(B-A+1-c_div-d_div+cd_div)

Solution (Python):
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n
A, B, C, D = map(int, input().split())
g = C * D // gcd(C, D)
def calc(N):
    return N - (N // C + N // D - N // g)
print(calc(B) - calc(A-1))

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())
length=b-a+1
lcm = c * d // fractions.gcd(c, d)
tmp=int(length-b//c+(a-1)//c-b//d+(a-1)//d+b//lcm-(a-1)//lcm)
print(tmp)

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

def getAns(a, b, c):
  num_c = b // c - (a - 1) // c
  return num_c

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


num_c =getAns(a,b,c)



num_d =getAns(a,b,d)



cd = c * d // gcd(c,d)
num_cd = getAns(a,b,cd)


print(b - a + 1 - (num_c + num_d - num_cd))

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

Solution (Python):
import fractions

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




left_c = (a - 1) // c
right_c = b // c

c_count = right_c - left_c

left_d = (a - 1) // d
right_d = b // d

d_count = right_d - left_d


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

left_cd = (a - 1) // (lcm_cd)
right_cd = b // (lcm_cd)

cd_count = right_cd - left_cd

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

Solution (Python):
import fractions
A, B, C, D = map(int, input().split())
Max = B-A+1
CD = C*D//(fractions.gcd(C, D))
Ccount = B//C-A//C
Dcount = B//D-A//D
CDcount = B//CD-A//CD
if A % C == 0:
    Ccount += 1
if A % D == 0:
    Dcount += 1
if A % CD == 0:
    CDcount += 1
count = Max+CDcount-Ccount-Dcount
print(count)

Solution (Python):
A, B, C, D=(int(x) for x in input().split(" "))
import fractions

def eks(x,a):
    return a//x

def wai(x):
    return eks(x,B)-eks(x,A-1)

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

zet=B-A+1-(wai(C)+wai(D)-wai(lcm(C,D)))
print (zet)

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

Solution (Python):
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return(a)
A,B,C,D=map(int,input().split())
CD=C*D//gcd(C,D)
print((B-A+1-(B//C-(A-1)//C)-(B//D-(A-1)//D)+(B//CD-(A-1)//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())
cn1=0
cn2=0
cn3=0
an1=0
an2=0
an3=0
cnt=0
ans=0
lm=(c*d)//gcd(c,d)


cn1=b//c
cn2=b//d
cn3=b//lm

an1=(a-1)//c
an2=(a-1)//d
an3=(a-1)//lm

cn1=cn1-an1
cn2=cn2-an2
cn3=cn3-an3

cnt=cn1+cn2-cn3
ans=b-a-cnt

print(ans+1)

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

a,b,c,d=map(int,input().split())
cn1=0
cn2=0
cn3=0
an1=0
an2=0
an3=0
cnt=0
ans=0
lm=(c*d)//gcd(c,d)


cn1=b//c
cn2=b//d
cn3=b//lm

an1=(a-1)//c
an2=(a-1)//d
an3=(a-1)//lm

cn1=cn1-an1
cn2=cn2-an2
cn3=cn3-an3

cnt=cn1+cn2-cn3
ans=b-a-cnt+1

print(ans)

Solution (Python):
def saidaikouyaku(a, b):
    if b > a:
        a, b = b, a

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

A, B, C, D = map(int, input().split())
hani = B - A + 1
Cnum = B // C - (A - 1) // C
Dnum = B // D - (A - 1) // D
saisyoukoubai = C * D // saidaikouyaku(C, D)
koubainum = B // saisyoukoubai - int(A - 1) // saisyoukoubai


print(hani - Cnum - Dnum + koubainum)

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

Solution (Python):
import fractions

if __name__ == '__main__':
  a, b, c, d = map(int, input().split(" "))
  c_num = b // c - (a-1) // c
  d_num = b // d - (a-1) // d
  lcm = (c * d) // fractions.gcd(c, d)
  cd_num = b // lcm - (a-1) // lcm
  print(b - a  + 1 - (c_num + d_num - cd_num))

Solution (Python):
import fractions

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


a,b,c,d = map(int,input().split(' '))
not_a_1 =0
not_b = 0
not_b =  b-(b//c)-(b//d)+(b//lcm(c,d))
not_a_1 = a-1-((a-1)//c)-((a-1)//d)+((a-1)//lcm(c,d))

print(not_b-not_a_1)

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

countC = (B // C) - (A // C)
countD = (B // D) - (A // D)
def lcm(x, y):
    return (x * y) // frac.gcd(x, y)
lcmCD = lcm(C, D)

countlcm = (B // lcmCD) - (A // lcmCD) 

num = B - A + 1
if A % C == 0:
  countC += 1
if A % D == 0:
  countD += 1
if A % lcmCD == 0:
  countlcm += 1

print(num - countC - countD + countlcm)

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

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

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

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

c_m = a // c
c_M = b // c
if a % c != 0:
              c_m += 1

d_m = a // d
d_M = b // d
if a % d != 0:
              d_m += 1

if c > d:
              q = c
              r = d
else:
              q = d
              r = c

while r != 1 and r != 0:
              q_next = r
              r_next = q%r
              q = q_next
              r = r_next          
gcd = 0
if r == 1:
              gcd = 1
if r == 0:
              gcd = q

lcm =(c*d)//gcd
cd_m = a // (lcm)
cd_M = b // (lcm)
if a % ((c*d)//gcd) !=0:
              cd_m += 1

cd_cnt=(cd_M-cd_m+1)

dim = (c_M-c_m+1) + (d_M-d_m+1) - (cd_cnt)
ans = b - a + 1 - dim
print(int(ans))

Solution (Python):
import fractions

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

r = A % C
min_c = A if r == 0 else A + C - r
r = A % D
min_d = A if r == 0 else A + D - r
lcm = (C * D) // fractions.gcd(C, D)
r = A % lcm
min_cd = A if r == 0 else A + lcm - r

num_c = max(0, (B - min_c) // C) + (1 if min_c <= B else 0)
num_d = max(0, (B - min_d) // D) + (1 if min_d <= B else 0)
num_cd = max(0, (B - min_cd) // lcm) + (1 if min_cd <= B else 0)

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

Solution (Python):
import sys
from fractions import gcd
def input(): return sys.stdin.readline().strip()
a,b,c,d=map(int,input().split())
def count(start, end, x):
    return (end // x) - (start // x + (1 if start % x else 0)) + 1
print(b-a+1-(count(a,b,c) + count(a,b,d) - count(a,b,c*d//gcd(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_div_byCorD = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)
B_div_byCorD = B//C + B//D - B//lcm(C, D)


canbedivided = B_div_byCorD - A_div_byCorD

print(B-A+1 - canbedivided)

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

Solution (Python):
a, b, c, d = 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(a, b):
    return a * b // gcd(a, b)
def solve(n):
    return n - n//c - n//d + n//lcm(c, d)

div_a = solve(a-1)
div_b = solve(b)
print(div_b - div_a)

Solution (Python):

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

def nev(u, C, D):
  l = C*D//gcd(C, D)
  ret = (l-(l//C+l//D)+1)*(u//l)
  m = u % l
  ret += m-(m//C+m//D)
  return ret

a = nev(B, C, D)
a -= nev(A-1, C, D) if A>1 else 0
print(a)

Solution (Python):
A,B,C,D=map(int, input().split())
L=D
M=C
if M < L:
    M, L = L, M
while M % L != 0:
    M, L = L, M % L
K=L
E=int((C*D)//K)
F=int(A//E)
G=int(B//E)
H=int(E//C+E//D-1)
I=int((E*(F+1)-A)//C+(E*(F+1)-A)//D)
J=int((B-(E*G))//C+(B-(E*G))//D)
if A%C==0 and A%D==0:
    print(B-A-((G-F-1)*H+I+J-1))
else:
    print(B-A-((G-F-1)*H+I+J))

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

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

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

A,B,C,D = (int(x) for x in input().split())
lcmCD = lcm(C, D)
A -= 1
ans = B - A
anstmp = B // C
anstmp -= A // C
ans -= anstmp
anstmp = B // D
anstmp -= A // D
ans -= anstmp
anstmp = B // lcmCD
anstmp -= A // lcmCD
ans += anstmp
print('%d' % ans)

Solution (Python):
import fractions

A,B,C,D = input().strip().split(" ")
A = int(A)
B = int(B)
C = int(C)
D = int(D)
X = C*D // fractions.gcd(C,D)

c = B//C - A//C + int(A%C == 0)
d = B//D - A//D + int(A%D == 0)
cd = B//X - A//X + int(A%X == 0)

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

Solution (Python):
from fractions import gcd

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

c = (B // C) - (A // C) + int(A % C == 0)
d = (B // D) - (A // D) + int(A % D == 0)
lcm_cd = (C * D) // gcd(C, D)
cd = (B // (lcm_cd)) - (A // (lcm_cd)) + int(A % (lcm_cd) == 0)

c_or_d = c + d - cd

print((B - A + 1) - c_or_d)