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(a, b):
    while b != 0:
        a, b = b, a % b
    return a

 


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


S = input().split()
A = int(S[0])
B = int(S[1])
C = int(S[2])
D = int(S[3])

E = lcm(C, D)

X = int(B//C)-int((A-1)//C)
Y = int(B//D)-int((A-1)//D)
Z = int(B//E)-int((A-1)//E)

print(B-A+1-X-Y+Z)

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

    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)
tmp = (B // C - (A - 1) // C) + (B // D - (A - 1) // D) - (B // lcm - (A - 1) // lcm)
output = B - A + 1 - tmp

print(output)

Solution (Python):
import fractions

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

cde = b // c - (a - 1) // c
dde = b // d - (a - 1)  // d
lde = b // lcm - (a - 1)  // lcm

print(b - a + 1 - cde - dde + lde)

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

c1 = min(C, D)
d1 = max(C, D)

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

lcm = c1 * d1 // gcd(d1, c1)

def count(x):
    return B // x - ((A - 1) // x)

s = count(c1)
t = count(d1)
u = count(lcm)
v = count(1)

print(v - s - t + u)

Solution (Python):
import fractions

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

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

A -= 1

lcm_cd = lcm(C, D)
a_by_c = A // C
a_by_d = A // D
b_by_c = B // C
b_by_d = B // D
a_by_cd = A // lcm_cd
b_by_cd = B // lcm_cd

toB = B - (b_by_c + b_by_d - b_by_cd)
toA = A - (a_by_c + a_by_d - a_by_cd)

print(toB - toA)

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

Solution (Python):
import sys
import math
from fractions import gcd

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

c1 = b // c - (a - 1) // c
d1 = b // d - (a - 1) // d
cd = (c * d) //gcd(c,d)
cd1 = b // cd - (a - 1) // cd



print((b - a + 1) - c1 - d1 + cd1)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b



def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2



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



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


def count(X, C, D):
    ans = X
    ans -= X // C
    ans -= X // D

    num = X // lcm(C, D)
    ans += num

    return ans


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

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

if __name__ == '__main__':
    main()

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

def lcm(C, D):
    return (C * D) // fractions.gcd(C, D)

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

print(function(B) - function(A - 1))

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

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

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

b_num = b//c + b//d -b//lcm(c,d)
a_num = (a-1)//c + (a-1)//d - (a-1)//lcm(c,d)
print(target - (b_num - a_num))

Solution (Python):
def main():
    a, b, c, d = map(int, input().split())
    ans = b - a + 1
    ans -= b // c - (a - 1) // c
    ans -= b // d - (a - 1) // d
    lcmcd = lcm(c, d)
    ans += b // lcmcd - (a - 1) // lcmcd
    print(ans)


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



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

main()

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

Solution (Python):
import fractions

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

def lcm(C,D):
  return C*D // fractions.gcd(C,D)
CD=lcm(C,D)

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

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

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

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

b_c = b//c
b_d = b//d
b_e = b//e
a_c = (a-1)//c
a_d = (a-1)//d
a_e = (a-1)//e

print(b-a+1 - (b_c - a_c) - (b_d - a_d) + (b_e - a_e))

Solution (Python):
a,b,c,d = map(int,input().split())
def gcd(x,y):
    while y:
        x, y = y, x%y
    return x
cd = int(c*d/gcd(c,d))
l = [c,d,cd]
count = 0
for i in range(3):
  if a%l[i] == 0:
    if i < 2:
      count += (1+len(range(a+1,b+1))//l[i])
    else:
      count -= (1+len(range(a+1,b+1))//l[i])
  else:
    e = a+l[i]-a%l[i]
    if e <= b:
      if i < 2:
        count += (1+len(range(e+1,b+1))//l[i])
      else:
        count -= (1+len(range(e+1,b+1))//l[i])
    
print(len(range(a,b+1))-count)

Solution (Python):
import fractions

A, B, C, D=map(int, input().split())
x=C*D//fractions.gcd(C, D)
p=B//C-(A-1)//C
q=B//D-(A-1)//D
r=B//x-(A-1)//x
print(int(B-(A-1)-(p+q-r)))

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):
from fractions import gcd
a, b, c, d = map(int, input().split())

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

def calc(x):
    return x - (x//c)-(x//d)+(x//e)

print(calc(b)-calc(a-1))

Solution (Python):
import sys
input = sys.stdin.readline
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 calc(x):
    return x-(x//c)-(x//d)+(x//lcm(c,d))

print(calc(b)-calc(a-1))

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

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

def calc(x):
    return x-(x//c)-(x//d)+(x//lcm)

print(calc(b)-calc(a-1))

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

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

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

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

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

Solution (Python):
def main():
  a, b, c, d = list(map(int, input().split(' ')))
  l = lcm(c, d) if c >= d else lcm(d, c)
  ans = (b - (b//c + b//d - b//l)) - ((a-1) - ((a-1)//c + (a-1)//d - (a-1)//l))
  print(ans)


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


def lcm(l, s):
  return l * s // gcd(l, s)


if __name__ == '__main__':
  main()

Solution (Python):
from fractions import gcd

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

def f(a, b, n):
  return b // n - (a - 1) // n

acc = 0
acc += f(a, b, c)
acc += f(a, b, d)
acc -= f(a, b, c * d // gcd(c, d))
print(b - a + 1 - acc)

Solution (Python):
a,b,c,d = map(int,input().split())
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
lc = c * d // (gcd(c,d))
total = b - a + 1
onlyc = (b//c) - (a-1)//c
onlyd = (b//d) - (a-1)//d
both = (b//lc) - (a-1)//lc

print(total - (onlyd + onlyc - both))

Solution (Python):
from fractions import gcd


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


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

div_c = b//c - (a-1)//c
div_d = b//d - (a-1)//d
cd = lcm(c, d)
div_cd = b//cd - (a-1)//cd

count -= div_c + div_d - div_cd
print(count)

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

Solution (Python):
def gcd(c, d):
  if c < d:
    tmp = d
    d = c
    c = tmp
  ret = c
  if d != 0:
    ret = gcd(d, c % d)
  return ret

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

def union_cardinality(a_cardi, b_cardi, ab_cardi):
  return a_cardi + b_cardi - ab_cardi

def count_cm(maxi, divi):
  return maxi // divi

def count_cm2(maxi, divi1, divi2):
  num1 = count_cm(maxi, divi1)
  num2 = count_cm(maxi, divi2)
  num12 = count_cm(maxi, lcm(divi1, divi2))
  return union_cardinality(num1, num2, num12)

def count_cm2_band(mini, maxi, divi1, divi2):
  return count_cm2(maxi, divi1, divi2) - count_cm2(mini - 1, divi1, divi2)

a, b, c, d = (int(x) for x in input().split())
print(b - a + 1 - count_cm2_band(a, b, c, d))

Solution (Python):
import math


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

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

A,B,C,D = map(int,input().split())
A = A - 1
l = lcm(D,C)
underA = A- A//C - A//D + A//l
underB = B- B//C - B//D + B//l
        
print(underB-underA)

Solution (Python):
import fractions

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


ans=b-a+1
c_m=b//c-(a-1)//c
d_m=b//d-(a-1)//d
cd=c*d//fractions.gcd(c,d)
cd_m=b//cd-(a-1)//cd
print(ans-c_m-d_m+cd_m)

Solution (Python):

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


c_div = (b // c) - ((a-1) // c)



d_div = (b // d) - ((a-1) // d)



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


div =  c_div  + d_div - cd_div

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

Solution (Python):
def main():
    A, B, C, D = map(int, input().split())
    if C <= D: p = gcd(C, D)
    else: p = gcd(D, C)
    lcm = C * D // p
    dA = (A-1) - (A-1) // C - (A-1) // D + (A-1) // lcm
    dB = B - B // C - B // D + B // lcm
    return dB - dA

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

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

Solution (Python):
import fractions

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


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

A-=1
Bbi =B//bi
Abi =A//bi
sa = Bbi -Abi


Bc = B//C
Bd = B//D

Ac = A//C
Ad = A//D

print(int( B-A - ((Bc-Ac) + (Bd-Ad) - sa)  ))

Solution (Python):
import fractions
A,B,C,D = list(map(int, input().split()))
a=(A-1)//C
b=B//C
c=(A-1)//D
d=B//D
g = C*D//fractions.gcd(C,D)
e=(A-1)//(g)
f=B//(g)

print(B-A+1 - (b-a) - (d-c) + (f-e))

Solution (Python):

import fractions


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


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


a1 = a - 1
div_a = (a1 // c) + (a1 // d) - (a1 // lcm(c, d))
div_b = (b // c) + (b // d) - (b // lcm(c, d))

print((b - a + 1) - (div_b - div_a))

Solution (Python):
from fractions import gcd 

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

total = B - A + 1

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

Cdiv = B // C - (A-1) // C
Ddiv = B // D - (A-1) // D
lcgdiv = B // (lcg) - (A-1) // (lcg)

print(total - Cdiv - Ddiv + lcgdiv)

Solution (Python):
a, b, c, d = list(map(int, input().split()))
def euqlid(x,y):
    if x < y :
        temp = x
        x = y
        y = temp
    while x % y != 0:
        temp = x
        x = y
        y = temp % y
    return y
def searchCount(p,q,k):
    min = 0
    max = 0
    if p % k == 0:
        min = p // k
    else:
        min = p//k + 1
    max = q//k
    return max - min + 1
lcm = euqlid(c,d)
gcd = int((c * d) / lcm)
print((b - a + 1) - searchCount(a,b,c) - searchCount(a,b,d) + searchCount(a,b,gcd))

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())
count=0
x=b-a+1
def cal(x):
    return x//c+x//d-x//(c*d//gcd(c,d))
print(b-a+1-(cal(b)-cal(a-1)))

Solution (Python):
import fractions


def solve():
    a, b, c, d = map(int, input().split())
    lcm = c * d // fractions.gcd(c, d)
    div_c = b // c - (a - 1) // c
    div_d = b // d - (a - 1) // d
    div_cd = b // lcm - (a - 1) // lcm
    return (b - a + 1) - div_c - div_d + div_cd


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

Solution (Python):
import math
import fractions

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

a_c = (a - 1) // c
b_c = b // c
a_d = (a - 1) // d
b_d = b // d

lcm = int(c * d / fractions.gcd(c, d))
a_cd = (a - 1) // lcm
b_cd = b // lcm

result = b - a + 1 - (b_c - a_c) - (b_d - a_d) + (b_cd - a_cd)
print(int(result))

Solution (Python):

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

def gcd(m,n):
    x = max(m,n)
    y = min(m,n)
    if x%y == 0:
        return y
    else:
        return gcd(x%y,y)

if A%C==0:
  ac=A//C-1
else:
  ac=A//C
bc=B//C

if A%D==0:
  ad=A//D-1
else:
  ad=A//D
bd=B//D

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

if A%CD==0:
  acd=A//CD-1
else:
  acd=A//CD
bcd=B//CD

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

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)



c_d = lcm(c, d)




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



print(ans)

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

Solution (Python):


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



def gcd(C, D):
    while D != 0:
        C, D = D, C % D
    return C


w = B // C
x = B // D
y = (A - 1) // C
z = (A - 1) // D
a = B // (C * D // gcd(C, D))
b = (A - 1) // ((C * D) // gcd(C, D))

ans = (B - A + 1) - ((w - y) + (x - z) - (a - b))
print(ans)

Solution (Python):
import fractions

entrada = list(map(int,input().split(" ")))

rangoMenor = entrada[0]
rangoMayor = entrada[1]
divisor1 = entrada[2]
divisor2= entrada[3]

multiplo = divisor2*divisor1// fractions.gcd(divisor1,divisor2)
cantidad = rangoMayor-rangoMenor
repetidos = rangoMayor//multiplo - rangoMenor//multiplo

if rangoMenor%divisor1==0:
	divisibles1 = rangoMayor//divisor1 +1 - rangoMenor//divisor1
else:
	divisibles1 = rangoMayor // divisor1 - rangoMenor//divisor1

divisibles2 = rangoMayor//divisor2 - rangoMenor//divisor2

resultado = cantidad -(divisibles1+divisibles2) +repetidos + 1
print(resultado)

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

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

def calc(n, c, d):
    lcm_num = n // lcm(c, d)
    c_num = n // c
    d_num = n // d
    return n - c_num - d_num + lcm_num

ret = calc(B, C, D) - calc(A-1, C, D)
print(ret)

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

total_num = B - A + 1

if A % C == 0:
    can_C_divided = B // C - A // C + 1
else:
    can_C_divided = B // C - A // C
if A % D == 0:
    can_D_divided = B // D - A // D + 1
else:
    can_D_divided = B // D - A // D
lcm = C * D // fractions.gcd(C, D)
if A % lcm == 0:
    can_CD_divided = B // lcm - A // lcm + 1
else:
    can_CD_divided = B // lcm - A // lcm

print(total_num - can_C_divided - can_D_divided + can_CD_divided)

Solution (Python):
import fractions

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

a -= 1


divC = b//c - a//c

divD = b//d - a//d

l = (c*d) // fractions.gcd(c,d)
divU= b//l - a//l
divSum = divC+divD-divU


print(b-a - divSum)

Solution (Python):
def gcd(A,B):
    if A==B:
        output=A
    n=max(A,B)
    m=min(A,B)
    r=100
    while r>0:
        r=n%m
        n=m
        m=r
    return n

def div(A,B,C):
    n1=A%C
    if n1==0:
        ans1=A//C
    else:
        ans1=A//C+1
    n2=B%C
    if n2==0:
        ans2=B//C
    else:
        ans2=(B-B%C)//C
    d=ans2-ans1+1
    if d>=1:
        ans3=d
    else:
        ans3=0
    return ans3

A,B,C,D=map(int,input().split())
num1=div(A,B,C)
num2=div(A,B,D)
n=gcd(C,D)
lcm=C*D//n
num3=div(A,B,lcm)
lastanswer=(B-A+1)-(num1+num2-num3)
print(lastanswer)

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


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

rng=b-a+1
x=b//c-(a-1)//c
y=b//d-(a-1)//d
z=b//e-(a-1)//e
print(rng-x-y+z)

Solution (Python):
from math import ceil
from fractions import gcd


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


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

A = max(b // c - ceil(a / c) + 1, 0)
B = max(b // d - ceil(a / d) + 1, 0)
C = max(b // lcm(c, d) - ceil(a / lcm(c, d)) + 1, 0)
print(max(b-a+1-(A+B-C), 0))

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

ce=b//c
if a<=ce*c and ce*c<=b:
    cs=1+((a-1)//c)
else:
    cs=ce+1

de=b//d
if a<=de*d and de*d<=b:
    ds=1+((a-1)//d)
else:
    ds=de+1

gg=(c*d)//gcd(c,d)
cde=b//gg
if a<=cde*gg and cde*gg<=b:
    cds=1+((a-1)//gg)
else:
    cds=cde+1


print(int(b-a-ce-de+cs+ds+cde-cds))

Solution (Python):
from sys import stdin
import fractions


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


def main():
    A, B, C, D = [int(x) for x in stdin.readline().rstrip().split()]

    C_D_lcm = lcm(C, D)

    z = B//C_D_lcm - A//C_D_lcm

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

    y = B//D - A//D

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


if __name__ == "__main__":
    main()

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

V = b - a + 1
ans = 0
if V != 1:
    if a%c != 0:
        c_cnt = len(range(a+(c - a%c),b+1,c))
    else:
        c_cnt = len(range(a,b+1,c))

    if a%d != 0:
        d_cnt = len(range(a+(d - a%d),b+1,d))
    else:
        d_cnt = len(range(a,b+1,d))

    cd_lcm = lcm(c,d)
    if a%(cd_lcm) != 0:
        cd_cnt = len(range(a+(cd_lcm - a%(cd_lcm)),b+1,cd_lcm))
    else:
        cd_cnt = len(range(a,b+1,cd_lcm))

    ans = V - (c_cnt + d_cnt - cd_cnt)
else:
    if a%c != 0 and a%d != 0:
        ans = 1
print(ans)

Solution (Python):
import fractions

def resolve():
    a,b,c,d = map(int,input().split())
 
    dividable_by_c = b // c - (a-1) // c
    dividable_by_d = b // d - (a-1) // d
    lcm = c * d // fractions.gcd(c,d)
    dividable_by_c_and_d = b // lcm - (a-1) // lcm
 
    result = (b - a + 1) - dividable_by_c - dividable_by_d + dividable_by_c_and_d
    print(result)
resolve()

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

Solution (Python):

from fractions import gcd


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

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

a = a-1


nb = b - b//c - b//d + b//lcm(c, d)
na = a - a//c - a//d + a//lcm(c, d)

print(nb - na)

Solution (Python):
import fractions
a,b,c,d = (int(i) for i in raw_input().split())

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

def get_num(n, c, d):
    num_of_c = n // c
    num_of_d = n // d
    num_of_cd = n // lcm(c,d)
    ret = n - num_of_c - num_of_d + num_of_cd
    return ret

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

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

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

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

def calc(ab, c, d):
    return ab - (ab//c + ab//d - ab//lcm(c,d))
  

num_a = calc(a, c, d)
num_b = calc(b, c, d)

print(num_b - num_a)

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

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

Solution (Python):
import fractions

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

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

Cm = B // C - (A - 1) // C
Dm = B // D - (A - 1) // D
CDm = B // CD - (A - 1) // CD

print(B + 1 - A - (Cm + Dm - CDm))

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

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


A_C = A // C
A_D = A // D
A_CD = A //(C*D // gcd(C,D))
countA = A - A_C - A_D + A_CD



B_C = B // C
B_D = B // D
B_CD = B //(C*D // gcd(C,D))
countB = B - B_C - B_D + B_CD


if A % C != 0 and A % D != 0 and A % C != 0 and A % D != 0:
    print(countB - countA + 1)
else:
    print(countB - countA)

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

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

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

    return (b - a + 1) - div

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

Solution (Python):
def main():
    tmp = list(map(int, input().split(" ")))
    a = tmp[0]
    b = tmp[1]
    c = tmp[2]
    d = tmp[3]

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


def gcd(c, d):
    big = max(c, d)
    small = min(c, d)
    if small == 0:
        return int(big)
    r = big % small
    return gcd(small, r)


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


def f(x, c, d):
    return x - (x//c) - (x//d) + (x // lcm(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=[int(x) for x in input().split()]

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


print(b-a+1-abletodiv)

Solution (Python):
a, b , c, d = map(int, input().split())
def num_cm(a, b, m):
    if a % m == 0:
        return (b//m) - (a//m) + 1 
    else:
        return (b//m) - (a//m)
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a    
lcm = (c * d) // gcd(c, d)
ans =  b - a + 1 - num_cm(a, b, c) - num_cm(a, b, d) + num_cm(a, b, lcm)
print(ans)

Solution (Python):
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)
 
a,b,c,d = map(int,input().split())
a -= 1

ba = b - b//c - b//d + b//lcm(c,d)
aa = a - a//c - a//d + a//lcm(c,d)
print(ba - aa)

Solution (Python):
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)
 
a,b,c,d = map(int,input().split())
a -= 1

ba = b - b//c - b//d + b//lcm(c,d)
aa = a - a//c - a//d + a//lcm(c,d)
print(ba - aa)

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)

if A%C == 0:
    start1 = A // C
else:
    start1 = A // C
    start1 += 1


if A%D == 0:
    start2 = A // D
else:
    start2 = A // D
    start2 += 1


tmp = lcm(C, D)


if A%(C*D) == 0:
    start3 = A // tmp
else:
    start3 = A // tmp
    start3 += 1

end1 = B // C
end2 = B // D
end3 = B // tmp


res = (B - A + 1) - (end1 - start1 + 1) - (end2 - start2 + 1) + (end3 - start3 + 1)


print(res)

Solution (Python):
from math import factorial, ceil, floor 
from operator import itemgetter as ig
from collections import defaultdict as dd

INF = float("inf")
MOD = int(1e9 + 7)

def gcd(v1, v2):
    if v2 == 0:
        return v1
    return gcd(v2, v1 % v2)
def lcm(v1, v2):
    return (v1 // gcd(v1, v2)) * v2


def main():
    A, B, C, D = map(int, input().split())
    def solve(v1, v2, d):
        return B // d - (A - 1) // d
    print((B - A + 1) - (solve(A, B, C) + solve(A, B, D) - solve(A, B, lcm(C, D))))

main()

Solution (Python):
import fractions
num = [int(i) for i in input().split()]
lower_limit = num[0]
upper_limit = num[1]
c = num[2]
d = num[3]

def count_multi(x):
  count = upper_limit//x- (lower_limit-1)//x                          
  return count

cd_gcd = fractions.gcd(c, d)

multi_c_d = count_multi(c) + count_multi(d) - count_multi(c*d//cd_gcd)
print(upper_limit - lower_limit + 1 - multi_c_d)

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

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

X = B//C - (A-1)//C
Y = B//D - (A-1)//D
CD = C*D // gcd(C, D)
XY = B//CD - (A-1)//CD

print(B-A+1-(X+Y-XY))

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

Solution (Python):
from fractions import gcd

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

def main():
    cd = c * d // gcd(c, d)
    print(b - a - f(c) - f(d) + f(cd))

def f(x):
    return b // x - a // x

main()

Solution (Python):
import fractions

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


g = fractions.gcd(c, d)


l = c * d // g



x = b - b // c - b // d + b // l


y = (a - 1) - (a - 1) // c - (a - 1) // d + (a - 1) // l
    
print(x - y)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)

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


def div(n,divn):
    
    return n//divn

def divor(n,div1,div2):
    
    return div(n,div1)+div(n,div2)-div(n,lcm(div1,div2))
def nondivor(n,div1,div2):
    
    return n-divor(n,div1,div2)


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


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

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

Solution (Python):
import fractions

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


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


def ans(x):
    can_div_c_count = x // c
    can_div_d_count = x // d
    can_div_lcm_count = x // lcm(c, d)
    return x - (can_div_c_count + can_div_d_count - can_div_lcm_count)


print(ans(b)-ans(a-1))

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

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

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

Solution (Python):
import math
import fractions
a,b,c,d = map(int, input().split())
e = a-1
less_than_e = e - (e//c) - (e//d) + (e//(c*d//fractions.gcd(c,d)))
less_than_b = b - (b//c) - (b//d) + (b//(c*d//fractions.gcd(c,d)))
if less_than_e < 0:
    less_than_e = 0
print(less_than_b - less_than_e)

Solution (Python):
import math

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



def f(x,y):
  z=int(x//y)
  return z


a,b,c,d=map(int,input().split())
g=gcd(c,d)
p=a-f(a,c)-f(a,d)+f(a,int(c*d/g))
q=b-f(b,c)-f(b,d)+f(b,int(c*d/g))
if(a%c!=0 and a%d!=0):
  print(q-p+1)
else:
  print(q-p)

Solution (Python):


a, b, c, d = [int(el) for el in 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))

def g(x, y):
    return(x // y + 1)

def f(x, y, z):
    return(x+1 - g(x, y) - g(x, z) + g(x, lcm(y, z)))

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

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

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

GcdCD = gcd(C,D)
LcmCD = (C*D)//GcdCD

s1 = B- ((B//C)+(B//D)-(B//LcmCD))
s2 = (A-1)-(((A-1)//C)+((A-1)//D)-((A-1)//LcmCD))
if s2 < 0:
    s2 = 0
print(s1-s2)

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

Solution (Python):


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


def count_multiple(A, B, x):
    """B と　A-1 までのxの倍数の数をかぞえる
    引けばA,B範囲内の倍数の数"""
    min_value = (A - 1) // x
    max_value = B // x
    
    return max_value - min_value


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


lcm = C * D // gcd(C, D)
cnt = 0
for i, x in enumerate([C, D, lcm]):
    if i < 2:
        cnt += count_multiple(A, B, x)
    else:
        cnt -= count_multiple(A, B, x)

total = B - (A - 1)
ans = total - cnt
print(ans)

Solution (Python):

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

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

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

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

b_cnt1 = b // c

b_cnt2 = b // d

b_cnt3 = b // lcm(c, d)

a_cnt1 = a // c

a_cnt2 = a // d

a_cnt3 = a // lcm(c, d)

print(b - (b_cnt1 + b_cnt2 - b_cnt3) - (a - (a_cnt1 + a_cnt2 - a_cnt3)))

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

Solution (Python):
import fractions

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

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

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

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

def lcm(n, h):
    return (n * h) // fractions.gcd(n, h)

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

print(w -(slove(b,c,d) - slove(a-1,c,d)) +1 )

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

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

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

def count(a, b, x):
    start = a + (a-a%x)%x
    end = b - b%x
    return max((end-start)//x + 1, 0)

A, B, C, D = map(int, input().split())
l = lcm(C, D)
ans = B - (A-1)
ans -= count(A, B, C) + count(A, B, D) - count(A, B, l)
print(ans)