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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()


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


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

def main():
  a,b,c,d=LI()
  ans=b-a+1
  ans-=b//c+(-a//c)+1
  ans-=b//d+(-a//d)+1
  ans+=b//int(lcm(c,d))+(-a//int(lcm(c,d)))+1
  return ans


print(main())

Solution (Python):
def find_saidaikouyakusu(n1,n2):
    shou = n1//n2
    amari = n1%n2
    if amari == 0:
        return n2
    else:
        return find_saidaikouyakusu(n2,amari)



def find_saishoukoubaisu(n1,n2):
    return (n1*n2)//find_saidaikouyakusu(n1,n2) 



a,b,c,d = map(int,input().split())
s = find_saishoukoubaisu(d,c)
ans = (b-a+1) - ((b//c - (a-1)//c) + (b//d - (a-1)//d) - (b//s - (a-1)//s))
print(ans)

Solution (Python):

import fractions

a,b,c,d = map(int, input().split())
t = b - a + 1
dc = (b // c) - (a // c) if a % c != 0 else (b // c) - (a // c) + 1
dd = (b // d) - (a // d) if a % d != 0 else (b // d) - (a // d) + 1
lc = c * d // fractions.gcd(c, d)
dlc = (b // lc) - (a // lc) if a % lc != 0 else (b // lc) - (a // lc) + 1
ans = t - dc - dd + dlc
print(ans)

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

f1=fractions.gcd(c,d)
f2=c*d//f1

a=a-1
aa=a-a//c-a//d+a//f2
bb=b-b//c-b//d+b//f2

print(bb-aa)

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

all_pattern = B - (A - 1)
mul_c = B // C - (A - 1) // C
mul_d = B // D - (A - 1) // D
l = C * D // fractions.gcd(C, D)
mul_gcd = B // l - (A - 1) // l

res = all_pattern - mul_c - mul_d + mul_gcd
print(res)

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())
r = 0
bc = b // c
bd = b // d
bcd = b // lcm(c, d)
ac = -(-a // c)
ad = -(-a // d)
acd = -(-a // lcm(c, d))
print(b - a + 1 - bc + ac - 1 - bd + ad - 1 + bcd - acd + 1)

Solution (Python):
import fractions

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

    c_cnt = (B // C) - ((A-1) // C)
    d_cnt = (B // D) - ((A-1) // D)

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

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

    print(B - (A-1) - c_cnt - d_cnt + cd_cnt)


    
if __name__ == "__main__":
    main()

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

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

divc=(b//c) - ((a-1)//c)
divd=(b//d) - ((a-1)//d)
divcd=(b//lcm) - ((a-1)//lcm)
print((b-a+1)-(divc+divd-divcd))

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

c = (B // C) - ((A-1) // C)
d = (B // D) - ((A-1) // D)
lcm = C * D // gcd(C,D)
cd = (B // lcm) - ((A-1) // lcm)
print((B-A+1)-(c + d - cd))

Solution (Python):
A,B,C,D=map(int, input().split())
f=C
g=D
while f%g!=0:
  f,g=[g, f%g]
l=C*D//g
m=B//l
n=(A-1)//l
print(B-(B//C)-(B//D)+m-(A-1-((A-1)//C)-((A-1)//D)+n))

Solution (Python):

A,B,C,D=(int(x) for x in input().split(" "))
def gcd(x,y): 
  
   while(y): 
        x, y = y, x % y 
  
   return x 
def nb_divisers(n,m):
    return n//m
def nb_d_btw(f,g,n):
    if f%n==0:
        return nb_divisers(g,n)-nb_divisers(f,n)+1
    else:
        return nb_divisers(g,n)-nb_divisers(f,n)
def nb_notdivisers_f(a,b,c,d):
    e=int(c*d/gcd(c,d))
    return b-a+1-(nb_d_btw(a,b,c)+nb_d_btw(a,b,d)-nb_d_btw(a,b,e))
print(nb_notdivisers_f(A,B,C,D))

Solution (Python):

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


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

cnt_a = a-1

cnt_b = b

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

Solution (Python):
a,b,c,d=map(int,input().split())
def Euclid(x,y):
    while x != 0:
        z=x
        x=y%x
        y=z
    return y
l = c*d//(Euclid(c,d))
print((b- b//c - b//d + b//l) - ((a-1)- (a-1)//c - (a-1)//d + (a-1)//l))

Solution (Python):
import fractions

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

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

print(B - A + 1 - (B// C - (A-1)// C) - (B// D - (A-1)// D) + (B // lcm - (A-1) // lcm))

Solution (Python):
def lcm(a,b):
    x,y=a,b
    while b:
        a,b=b,a%b
    return x*y//a
X=[]
A=list(map(int, input().split()))
A.append(lcm(A[2],A[3]))
for i in range(3):
    if A[0]%A[i+2]==0:
        X.append(int(A[1]//A[i+2])-int(A[0]//A[i+2])+1)
    else:
        X.append(int(A[1]//A[i+2])-int(A[0]//A[i+2]))
print(A[1]-A[0]+1-X[0]-X[1]+X[2])

Solution (Python):
a,b,c,d=map(int,input().split())
def Euclid(x,y):
    while x != 0:
        z=x
        x=y%x
        y=z
    return y
l = c*d//(Euclid(c,d))
print((b- b//c - b//d + b//l) - ((a-1)- (a-1)//c - (a-1)//d + (a-1)//l))

Solution (Python):

import sys
from fractions import gcd

def f(a, x):
    """1以上a以下で、xで割り切れるものの数"""
    return a // x

def lcm(a, b):
    """ aとbの最小公倍数を返す """
    return a*b // gcd(a, b)

def solve():
    A, B, C, D = list(map(int, sys.stdin.readline().split()))
    A -= 1

    
    b = B - f(B, C) - f(B, D) + f(B, lcm(C, D))
    a = A - f(A, C) - f(A, D) + f(A, lcm(C, D))
    print(b-a)


if __name__ == "__main__":
    solve()

Solution (Python):
def gcd(x,y):
    if x<y:
        x,y = y,x
    while y>0:
        x,y = y,x%y
    return x
A,B,C,D = map(int,input().split())
c1 = B//C
d1 = B//D
CD = C*D//gcd(C,D)
cd1 = B//CD
c2 = (A-1)//C
d2 = (A-1)//D
cd2 = (A-1)//CD
b = B-(c1+d1-cd1)
a = A-1-(c2+d2-cd2)
print(b-a)

Solution (Python):
A = list(map(int, input().split()))
import fractions
def lcm(x, y):
  return (x * y) // fractions.gcd(x, y)

count_c = 0
count_d = 0
count_cd = 0
cd = lcm(A[2], A[3])
count_c = A[1] // A[2] - (A[0]-1) // A[2]
count_d = A[1] // A[3] - (A[0]-1) // A[3]
count_cd= A[1] // cd - (A[0]-1) // cd

print(A[1] - A[0] + 1 - (count_c + count_d - count_cd))

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)

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

Solution (Python):
import fractions
a,b,c,d = map(int, input().split())
gcd = fractions.gcd(c,d)
lcm = c * d // gcd
num = [c,d,lcm]
l = b - a + 1
temp = []

for i in range(3):
  count = 0
  if l - num[i] >= 0:
    count += l // num[i]
    if l % num[i] != 0:
      if b % num[i] < (b - (l % num[i]) + 1) % num[i]:
        count += 1
  else:
    if b % num[i] < l:
        count += 1
  temp.append(count)

ans = l - (temp[0] + temp[1] - temp[2])
print(ans)

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


divc = b//c-(a-1)//c
divd = b//d-(a-1)//d

def gcd(s,t):
    while s%t!=0:
        tmp1 = s//t
        tmp2 = t
        t = s-tmp1*t
        s = tmp2
        
    return t
g = gcd(c,d)

l = c*d//g

divl = b//l-(a-1)//l

ans = b-a+1 -divc-divd + divl

print(ans)

Solution (Python):
import fractions
a,b,c,d = [int(i) for i in input().split()]
e = b - a + 1
lcm = c*d//fractions.gcd(c,d)

bc = b//c
bd = b//d
ac = (a-1)//c
ad = (a-1)//d
bcd = b//lcm
acd = (a-1)//lcm

f = (bc + bd - bcd) - (ac+ ad -acd)

print(e-f)

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

import math

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

mul_c = b // c - (a - 1) // c
mul_d = b // d - (a - 1) // d

gcd_c_d = gcd(c, d)
lcm_c_d = gcd_c_d * (c // gcd_c_d) * (d // gcd_c_d)
mul_c_d = b // lcm_c_d - (a - 1) // lcm_c_d

print(b - a + 1 - (mul_c + mul_d - mul_c_d))

Solution (Python):




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

    else:
        return gcd(y,x%y)



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

if C < D:
    t = C
    C = D
    D = t

max_ky = gcd(C,D)


lcm_c = C//max_ky
lcm_d = D//max_ky
lcm = max_ky * lcm_c * lcm_d

count_b = B - (B//C + B//D - B//lcm)
count_a = A - (A//C + A//D - A//lcm)
if(A%C != 0 and A%D != 0):
    count_a -= 1
ans = count_b - count_a

print(ans)

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)
def div(x):
  return x-(x//c+x//d-x//lcm(c,d))
a,b,c,d=map(int,input().split())
print(div(b)-div(a-1))

Solution (Python):
import fractions
A,B,C,D = list(map(int,input().split()))
l = (C*D)//fractions.gcd(C,D)
a = [(A-1)//C,(A-1)//D,(A-1)//l]

b = [B//C,B//D,B//l]

c = [b[0]-a[0],b[1]-a[1],b[2]-a[2]]
print(B-A+1-c[0]-c[1]+c[2])

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

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

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

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

def divnumber(c):
	ans=lenth//c
	rem1=a%c
	if rem1==0:rem1+=c
	rem2=lenth%c-1
	if rem1+rem2>=c:
		ans+=1
	return ans

print(lenth-divnumber(c)-divnumber(d)+divnumber(lcm(c,d)))

Solution (Python):
from fractions import gcd

def calcAns(n,x,y):
    return n - ((n//x) + (n//y) - (n//((x*y)//gcd(x,y))))

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

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

Solution (Python):
import fractions
a,b,c,d = map(int, input().split())
cnt=0
num_c = b//c - (a-1)//c
num_d = b//d - (a-1)//d
num_c_d = b//((c*d)//fractions.gcd(c,d))- (a-1)//((c*d)//fractions.gcd(c,d))
total = num_c + num_d - num_c_d
print(int((b-a+1) - total))

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

print(int(num_divb)-int(num_diva))

Solution (Python):
import fractions

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

def get_muls(n):
    return (b // n) - ((a - 1) // n)

res = b - (a - 1) - get_muls(c) - get_muls(d) + get_muls(c * d // fractions.gcd(c, d))
print(res)

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

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

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

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

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

def ans(num, x, y):
    return num - (num // x + num // y - num // lcm(x, y))

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

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

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

B_both = B//(CD)
B_C = B//C
B_D = B//D
first_term = B-(B_C+B_D-B_both)
A -= 1
A_both = A//(CD)
A_C = A//C
A_D = A//D
second_term = A-(A_C+A_D-A_both)

print(first_term-second_term)

Solution (Python):
def gcd(x, y):
    while y != 0:
        x, y = y, x % y
    return x
import math
A, B, C, D = (int(a) for a in input().split())
lcm = gcd(C, D)
a = B - A + 1
b = abs(B // C - (A - 1) // C)
c = abs(B // D - (A - 1) // D)
d = abs(B // ((C * D) // lcm) - (A - 1) // ((C * D) // lcm))
print(a - (b + c - d))

Solution (Python):
import fractions 

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

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

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

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

n = B - A + 1
cn = g(A, B, C)
dn = g(A, B, D)
cdn = g(A, B, lcm(C, D))

ans = n - (cn + dn - cdn)
print(ans)

Solution (Python):
import fractions 

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

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

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

ans = n - (cn + dn - cdn)
print(ans)

Solution (Python):
import fractions

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

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

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

ans = n - (cn + dn - cdn)
print(ans)

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

def gcd(c,d):
	if(c==0):
		return d
	else:
		return gcd(d%c,c)
prod = C*D
lcm = prod//gcd(C,D)

A-=1

AC=A//C
AD=A//D
BC=B//C
BD=B//D
ACD=A//lcm
BCD=B//lcm

print(B-A-BC-BD+BCD+AC+AD-ACD)

Solution (Python):
import fractions as math

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

a -= 1
cd = (c * d) // math.gcd(c, d)

a_d_c = a // c
b_d_c = b // c

a_d_d = a // d
b_d_d = b // d

a_d_cd = a // cd
b_d_cd = b // cd

b_d = b_d_c + b_d_d - b_d_cd
a_d = a_d_c + a_d_d - a_d_cd

ans =  b - b_d - a + a_d

print(ans)

Solution (Python):
a,b,c,d = map(int,input().split(" "))
def gcd(c,d):
	if(c==0):
		return d
	else:
		return gcd(d%c,c)
prod = c*d
lcm = prod//gcd(c,d)
a1=b//c - (a-1)//c
a2=b//d - (a-1)//d
a3=b//lcm-(a-1)//lcm
print(b-a+1-a1-a2+a3)

Solution (Python):
def f_c():
    import fractions
    a, b, c, d = map(int, input().split())
    c_n = b//c-a//c+1 if a%c==0 else b//c-a//c
    d_n = b//d-a//d+1 if a%d==0 else b//d-a//d
    cd = c*d//fractions.gcd(c, d)
    cd_n = b//cd-a//cd+1 if a%cd==0 else b//cd-a//cd
    print(b-a+1-(c_n+d_n-cd_n))

if __name__ == "__main__":
    f_c()

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

lcm = int(C*D / fractions.gcd(C,D))
ans_b = B - (B // C) - (B // D) + (B // lcm)
ans_a = A - 1 - ((A - 1)// C) - ((A - 1)// D) + ((A - 1) // lcm)

print(ans_b - ans_a)

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

icm = int(C*D / fractions.gcd(C,D))
ans = (B - A + 1)
if A != B:
    if A % C == 0:
        ans -= int(B // C - A // C) + 1
    else:
        ans -= int(B // C - A // C)
    if A % D == 0:
        ans -= int(B // D - A // D) + 1
    else:
        ans -= int(B // D - A // D)

    if A % icm == 0:
        ans += int(B // icm - A // icm) + 1
    else:
        ans += int(B // icm - A // icm)
    print(int(ans))
else:
    if A % C == 0 or A % D == 0:
        print(0)
    else:
        print(1)

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

lcm = int(C*D / fractions.gcd(C,D))
ans_b = B - (B // C) - (B // D) + (B // lcm)
ans_a = A - 1 - ((A - 1)// C) - ((A - 1)// D) + ((A - 1) // lcm)

print(int(ans_b - ans_a))

Solution (Python):
A, B, C, D, = input().split()
A, B, C, D, = int(A), int(B), int(C), int(D)
def lcm(a,b):
    m = a*b
    while a != 0 and b != 0:
        if a > b:
            a %= b
        else:
            b %= a
    return m // (a+b)
for_D_start = B // D
for_C_start = B // C
sum_start = for_C_start + for_D_start - B // lcm(C,D)
for_C_A = (A-1) // C
for_D_A = (A-1) // D
sum_A = for_C_A + for_D_A - (A-1) // lcm(C,D)
print(B - A + 1 - (sum_start - sum_A))

Solution (Python):
def nod(a, b):
    if a == 0:
        return b
    elif a > b:
        return nod(a % b, b)
    return nod(b % a, a)


a, b, c, d = map(int, input().split())
ac = a + (c - a % c) % c
ad = a + (d - a % d) % d
bc = b - b % c
bd = b - b % d
dc = c*d//nod(c,d)
acd=a + (dc - a % dc) % dc
bcd = b - b % dc
print(b-a+1-(max(0,(bc-ac)//c+1)+max(0,(bd-ad)//d+1)-max(0,(bcd-acd)//dc+1)))

Solution (Python):
import sys
from math import ceil
from operator import itemgetter as get

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

def iinput():
    return int(input())

def finput():
    return float(input())

def tinput():
    return input().split()

def rinput():
    return map(int, tinput())

def rlinput():
    return list(rinput())

def nod(a, b):
    while b: 
        a, b = b, a % b
    return a
 
def calc(n, c, d):
    return n - n // c - n // d + n // (c * d // nod(c, d))   


def main():
    a, b, c, d = rinput()
    print(calc(b, c, d) - calc(a - 1, c, d))
main()

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

Solution (Python):
import fractions
A,B,C,D = map(int,input().split())
all = B-A+1
X = fractions.gcd(C,D)
gC = B//C - ((A-1)//C)
gD = B//D - ((A-1)//D)
gcdcd = C*D//X
gCD = B//gcdcd - ((A-1)//gcdcd)
ans = all - (gC+gD -gCD)
print(ans)

Solution (Python):
import fractions
A,B,C,D = [int(i) for i in input().split()]
def lcm(a,b):
    return a*b // fractions.gcd(a,b)
lcm = lcm(C,D)
aa = ((B//C)-(1 if A%C != 0 else 0))-(A//C)+1
bb = ((B//D)-(1 if A%D != 0 else 0))-(A//D)+1
ll = ((B//lcm)-(1 if A%lcm != 0 else 0))-(A//lcm)+1
print(int((B-A+1)-(aa+bb-ll)))

Solution (Python):
import math



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

cd = lcm(c, d)
r = b - a + 1

min_c = a // c + (1 if a % c != 0 else 0)
max_c = b // c
min_d = a // d + (1 if a % d != 0 else 0)
max_d = b // d
min_cd = a // cd + (1 if a % cd != 0 else 0)
max_cd = b // cd

c_num = max_c - min_c + 1
d_num = max_d - min_d + 1
cd_num = max_cd - min_cd + 1

print(r - c_num - d_num + cd_num)

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

Solution (Python):



import fractions
a,b,c,d=[int(i) for i in input().split()]
c1=b//c-a//c
if a%c==0:
    c1+=1
d1=b//d-a//d
if a%d==0:
    d1+=1
cd1=b//(c*d//fractions.gcd(c,d))-a//(c*d//fractions.gcd(c,d))
if a%(c*d)==0:
    cd1+=1



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

Solution (Python):
from fractions import gcd
a, b, c, d = [int(x) for x in input().split(' ')]

num = b-a+1
e = b//c - (a-1)//c 
f = b//d - (a-1)//d
g = c*d//gcd(c, d)
print(num - e - f + b//g - (a-1)//g)

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

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

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

CD=int(C*D/gcd(C,D))
Ac=A if A%C==0 else A+(C-A%C)
Ad=A if A%D==0 else A+(D-A%D)
Acd=A if A%CD==0 else A+(CD-A%CD)

Rc=(B-Ac)//C+1 if Ac>=A and Ac<=B else 0
Rd=(B-Ad)//D+1 if Ad>=A and Ad<=B else 0
Rcd=(B-Acd)//CD+1 if Acd>=A and Acd<=B else 0

print(B-A+1-(Rc+Rd-Rcd))

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

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

def gcd(x, y):
    if y > x:
        x, y = y, x
    if y == 1:
        return 1
    elif y == 0:
        return x
    else:
        return gcd(y, x-y*(x//y))

def num_divide(p, q, r):
    if p % r == 0:
        rmin = p
    else:
        rmin = p + (r-p%r)
    rmax = q - q%r
    return (rmax-rmin)//r + 1

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    a, b, c, d = list(map(int, input().rstrip('\n').split()))
    import fractions
    lcm = c * d // fractions.gcd(c, d)
    print((b - a + 1) - ((b // c - (a - 1) // c) + (b // d - (a - 1) // d) - (b // lcm - (a - 1) // lcm)))


if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd


def solve(string):
    a, b, c, d = map(int, string.split())
    lcm = c * d // gcd(c, d)
    return str(b - a + 1 - (b // c - (a - 1) // c + b // d - (a - 1) // d - b // lcm +
                            (a - 1) // lcm))


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

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

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

Solution (Python):
from fractions import gcd

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


def lcb(x, y):
    return int((x * y) / gcd(x, y))


l = lcb(C, D)

A -= 1
AC = A // C
AD = A // D
ACD = A // l
x = A - AC - AD + ACD


BC = B // C
BD = B // D
BCD = B // l
y = B - BC - BD + BCD


ans = y - x
print(ans)

Solution (Python):
import fractions

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

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

if A%C == 0:
    C_div = B//C - A//C + 1
else:
    C_div = B//C - A//C

if A%D == 0:
    D_div = B//D - A//D + 1
else:
    D_div = B//D- A//D


if A%E == 0:
    CandD_div  = B//E - A//E + 1
else:
    CandD_div  = B//E- A//E


ans = B - A + 1 - (C_div + D_div - CandD_div)
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 f(num, c, d):
    return num - (num // c + num // d - num // int(c * d / gcd(c, d)))

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

Solution (Python):
def gcd(x, y):
    while y >= 1:
        x, y = y, x%y
    return x
def lcm(x,y):
    return int((x*y)/gcd(x,y))

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

lcm_cd = lcm(c, d)
a -= 1
result = int(b - a)
result -= b//c - a//c
result -= b//d - a//d
result += b//lcm_cd -a//lcm_cd

print(result)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def gcd(n,m):
    if m == 0:
        return n
    else:
        return gcd(m,n%m)
def lcm(n,m):
    return (n*m)//gcd(n,m)

a,b,c,d = LI()
i,j,k = (a-1)//c,(a-1)//d,(a-1)//lcm(c,d)
s,t,u = b//c,b//d,b//lcm(c,d)
x = b - s - t + u
y = (a-1) - i - j + k
print(x -y)

Solution (Python):
import math

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

cd = lcm(c,d)
r = b-a+1

min_c = a//c + (1 if a%c!=0 else 0)
max_c = b//c
min_d = a//d + (1 if a%d!=0 else 0)
max_d = b//d
min_cd = a//cd + (1 if a%cd!=0 else 0)
max_cd = b//cd

c_num = max_c-min_c+1
d_num = max_d-min_d+1
cd_num = max_cd-min_cd+1

print(r-c_num-d_num+cd_num)

Solution (Python):
import fractions
def CntMultiples(a, b, n):
    return b//n - a//n + int(a%n==0)

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

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

def f(x, y, z):
    cnt = y // z - x // z
    if x % z == 0:
        cnt += 1
    return cnt

div_c_count = f(a, b, c)
div_d_count = f(a, b, d)

import fractions
lcm = c * d // fractions.gcd(c, d)
cm_count = f(a, b, lcm)

print(all_count - (div_c_count + div_d_count - cm_count))

Solution (Python):
from fractions import gcd


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


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


a, b, c, d = map(int, input().split())
l = lcm(c, d)
print(b - a + 1 - (counter(b, c) - counter(a - 1, c) +
                   counter(b, d) - counter(a - 1, d) -
                   (counter(b, l) - counter(a - 1, l))))

Solution (Python):
A, B, C, D = map(int, input().split())
def gcd(a, b):
    while b: 
      a, b = b, a % b
    return a

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

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

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


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


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

n_c = b // c - (a - 1) // c
n_d = b // d - (a - 1) // d

e = lcm(c, d)
n_e = b // e - (a - 1) // e

print(b - a + 1 - (n_c + n_d - n_e))

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

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

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

Solution (Python):
from fractions import gcd
a,b,c,d = map(int,input().split())
lcm = c*d//gcd(c,d)
ac = (a-1)//c
ad = (a-1)//d
la = (a-1)//lcm
a_ = a-(ac+ad-la)
bc = b//c
bd = b//d
lb = b//lcm
b_ = b-(bc+bd-lb)
ans = b_ - a_ + 1
print(ans)

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

first = [c,d%c]
first.sort()
if d%c==0:
  cd = c
elif first[0]==1:
  cd = 1
else:
  while first[0]>=2:
    first[1] = first[1]%first[0]
    first.sort()
    if first[0]==0:
      cd = first[1]
    elif first[0]==1:
      cd = 1
lcm = c*d//cd
 

zen = b-a+1
if a%c==0:
  c_num = b//c - a//c + 1
else:
  c_num = b//c - a//c
  
if a%d==0:
  d_num = b//d - a//d + 1
else:
  d_num = b//d - a//d
  
if a%lcm==0:
  lcm_num = b//lcm - a//lcm + 1
else:
  lcm_num = b//lcm - a//lcm

ans = zen - c_num - d_num + lcm_num
print(ans)

Solution (Python):
from fractions import gcd


def wari(x, c, d):
    nc = x // c
    nd = x // d
    ncd = x // ((c * d) // gcd(c, d))
    return x - nc - nd + ncd


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

nb = wari(B, C, D)
na = wari(A - 1, C, D)
print(nb - na)

Solution (Python):
import fractions
n1, n2, n3, n4 = [int(i) for i in input().split()]

n5 = (n3 * n4) // fractions.gcd(n3, n4)

n6 = (n2 // n3) - ((n1 - 1) // n3)
n7 = (n2 // n4) - ((n1 - 1) // n4)
n8 = (n2 // n5) - ((n1 - 1) // n5)

r1 = (n2 - n1  + 1)- n6 - n7 + n8 

print(r1)

Solution (Python):

import numpy as np
import math
import copy
import re
import sys


inf = float("inf")
prime = 1

def check_print(check_flag, text, end="\n"):
  if check_flag:
    print(text, end=end)

def c2i(x):
    if str.isdecimal(x):
        return int(x)
    else:
        return x

def cout(*args):
  arglist = []
  for arg in args:
    if hasattr(arg, "__iter__"):
      arglist.extend(arg)
    else:
      arglist.append(arg)
      
  arglist = list(map(str, arglist))
  print(" ".join(arglist))



  
def count2(num):
  count=0
  while num%2==0:
    num/=2
    count+=1
  return count

def ketasum(num):
  nums = [int(s) for s in str(num)]
  return sum(nums)


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

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

def gcd3(lis): 
  ans = lis[0]
  for li in lis:
    ans = gcd(ans,li)

def lcm3(lis): 
  ans = lis[0]
  for li in lis:
    ans = lcm(ans,li)

def divisor(n): 
  i = 1
  table = []
  while i * i <= n:
    if n%i == 0:
      table.append(i)
      table.append(n//i)
    i += 1
  table = list(set(table))
  return table

def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(n)
  return table

def is_prime(n):
  for i in range(2, n + 1):
    if i * i > n:
      break
    if n % i == 0:
      return False
  return n != 1


"""
a = sorted(a)
a = sorted(a, reverse=True)

"""


"""
pattern = ""
result = re.search(pattern, s)

result = re.fullmatch(pattern, s)

result = re.finditer(pattern, s)
for res in result:
    print res.group()   
    print res.start()   
    print res.end()     
    print res.span()    
"""



"""

math.floor()

math.ceil()
"""


"""
n = int(input())


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

d=[]
for i in range(n):
  d[i]=int(input())


a = [list(map(int, input().split())) for i in range(n)]

s = input()
ss =  [int(si) for si in s]
"""


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

def anti_d(num):
  return num - (num//c + num//d - num//lcm(c,d))

ret = anti_d(b) - anti_d(a-1)
print(int(ret))

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())

c1=(b//c)-(-((-1*a)//c))+1
d1=(b//d)-(-((-1*a)//d))+1
cd=lcm(c, d)

cd1=(b//cd)-(-((-1*a)//cd))+1

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

Solution (Python):
import math
import copy
import fractions
import numpy as np



def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList



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


def LCM(x, y):
    return x // GCD(x, y) * y

num = getInputListInt()

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

c_modzero = b//c - (a-1)//c
d_modzero = b//d - (a-1)//d

cd = LCM(c, d)

cd_modzero = b//cd - (a-1)//cd

ans = b - a + 1 - (c_modzero + d_modzero - cd_modzero)

print(int(ans))

Solution (Python):
























































import copy







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


def LCM(x, y):
    return x // GCD(x, y) * y


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

S = B - A + 1

canC = B // C - (A - 1) // C
canD = B // D - (A - 1) // D
canCD = B // LCM(C, D) - (A - 1) // LCM(C, D)

print(S - (canC + canD) + canCD)

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

def cont(x):
    cnt1 = x//c 
    cnt2 = x//d
    ans = x - cnt1 - cnt2 + x//lcm
    return int(ans)

print(cont(b) - cont(a-1))