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

from fractions import gcd

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


aa = a // c
if (a % c == 0):
    aa -= 1
bb = b // c
ans = bb - aa


aa = a // d
if (a % d == 0):
    aa -= 1
bb = b // d
ans += bb - aa

'''
Python3.5
>>
最大公約数
import math
z = math.gcd(a,b)
最小公倍数
z = (a * b)//math.gcd(a,b)

Python3.4
from fractions import gcd
'''

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

aa = a // z
if (a % z == 0):
    aa -= 1
bb = b // z
ans -= bb - aa

tar = b - a + 1 - ans
print(tar)

Solution (Python):
import fractions

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




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


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


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

print(int((b-a+1)-i-j+k))

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

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

def gcd(a,b):
    if b>a:
        a,b = b,a
    while b:
        a,b = b,a%b
    return a
def lcm(a,b):
    return a*b//gcd(a,b)
E = lcm(C,D)

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

print(R)

Solution (Python):

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


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


both=B//f2 - (A-1)//f2



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



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


print(B-A+1 -(Ccount+Dcount-both))

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

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

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

Solution (Python):
from fractions import gcd


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


def main():
    a, b, c, d = map(int, input().split())
    c_num = (b//c-(a-1)//c)
    d_num = (b//d-(a-1)//d)
    cd_num = (b//lcm(c, d)-(a-1)//lcm(c, d))
    ans = b-a+1-c_num-d_num+cd_num
    print(ans)


if __name__ == "__main__":
    main()

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

x=b-a+1
A=a-1
bclcm=(d*c)//fractions.gcd(c,d)
y=(b//c)+(b//d)-(b//(bclcm))
z=(A//c)+(A//d)-(A//(bclcm))

ans=x-y+z

print(int(ans))

Solution (Python):
from fractions import gcd
 
def main():
    a,b,c,d = map(int,input().split(" "))
    n = b - a + 1
    cdiv = b//c - (a-1)//c
    ddiv = b//d - (a-1)//d
    cddiv = b//(c * d // gcd(c, d))  - (a-1)//(c * d // gcd(c, d))
    print(n - cdiv -ddiv + cddiv)
 
if __name__ == '__main__':
    main()

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

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

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

def get_num(X):
    num_c = X // C
    num_d = X // D
    common = X // lcm
    return X - (num_c + num_d) + common


print(get_num(B) - get_num(A - 1))

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

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

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

sb1 = b // c
sb2 = b // d
sb3 = b // lcm(c,d)

b2 = b - sb1 - sb2 + sb3

sa1 = a // c
sa2 = a // d
sa3 = a // lcm(c,d)

a2 = a - sa1 - sa2 + sa3

print(b2-a2)

Solution (Python):
from fractions import gcd
from functools import reduce

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

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

tmp = B // C + B // D - A//C - A // D
if A%C == 0:
    tmp += 1
if A%D == 0:
    tmp += 1
lcm = lcm_base(C, D)
tmp -= (B//lcm - A//lcm)
if A%lcm == 0:
    tmp -= 1
print(B-A+1-tmp)

Solution (Python):
import sys

def lcm(a, b):
    a, b = max(a, b), min(a, b)
    c = a * b
    while a % b > 0:
        a, b = b, a % b
    return c // b

def solve():
    input = sys.stdin.readline
    A, B, C, D = map(int, input().split())
    divC = B // C - (A - 1) // C
    divD = B // D - (A - 1) // D
    cd = lcm(C, D)
    divCD = B // cd - (A - 1) // cd
    print(B - A + 1 - (divC + divD - divCD))


    return 0

if __name__ == "__main__":
    solve()

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

def calc(x,a,b,l):
  return x - (x//a) - (x//b) + (x//l)

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

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

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

Solution (Python):

from fractions import gcd

A, B, C, D = map(int, input().split())
X = B // C - (A - 1) // C
Y = B // D - (A - 1) // D
lcm = int(C / gcd(C, D) * D)
Z = B // lcm - (A - 1) // lcm
print(B - A + 1 - X - Y + Z)

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

    def anti_div(x, y, z):
        
        
        cnt = x
        cnt -= x // c
        cnt -= x // d
        
        cnt += x // lcd(c, d)
        return cnt
    
    
    ans = anti_div(b, c, d) - anti_div(a-1, c, d)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    A, B, C, D = map(int, input().split())
    num_C_div = B // C - (A - 1) // C
    num_D_div = B // D - (A - 1) // D
    CD_lcm = lcm(C, D)
    num_CandD_div = B // (CD_lcm) - (A - 1) // (CD_lcm)
    num_CorD_div = num_C_div + num_D_div - num_CandD_div
    print((B - A + 1) - num_CorD_div)


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


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


if __name__ == '__main__':
    main()

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


def count_div(N, C, D):

    t = N // C + N // D - N // (C * D // fractions.gcd(C, D))
    return N - t


def solve():

    A, B, C, D = map(int, input().split())
    ans = count_div(B, C, D) - count_div(A - 1, C, D)
    print(ans)


if __name__ == '__main__':
    solve()

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

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

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

from fractions import gcd
lcm = c * d // gcd(c, d)



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

print(b-a+1 - (b_num - a_num))

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

from fractions import gcd
lcm = c * d // gcd(c, d)


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

print(b-a+1 - (b_num - a_num))

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

from fractions import gcd

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

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

print(b-a+1 - (b_num - a_num))

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

Solution (Python):
import fractions


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



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

    sss = b//c - (a-1)//c
    ssss = b//d - (a-1)//d
    di = lcm(c,d)
    sssss = b // di - (a - 1) // di if di != 0 else 0
    print((b-a+1) - (sss+ssss-sssss))


if __name__ == '__main__':
    main2()

Solution (Python):
import math
import fractions

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

Cans = (B // C  + (-A // C)) + 1
Dans = (B // D  + (-A // D)) + 1

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

CDans = (B // CDlcm  + (-A // CDlcm)) + 1

ans = (B - A + 1) - (Cans + Dans - CDans)
print(ans)

Solution (Python):
import fractions

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


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


ans=B -(B//C+B//D-B//kobaisu)

ans2=(A-1)-((A-1)//C +(A-1)//D - (A-1)//kobaisu)

print(int(ans-ans2))

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

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

import fractions

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


def f(x, c, d):
    cd = lcm(c, d)
    
    divisible = x//c + x//d - x//cd
    
    return x - divisible

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

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)


def f(a,b,c):
	if a == 0:
		return b/c + 1
	return b/c  - (a-1)/c 

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

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

A, B, C, D = map(int, input().split())
count = 0
BCcount = B // C
BDcount = B // D
ACcount = (A - 1) // C
ADcount = (A - 1) // D
l = C * D // gcd(C, D)
BCDcount = B // l
ACDcount = (A - 1) // l
Bcount = Decimal(B - (BCcount + BDcount - BCDcount))
Acount = Decimal((A - 1) - (ACcount + ADcount - ACDcount))
print(Decimal(Bcount - Acount))

Solution (Python):
import fractions

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

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

gcd_cd = lcm(c,d)

a_c_numbers = a//c
if a % c == 0:
    a_c_numbers -= 1
a_d_numbers = a//d
if a % d == 0:
    a_d_numbers -= 1

a_numbers = a_c_numbers + a_d_numbers

b_c_numbers = b//c
b_d_numbers = b//d
b_numbers = b_c_numbers + b_d_numbers

if a % gcd_cd == 0:
    gcd_cd_numbers = b//gcd_cd - a//gcd_cd + 1
else:
    gcd_cd_numbers = b//gcd_cd - a//gcd_cd





ans = b - a + 1 - (b_numbers - a_numbers - gcd_cd_numbers)

if ans > 0:
    print(ans)
else:
    print(0)

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

Solution (Python):
from fractions import *
from math import *

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

a, b, c, d = map(int, input().split())
def f(x):
    xc = x // c
    xd = x // d
    cd = x // lcm(c, d)
    return x - xc - xd + cd

print(f(b) - f(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 lcm(x, y):
    return (x * y) // gcd(x, y)


C_num = B // C - A // C

D_num = B // D - A // D

lcm = lcm(C, D)
CD_num = B // lcm - A // lcm



tasu = 0
if (A % C == 0):
    tasu += 1
tasu += C_num


if (A % D == 0):
    tasu += 1
tasu += D_num


hiku = 0
if (A % lcm == 0):
    hiku += 1
hiku += CD_num

print((B - A + 1) - (tasu - hiku))

Solution (Python):
A, B, C, D = map(int, input().split())
total = B - A + 1
num_C = B // C - (A-1)//C
num_D = B // D - (A-1)//D

from fractions import gcd
g = gcd(C, D)
l = C * D // g
num_CD = B//l - (A-1)//l
ans = total - (num_C + num_D - num_CD)
print(ans)

Solution (Python):
import fractions
import sys
from functools import reduce
import math

input = sys.stdin.readline

def main():
    a, b, c, d = input_list()
    l = lcm_base(c, d)
    a -= 1
    ans_b = b - (b//c + b//d) + b//l
    ans_a = a - (a//c + a//d) + a//l
    print(ans_b - ans_a)


def divide_list(l, args):
    result = []
    pre = 0
    for i, a in enumerate(args):
        if i != 0:
            pre = args[i-1]
        result.append(l[pre:a])
        if i == len(args) - 1:
            result.append(l[a:])
    return result

def divide_two(n):
    c = 0
    while True:
        if c >= 2:
            break
        if n % 2 != 0:
            break
        n //= 2
        c += 1
    return c

def get_camulative(l):
    import itertools
    
    return [0] + list(itertools.accumulate(l))

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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


def input_list_str():
    return list(map(str, input().split()))


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


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def gcd(*numbers):
    return reduce(fractions.gcd, numbers)


def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


if __name__ == "__main__":
    main()

Solution (Python):
from fractions import gcd
import math

def lcm(x, y):
    return (x * y) // gcd(x, y)
a,b,c,d = map(int,input().split())
all = b - a + 1
if c == 1 or d == 1:
    print(0)
    exit()

cd = lcm(c, d)

cs = b//c - math.ceil(a/c) + 1
ds = b//d - math.ceil(a/d) + 1
cds = b//cd - math.ceil(a/cd) + 1

print(all - cs - ds + cds)

Solution (Python):
from fractions import gcd
a,b,c,d = map(int,input().split())
n = (a-1)//c 
m = b//c 
ck = m - n 

o = (a-1)//d 
p = b//d 
dk = p - o 


e = (c * d) // gcd(c, d) 
q = (a-1)//e 
r = b//e 
ek = r - q 


cord = ck + dk - ek

ans = b - a + 1 - cord
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
import fractions
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

INF = float("inf")












a,b,c,d=map(int,input().split())
e=fractions.gcd(c,d)
f=c*d//e
ans1=a-1
ans1-=(a-1)//c
ans1-=(a-1)//d
ans1+=(a-1)//f
ans2=b
ans2-=b//c
ans2-=b//d
ans2+=b//f
print(ans2-ans1)

Solution (Python):
from sys import stdin, setrecursionlimit


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


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


def main():
    input = stdin.buffer.readline
    a, b, c, d = map(int, input().split())
    l = lcm(c, d)
    ans = 0
    ans += (l - (l // c + l // d - 1)) * (b // l - (a - 1)//l)
    tmp_b = b % l
    ans += tmp_b - (tmp_b // c + tmp_b // d)
    tmp_a = (a - 1) % l
    ans -= tmp_a - (tmp_a // c + tmp_a // d)
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
import fractions

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



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

if a % c == 0:
    csyou = (b//c) - (a//c) +1
else:
    csyou = (b//c) - (a//c)
    
if a % d == 0:
    dsyou = (b//d) - (a//d) +1
else:
    dsyou = (b//d) - (a//d)
    
if a % lcm == 0:
    lcmsyou = (b//lcm) - (a//lcm)+1
else:
    lcmsyou = (b//lcm) - (a//lcm)

print((b-a+1)-(csyou+dsyou-lcmsyou))

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

Solution (Python):
a, b, c, d = map(int, input().split())
from fractions import gcd
xc = (a-1)//c
yc = b//c
xd = (a-1)//d
yd = b//d    
xcd = (a - 1) // ((c * d) // gcd(c, d))
ycd = b // ((c * d) // gcd(c, d))

print(b - a + 1 -((yc - xc) + (yd - xd) - (ycd - xcd)))

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

Solution (Python):
import fractions as fra

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

e = c * d // fra.gcd(c,d)
x = b//c + b//d - (a-1)//c - (a-1)//d
y = b//e - (a-1)//e

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

Solution (Python):
import math
import fractions

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

count = 0
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

if A % lcm == 0:    
    div_C_D = B // lcm - A // lcm + 1
else:
    div_C_D = B // lcm - A // lcm

print(B - A + 1 - div_C - div_D + div_C_D)

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

print(B - A)

Solution (Python):
def gcd(a, b):
    a, b = sorted([a, b], reverse=True)
    while(b != 0):
        a, b = (b, a%b)
    return a

def getMultiCount(start, end, times):
    if end < times: return 0
    s = start-(start%times)
    if s != start: s += times
    e = end-(end%times)
    if s > e: return 0
    return ((e-s)//times)+1
    
def answer(a, b, c, d):
    cCount = getMultiCount(a, b, c)
    dCount = getMultiCount(a, b, d)
    cdCount = getMultiCount(a, b, c*d//gcd(c,d))
    return (b-a+1)-(cCount + dCount - cdCount)

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

Solution (Python):
from fractions import gcd







def getMultiCount(start, end, times):
    if end < times: return 0
    s = start-(start%times)
    if s != start: s += times
    e = end-(end%times)
    if s > e: return 0
    return ((e-s)//times)+1
    
def answer(a, b, c, d):
    cCount = getMultiCount(a, b, c)
    dCount = getMultiCount(a, b, d)
    cdCount = getMultiCount(a, b, c*d//gcd(c,d))
    return (b-a+1)-(cCount + dCount - cdCount)

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

Solution (Python):



def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y) 
def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


from functools import reduce
import fractions 
import sys
input = sys.stdin.readline
A,B,C,D = (int(x) for x in input().split())

c_multi_B =B // C 
d_multi_B =B // D 
least_common_multi =lcm(C,D)
cd_common_multi_B =B //least_common_multi

c_multi_A =(A -1) // C 
d_multi_A =(A -1) // D 
least_common_multi =lcm(C,D)
cd_common_multi_A =(A -1)//least_common_multi


all =B -A +1

ans =all - (c_multi_B -(c_multi_A )) -(d_multi_B -(d_multi_A )) + cd_common_multi_B -cd_common_multi_A
print(ans)

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)

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

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

Solution (Python):
import fractions

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

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

div_c = (B // C) - (A // C)
if A // C != 0 and A % C == 0:
    div_c += 1
div_d = (B // D) - (A // D)
if A // D != 0 and A % D == 0:
    div_d += 1
div_cd = (B // (lcm)) - (A // (lcm))
if A // (lcm) != 0 and A % lcm == 0:
    div_cd += 1

ans = (B - A + 1) - (div_c + div_d - div_cd)
print(ans)

Solution (Python):
import fractions

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

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

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)

cd_lcm = lcm(C, D)


C_onetoB = B//C
C_onetoA = (A-1)//C
C_div = C_onetoB - C_onetoA


D_onetoB = B//D
D_onetoA = (A-1)//D
D_div = D_onetoB - D_onetoA


cd_lcm_onetoB = B//cd_lcm
cd_lcm_onetoA = (A-1)//cd_lcm
cd_lcm_div = cd_lcm_onetoB - cd_lcm_onetoA

print(B-A+1 - C_div - D_div + cd_lcm_div)

Solution (Python):
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *

from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction



mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]






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


a,b,c,d=l()
ans=0

ans+=(b//c-a//c+(a%c==0))

ans+=(b//d-a//d+(a%d==0))

lcm=c*d//G(c,d)
ans-=(b//lcm-a//lcm+(a%lcm==0))
print(b-a+1-ans)

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


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

ans+=(b//c-a//c+(a%c==0))

ans+=(b//d-a//d+(a%d==0))

lcm=c*d//G(c,d)
ans-=(b//lcm-a//lcm+(a%lcm==0))
print(b-a+1-ans)

Solution (Python):
from fractions import gcd

def main():
    A, B, C, D = map(int, input().split())
    nC = B//C - (A-1)//C
    nD = B//D - (A-1)//D
    CD = (C*D)//gcd(C, D)
    nCD = B//CD - (A-1)//CD
    cnt = nC + nD - nCD
    ans = B - A + 1 - cnt
    print(ans)

if __name__ == "__main__":
    main()

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

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

from fractions import gcd
m=(c*d)//gcd(c,d)

A=b//c-(a-1)//c 
B=b//d-(a-1)//d 
C=b//m-(a-1)//m 
ans=(b-a+1)-(A+B-C)
print(ans)

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

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

a, b, c, d = map(int, input().split())
divc = b // c - a // c
if a % c == 0:
    divc += 1
divd = b // d - a // d
if a % d == 0:
    divd += 1
    
lcm = c * d // gcd(c, d)
divlcm = b // lcm - a // lcm
if a % lcm == 0:
    divlcm += 1


div_or = divc + divd - divlcm
print(int(b - a + 1 - div_or))

Solution (Python):
from fractions import gcd
a,b,c,d = map(int,input().split())
cd = c * d // gcd(c,d)
n_c = 0
n_d = 0
n_cd = 0
s = a // c 
e = b // c
n_c = e - s
if a%c==0:
    n_c+=1
s = a // d
e = b // d
n_d = e - s
if a%d==0:
    n_d+=1
s = a // cd 
e = b // cd
n_cd = e - s
if a%cd==0:
    n_cd+=1
print(b-a+1-(n_c+n_d-n_cd))

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

Solution (Python):
line = input().split()
import fractions
lcm = (int(line[2])*int(line[3]) //fractions.gcd(int(line[2]),int(line[3])))
 
pos_c = int(line[1])//int(line[2]) 
pos_d = int(line[1])//int(line[3])
pos_lcm = int(line[1])//lcm
 
pos_c1 = (int(line[0])-1)//int(line[2])
pos_d1 = (int(line[0])-1)//int(line[3])
pos_lcm1 = (int(line[0])-1)//lcm
 
b = int(line[1]) - pos_c - pos_d + pos_lcm
a = int(line[0]) - 1 - pos_c1 - pos_d1 + pos_lcm1
 
print(b-a)

Solution (Python):
line = input().split()
import fractions
lcm = (int(line[2])*int(line[3]) //fractions.gcd(int(line[2]),int(line[3])))

pos_c = int(int(line[1])//int(line[2])) 
pos_d = int(int(line[1])//int(line[3])) 
pos_lcm = int(int(line[1])//lcm)

pos_c1 = int((int(line[0])-1)//int(line[2])) 
pos_d1 = int((int(line[0])-1)//int(line[3])) 
pos_lcm1 = int((int(line[0])-1)//lcm)

b = int(line[1]) - pos_c - pos_d + pos_lcm
a = int(line[0]) - 1 - pos_c1 - pos_d1 + pos_lcm1

print(b-a)

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

from fractions import gcd
m=(c*d)//gcd(c,d)

A=b//c-(a-1)//c 
B=b//d-(a-1)//d 
C=b//m-(a-1)//m 
ans=(b-a+1)-(A+B-C)
print(ans)

Solution (Python):
from fractions import gcd


A, B, C, D = map(int, input().split())
LCM = C * D // 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 gcd(a,b):
  if b!=0:
    return gcd(b, a%b)
  else:
    return a
  
def lcm(a,b):
  return a//gcd(a,b)*b
  
def f(x, c, d):
  a = x
  a -= x//c
  a -= x//d
  a += x//lcm(c,d)
  return a
  
A,B,C,D = list(map(int, input().split()))

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

Solution (Python):
import fractions

left, right, fizz, buzz = map(int, input().split())


nums = right - left + 1
fizzbuzz = fizz * buzz // fractions.gcd(fizz, buzz)


if left % fizz == 0:
  fizz_nums = right // fizz - left // fizz + 1
else:
  fizz_nums = right // fizz - (left // fizz + 1) + 1

if left % buzz == 0:
  buzz_nums = right // buzz - left // buzz + 1
else:
  buzz_nums = right // buzz - (left // buzz + 1) + 1
  
if left % fizzbuzz == 0:
  fizzbuzz_nums = right // fizzbuzz - left // fizzbuzz + 1
else:
  fizzbuzz_nums = right // fizzbuzz - (left // fizzbuzz + 1) + 1
  

print(nums - (fizz_nums + buzz_nums - fizzbuzz_nums))
  

"""
debug = int(input())
if debug == (nums - (fizz_nums + buzz_nums - fizzbuzz_nums)):
  print("AC")
else:
  print("WA")
"""

Solution (Python):
from fractions import gcd
def LCM(x,y):
  return x*y//gcd(x,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())

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

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

    mult_b = b // c
    mult_b += b // d
    duplicates = b // lcm
    mult_b -= duplicates

    mult_a = (a-1) // c
    mult_a += (a-1) // d
    duplicates_a = (a-1) // lcm
    mult_a -= duplicates_a

    result = b - (a-1) - (mult_b - mult_a)
    print(result)

solve(a,b,c,d)

Solution (Python):
import fractions

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

U = B - A  + 1
c = (B // C) + (-A // C) + 1
d = (B // D) + (-A // D) + 1
x = C * D // fractions.gcd(C , D)
e = (B // x) + (-A // x) + 1
print(U - c - d + e)

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

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

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

cnt = 0
cnt += ((B // C) - ((A-1) // C))
cnt += ((B // D) - ((A-1) // D))

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

ans = B-A+1-cnt+tmp
print(ans)

Solution (Python):
from fractions import gcd

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

A,B,C,D = list(map(int,input().split()))
C1 = B//C
C2 = (A-1)//C
C3 = B//D
C4 = (A-1)//D
C5 = B//lcm(C,D)
C6 = (A-1)//lcm(C,D)
print((B-(A-1))-((C1-C2)+(C3-C4)-(C5-C6)))

Solution (Python):

import fractions
a, b, c, d = map(int, input().split())
l = c * d // fractions.gcd(c, d)
n = b - a + 1
print(n - (((b//c) - (a//c) + (a%c == 0)) + ((b//d) - (a//d) + (a%d == 0)) - ((b//l) - (a//l) + (a%l == 0))))

Solution (Python):
from fractions import gcd

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

A,B,C,D = list(map(int,input().split()))
C1 = B//C
C2 = (A-1)//C
C3 = B//D
C4 = (A-1)//D
C5 = B//lcm(C,D)
C6 = (A-1)//lcm(C,D)
print((B-(A-1))-((C1-C2)+(C3-C4)-(C5-C6)))

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

while True:
  if X > Y:
    t = Y
    Y = X % Y
    X = t
  else:
    Y = Y % X
  if Y == 0:
    break;

a = (A - 1) - (A - 1)//C - (A - 1)//D + (A - 1)//(C*D//X)
b = B - B//C - B//D + B//(C*D//X)

print(b - a)

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

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

b = B-(B//C)-(B//D)+B//lcm

a = A-(A//C)-(A//D)+A//lcm
print(b-a)

Solution (Python):
import fractions

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

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

divisible_c = (b // c) - (a // c)
divisible_d = (b // d) - (a // d)
divisible_cd = (b // lcm) - (a // lcm)

print((b - a) - (divisible_c + divisible_d - divisible_cd))

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(x, y):
    return x * y // gcd(x, y)


def f(x, c, d):
    num_c = x // c
    num_d = x // d
    num_c_d = x // lcm(c,d)

    return x - num_c - num_d + num_c_d

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

n_C = B//C - (A-1)//C 
n_D = B//D - (A-1)//D
CandD = lcm(C,D)
n_CandD = B//CandD - (A-1)//CandD


ans = B-A+1 - (n_C + n_D - n_CandD)
print(ans)

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

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

Solution (Python):

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



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


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

def mod(x,y):
    return x // y if x % y == 0 else x //y + 1

cc = b // c - mod(a,c)+1
dd = b // d - mod(a,d)+1
cd = b // lcm(c,d) - mod(a,lcm(c,d))+1
print(b-a+1-cc-dd+cd)

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

cd=int(c*d/gcd(c,d))
e=(b-a+1)
f=(b//c-a//c)
if c*(a//c)==a:
  f+=1
g=(b//d-a//d)
if d*(a//d)==a:
  g+=1
h=(b//cd-a//cd)
if cd*(a//cd)==a:
  h+=1

print(e-f-g+h)

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

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

def amari(x,y):
    return x//y if x%y==0 else x//y+1

cnum=b//c-amari(a,c)+1
dnum=b//d-amari(a,d)+1
cdnum=b//lcm(c,d)-amari(a,lcm(c,d))+1

print(b-a+1-cnum-dnum+cdnum)

Solution (Python):
import numpy as np
import functools
import math
import collections
import scipy
import fractions
import itertools
 
def solve():
  a, b, c, d = map(int, input().split())
  a -= 1
  lc = (c*d)//fractions.gcd(c,d)
  ans = (b-a)-(b//c-a//c)-(b//d-a//d)+(b//lc-a//lc)
  print(ans)
  return 0
 
if __name__ == "__main__":
  solve()

Solution (Python):
import fractions

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

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