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 sys
import os
import fractions

MOD = 10 ** 9 + 7


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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    A, B, C, D = list(map(int, sys.stdin.readline().split()))
    E = B - A + 1

    ret = 0
    ret += B // C
    ret += B // D
    ret -= (A - 1) // C
    ret -= (A - 1) // D
    ret -= B // lcm(C, D)
    ret += (A-1) // lcm(C, D)
    print(E - ret)


if __name__ == '__main__':
    main()

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):
    return x - x // C - x // D + x // lcm(C,D)

print(f(B) - f(A - 1))

Solution (Python):
import fractions

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

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

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

Solution (Python):
from fractions import gcd

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

 
  
   
  


def count2(A):
  c = A - A//C - A//D + A//lcm
  return c

c = count2(B)-count2(A-1)
print(int(c))

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

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

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

c_num = B//C - (A-1)//C
d_num = B//D - (A-1)//D

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

res = (B-A + 1) - (c_num + d_num - cd_num)

print(res)

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

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

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

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

print(k(b)-k(a-1))

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)

e=lcm(c,d) 

def kosu(num):
    z=num
    nc=num//c
    nd=num//d
    ne=num//e
    return z-nc-nd+ne

ans=kosu(b)-kosu(a-1)

print(ans)

Solution (Python):

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

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

def fun(x,c,d):
    return x - div(x,c) - div(x,d) + div(x,c*d//gcd(c,d))

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

print(fun(a[1],a[2],a[3])-fun(a[0]-1,a[2],a[3]))

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

lcm = c*d//fractions.gcd(c,d)
ansa = a - a//c - a//d + a//lcm
ansb = b - b//c - b//d + b//lcm

print(ansb-ansa)

Solution (Python):
from fractions import gcd

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

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

ABmulC = B//C - (A-1)//C
ABmulD = B//D - (A-1)//D
LCM = lcm(C,D)
ABmulLCM = B//LCM - (A-1)//LCM

ans = B - A + 1
ans -= ABmulC + ABmulD
ans += ABmulLCM
print(ans)

Solution (Python):

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

Solution (Python):
from fractions import gcd
a,b,c,d=map(int,input().split())
ans=(b-a+1)
lcm=(c*d)//gcd(c,d)
dba=(a//c-int(a%c==0))+(a//d-int(a%d==0))-(a//lcm-int(a%lcm==0))
dbb=(b//c)+(b//d)-(b//lcm)
dbb-=dba
ans-=dbb
print(ans)

Solution (Python):
from fractions import gcd
a,b,c,d=map(int,input().split())
ans=(b-a+1)
lcm=(c*d)//gcd(c,d)
dba=(a//c-int(a%c==0))+(a//d-int(a%d==0))-(a//lcm-int(a%lcm==0))
dbb=(b//c)+(b//d)-(b//lcm)
dbb-=dba
ans-=dbb
print(ans)

Solution (Python):
import fractions

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



def count_divisible_numbers(x):
  a = (A-1) // x
  b = B // x
  return b - a

n_C = count_divisible_numbers(C)
n_D = count_divisible_numbers(D)
CD_lcm = C*D // fractions.gcd(C, D)
n_CD = count_divisible_numbers(CD_lcm)

res = (B - A + 1) - n_C - n_D + n_CD
print(res)

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def gcd(n, m):
  while m:
    n, m = m, n%m
  return n

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

def f(n, m, l):
  a = n // l
  if a * l < n:
    a += 1
  b = m // l
  return b - a + 1

def main():
    a, b, c, d = na()
    x = b - a + 1
    print(x - (f(a, b, c) + f(a, b, d) - f(a, b, lcm(c, d))))

if __name__ == '__main__':
    main()

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

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

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

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

print(ans)

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


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


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

def multiple_counting(range, a, b):
    return (range -range//a -range//b +range//lcm(a,b))

def main():
    print(multiple_counting(b, c, d) - multiple_counting(a-1, c, d))

def test():
    from fractions import gcd
    A, B, C, D = map(int, input().split())
    l = C * D // gcd(C, D)
    x = (A - 1) - (A - 1) // C - (A - 1) // D + (A - 1) // l
    y = B - B // C - B // D + B // l
    print(y - x)
main()

Solution (Python):
from fractions import gcd

A, B, C, D = map(int, input().split())
l = C * D // gcd(C, D)
x = (A - 1) - (A - 1) // C - (A - 1) // D + (A - 1) // l
y = B - B // C - B // D + B // l
print(y - x)

Solution (Python):
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)

def mulc(t) :
    return (B//t - (A-1)//t)

print((B-A+1) - mulc(C) - mulc(D) + mulc(E))

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

sub_c = (A - 1) // C
all_c = (B) // C
bic = all_c - sub_c


sub_d = (A - 1) // D
all_d = (B) // D
bid = all_d - sub_d

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


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

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

sub_all = bid + bic - bitmp


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

Solution (Python):

def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n


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

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

def f(n,C=C,D=D):
    return n - (n//C + n//D - n//lcm(C,D))
print(f(B)-f(A-1))

Solution (Python):

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


ans1=b
a-=1
ans2=0
ans1-=b//c
ans1-=b//d

ans1+=b//(lcm(c,d))


ans2+=a//c
ans2+=a//d
ans2-=a//(lcm(c,d))
print(ans1-(a-ans2))

Solution (Python):

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

def lcm(x,y):
  return x*y//gcd(x,y)
a,b,c,d=map(int,input().split())
a-=1
e=lcm(c,d)
tc=b//c-a//c
td=b//d-a//d
te=b//e-a//e
print(b-a-tc-td+te)

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

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

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

def count(b, c, d):
    return b - b//c - b//d + b//(c * d // gcd(c, d))

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

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

def cnt_num(n, ok):
    cnt = 0
    
    cnt += n // c
    
    cnt += n // d
    
    cnt -= n // ((c*d) // gcd(c, d))
    
    if ((n % c == 0 or n % d == 0) or n % ((c*d) // gcd(c, d)) == 0) and ok:
        cnt -= 1
        
    return cnt

ans = cnt_num(b, False) - cnt_num(a, True)


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

Solution (Python):
from fractions import gcd

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

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

all_count = b - a + 1
c_count = b // c - (a-1) // c
d_count = b // d - (a-1) // d
cd_count = b // lcm - (a-1) // lcm

print(all_count - c_count - d_count + cd_count)

Solution (Python):
from fractions import gcd

"""

+ その範囲の6の倍数の数
  これはBまでの倍数の数からAまでの倍数の数を引く
+ その範囲の8の倍数の数
- その範囲の48の倍数の数
で答えが求まる

"""

def v_count(n, start, end):
  a = (start - 1) // n
  b = end // n
  return b - a

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

all_count = B - A + 1

c_count = v_count(C, A, B)
d_count = v_count(D, A, B)
min_cd = (C * D) // gcd(C, D)
cd_count = v_count(min_cd, A, B)

print(all_count - c_count - d_count + cd_count)

Solution (Python):

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

def calc_lcd(x, y):
    if x < y:
        temp = x
        x = y
        y = temp
    
    r = x % y
    z = x * y
    while r != 0:
        x = y
        y = r
        r = x % y
    
    return z // y

lcd = calc_lcd(c, d)
a -= 1


print(b - b // c - b // d + b // lcd - (a - a // c - a // d + a // lcd))

Solution (Python):




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


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


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

    b = B // C + B // D - B // lcm(C, D)
    a = (A - 1) // C + (A - 1) // D - (A - 1) // lcm(C, D)

    print(B - A + 1 - (b - a))


if __name__ == "__main__":
    main()

Solution (Python):
from fractions import gcd
A,B,C,D=map(int,input().split())
A=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 gcd
a,b,c,d=map(int,input().split())
def not_c_d(x):
    return x-(x//c+x//d-x//(c*d//gcd(c,d)))
print(not_c_d(b)-not_c_d(a-1))

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

Solution (Python):
from fractions import gcd
A,B,C,D = map(int,input().split())
def f(n):
    x = n//C
    y = n//D
    z = n//(C*D//gcd(C,D))
    return n - x - y + z

print(f(B) - f(A-1))

Solution (Python):
import math
def gcd(a,b):
  if a < b:
     a,b = b,a
  c = a%b
  if c == 0:
    return b
  return gcd(b,c)

A,B,C,D = map(int,input().split())
x = gcd(C,D)
count = 0
x = int(C*D/x)
a = (B//C)-((A-1)//C)
b = (B//D)-((A-1)//D)
c = (B//x)-((A-1)//x)
ans = B-A+1 - (a+b-c)
print(ans)

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)

def under_div(A, C, D):
    lcm = C*D//gcd(C, D)
    return A//C+A//D-A//lcm

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

Solution (Python):

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


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

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

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

nc=B//C-(A-1)//C
nd=B//D-(A-1)//D
cd=int(C*D/gcd(C,D))
ncd=B//cd-(A-1)//cd
print(B-A+1-nc-nd+ncd)

Solution (Python):
def gcd(a,b):
    if a<b:
        a,b=b,a
    c=a%b
    if c==0:
        return b
    return gcd(b,c)
A,B,C,D=map(int,input().split())
if A%C==0:
    
    nc=B//C-A//C+1
else:
    
    nc=B//C-A//C
if A%D==0:
    
    
    nd=B//D-A//D+1
else:
    
    nd=B//D-A//D
cd=int(C*D/gcd(C,D))
if A%cd==0:
    
    ncd=B//cd-A//cd+1
else:
    
    ncd=B//cd-A//cd

print(B-A+1-nc-nd+ncd)

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 int(a*b / gcd(a,b))
X = A-B
Y = lcm(C,D)
i = B//C-(A-1)//C
j = B//D-(A-1)//D
k = B//Y-(A-1)//Y
print(int(B-A+1-(i+j-k)))

Solution (Python):
import itertools as it
import collections as col
import fractions
from functools import reduce

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

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

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def ppp(c,d):
	while c % d != 0:
		nc = d
		nd = c % d
		c,d = nc, nd
	return d

def main():
	a, b, c, d = IN()
	cad=(c*d)//ppp(c,d)
	
	
	all_num=b-a+1
	can_c=(b//c) - ((a-1)//c)
	can_d = (b//d) - ((a-1)//d)
	can_c_and_d = (b//(cad)) - ((a-1)//(cad))
	ret = all_num-can_c-can_d+can_c_and_d
	
	print(ret)
	
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

c = C
d = D

gcd = 0
while gcd == 0:
  if c > d:
    k = c
    c = d
    d = k 
  if d%c == 0:
    gcd = c
  else:
    d = d%c


lcm = int(C*D/gcd)

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

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

from fractions import gcd

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

ac = (a - 1) // c
ad = (a - 1) // d
acd = (a - 1) // lcm(c, d)
x = (a - 1) - ac - ad + acd

bc = b // c
bd = b // d
bcd = b // lcm(c, d)
y = b - bc - bd + bcd

ans = y - x
print(ans)

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

def f(n):
    if n==0:
        return 0
    else:
        return n-n//c-n//d+n//(c*d//fractions.gcd(c,d))

print(f(b)-f(a-1))

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

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

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

Solution (Python):
from fractions import gcd
a, b, c, d = map(int, input().split())
lcm = (c*d)//gcd(c, d)
ac = (a-1)//c
bc = b//c
ad = (a-1)//d
bd = b//d
acd = (a-1)//lcm
bcd = b//lcm
abc = bc-ac
abd = bd-ad
abcd = bcd-acd
print((b-a+1)-abc-abd+abcd)

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

lcm = lambda x, y: x * y // gcd(x, y)
a -= 1
ans = b - (b // c + b // d - b // lcm(c, d)) - a + (a // c + a // d - a // lcm(c, d))
print(ans)

Solution (Python):
from fractions import gcd

def count_divisible_by(n, start, end):
    return end // n - (start - 1) // n

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

lcm = c * d // gcd(c, d)
print(b-a+1 - count_divisible_by(c, a, b) - count_divisible_by(d, a, b) + count_divisible_by(lcm, a, b))

Solution (Python):
import fractions

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

a,b,c,d=map(int,input().split())
d1=b//c
d2=b//d
d3=b//lcm(c,d)

d4=(a-1)//c
d5=(a-1)//d
d6=(a-1)//lcm(c,d)

ansb=b-(d1+d2-d3)
ansa=a-1-(d4+d5-d6)
print(ansb-ansa)

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

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

def count(num, a, b):
    return int(num - num//a - num//b + num//lcm(a,b))

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

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

l = c*d // gcd(c, d)
x = (a-1) - (a-1)// c - (a-1) // d + (a-1) // l
y = b - b // c - b // d + b //l
print(y - x)

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

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

def div(x,y,z):
    x_r = x % z
    y_r = z - y % z
    tmp = ((y + y_r) - (x - x_r)) // z + 1
    if x_r != 0:
        tmp -= 1
    if y_r != 0:
        tmp -= 1
    return tmp

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

Solution (Python):

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

def anti_div(a,b,c):
    aa = (a-1) - (a-1) // c
    bb = b - b // c
    return bb - aa




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

ans = anti_div(a,b,c) + anti_div(a,b,d) - anti_div(a,b,lcd)
print(ans)

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

    def lcm(v1, v2):
        v3 = v1 % v2
        if v3 == 0:
            return v2
        return lcm(v2, v3)

    def gcd(v1, v2):
        return v1 * v2 // lcm(v1, v2)

    count = B - (A - 1)

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

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

    print(count - c - d + cd)


main()

Solution (Python):
from fractions import gcd
A, B, C, D = map(int, input().split())
cnt_C = int(B // C) - int((A-1) // C)
cnt_D = int(B // D) - int((A-1) // D)
cnt_CD = int(B // int((C*D)/gcd(C,D))) - int((A-1) // int((C*D)/gcd(C,D)))
print(B - A + 1 - (cnt_C + cnt_D - cnt_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 int(a*b / gcd(a,b))
cnt = 0
quo_c = b//c - (a-1)//c
quo_d = b//d - (a-1)//d
quo_cd = b//lcm(c,d) - (a-1)//lcm(c,d)
print(b - (a-1) - quo_c - quo_d + quo_cd)

Solution (Python):
import os, sys, re, math, queue

A,B,C,D = list(map(int,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)

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

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

print(ans)

Solution (Python):



from fractions import gcd

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

def main():
  a, b, c, d = map(int, input().strip().split())
  a -= 1
  x = (b - a) - ((b//c - a//c) + (b//d - a//d) - (b//lcm(c, d) - a//lcm(c, d)))
  print(x)

if __name__=='__main__':
  main()

Solution (Python):
import os, sys, re, math

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

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

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

minC = (A + (C - 1)) // C
maxC = B // C

minD = (A + (D - 1)) // D
maxD = B // D

CD = lcm(C, D)
minCD = (A + (CD - 1)) // CD
maxCD = B // CD

countC = maxC - minC + 1
countD = maxD - minD + 1
countCD = maxCD - minCD + 1

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

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

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

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

def counter(division):
    a = A // division
    if A % division == 0:
        a -= 1
    b = B // division
    return b - a

c = counter(C)
d = counter(D)
cd = counter(lcm(C , D))
ret = B - A + 1 - c - d + cd
print(ret)

Solution (Python):
a, b, c, d = list(map(int, input().split(' ')))
a = a - 1
def gcd(a, b):
    return b if a % b == 0 else gcd(b, a%b)

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

right_c = b // c
right_d = b // d
right = b - right_c - right_d + (b // (cd))
left_c = a // c
left_d = a // d
left = a - left_c - left_d + (a // (cd))
print(right - left)

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

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

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

bc = B // C
bd = B // D
bcd = B // lcm

ac = (A-1) // C
ad = (A-1) // D
acd = (A-1) // lcm

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

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def lcm(a,b):
    return a*b//gcd(a,b)
def main():
    A,B,C,D=MI()
    A-=1
    B_cnt = B-(B//C+B//D-B//lcm(C,D))
    A_cnt = A-(A//C+A//D-A//lcm(C,D))
    print(B_cnt-A_cnt)
if __name__ == '__main__':
    main()

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

ans = b_num - a_num

print(ans)

Solution (Python):

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

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

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

num_C = b // c - (a - 1) // c  
num_D = b // d - (a - 1) // d  
num_CD = b // lcm(c, d) - (a - 1) // lcm(c, d)

ans = (b - a + 1) - num_C - num_D + num_CD
print(ans)

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

import fractions

def f(N):
    red = N//C
    red += N//D
    X = (C * D // fractions.gcd(C, D))
    red -= N//X
    return N - red

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

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

Solution (Python):
import math
import fractions

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

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

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


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


div_c = b // c - (a - 1) // c
div_d = b // d - (a - 1) // d
e = c // gcd(c, d) * d
div_e = b // e - (a - 1) // e

ans = b - a + 1
ans -= (div_c + div_d) - div_e

print(ans)

Solution (Python):
from fractions import gcd


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


if __name__ == "__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)
    ans += (b // lcm(c, d) - (a-1) // lcm(c, d))
    print(ans)

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())
lcm = (c*d) // gcd(c, d)
ans = b - a + 1
cdiv = b // c - (a-1) // c
ddiv = b // d - (a-1) // d
cddiv = b // lcm - (a-1) // lcm

ans -= cdiv + ddiv - cddiv
print(ans)

Solution (Python):
import fractions

from functools import reduce
def lcm_base(x, y):
  return (x * y) // fractions.gcd(x, y)
  

a, b, c, d = map(int, input().split())
l = lcm_base(c, d)
x = b//c-(a-1)//c
y = b//d-(a-1)//d
z = b//l-(a-1)//l
print((b-a+1)-(x+y-z))

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

Solution (Python):
a,b,c,d=map(int,input().split())
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)
a1=(a-1)//c
a2=b//c
b1=(a-1)//d
b2=b//d
new_lcm=lcm(c,d)
c1=(a-1)//new_lcm
c2=b//new_lcm
print((b-a)+1-((a2-a1)+(b2-b1))+(c2-c1))

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

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

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

Solution (Python):
a, b, c, d = map(int, input().split())
x, y = c, d
if x<y: x, y = y, x
while y: x, y = y, x%y
lcm = c*d//x

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

Solution (Python):
def main():
    a, b, c, d = (int(i) for i in input().split())

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

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

    def f(x):
        p = x//c
        q = x//d
        r = x//lcm(c, d)
        return x - p - q + r

    print(f(b) - f(a-1))


if __name__ == '__main__':
    main()

Solution (Python):
import sys

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

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

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
import fractions

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


all_count = B-A+1








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


C_A_count = (A-1) // C
D_A_count = (A-1) // D
G_A_count = (A-1) // G


C_B_count = B // C
D_B_count = B // D
G_B_count = B // G

C_count = C_B_count - C_A_count
D_count = D_B_count - D_A_count
G_count = G_B_count - G_A_count

print(all_count - (C_count + D_count - G_count))

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

Solution (Python):
import fractions
a,b,c,d = map(int,input().split())
t1 = b//c-a//c
if a%c == 0:
    t1 += 1
t2 = b//d-a//d
if a%d == 0:
    t2 += 1
lcm = c*d//fractions.gcd(c,d)
t3 = b//lcm-a//lcm
if a%lcm == 0:
    t3 += 1
print(b-a+1-(t1+t2-t3))

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp



def main():
    a,b,c,d=mis()
    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))

main()

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

from fractions import gcd
total = B - A + 1
c_multiples = B // C + 1 - (A+C-1) // C
d_multiples = B // D + 1 - (A+D-1) // D
lcm = C * D // gcd(C, D)
lcm_multiples = B // lcm + 1 - (A+lcm-1) // lcm

ans = total - c_multiples - d_multiples + lcm_multiples
print(ans)

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

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

cdiv = b//c - (a-1)//c
ddiv = b//d - (a-1)//d

div = b//lcm -(a-1)//lcm

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

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

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

ret_b = b - (b // c + b // d - b // f)
ret_a = (a - 1) - ((a - 1) // c + (a - 1) // d - (a - 1) // f)
ans = ret_b - ret_a
print(ans)

Solution (Python):
def gcd(x, y):
    if x < y:
        x, y = y, x
    while y:
        x, y = y, x % y
    return x
def lcm(x, y):
    return x * y // gcd(x, y)

a, b, c, d = map(int, input().split())
m = lcm(c, d)
if a % c == 0:
    x = b // c - a // c + 1
else:
    x = b // c - a // c
if a % d == 0:
    y = b // d - a // d + 1
else:
    y = b // d - a // d
if a % m == 0:
    z = b // m - a // m + 1
else:
    z = b // m - a // m
print(b - a - x - y + z + 1)

Solution (Python):
def count(a,b,k):
    l = a + (k - (a % k)) if a % k != 0 else a
    r = b - (b % k)
    if l > r:
        return 0
    return (r-l)//k+1
def gcd(n,m):
    n,m = max(n,m),min(n,m)
    r = n % m
    while r > 0:
        n,m = m,r
        r = n % m
    return m
def lcm(n,m):
    return n*m//gcd(n,m)
a,b,c,d = map(int,input().split())
print((b-a+1)-count(a,b,c)-count(a,b,d)+count(a,b,lcm(c,d)))

Solution (Python):
A ,B ,C ,D = (map(int , input().split()))
from fractions import gcd
def lcm(x,y):
    return (x*y) // gcd(x,y)
koubaisuu = lcm(C,D)
e = B//C
f = B//D
g = B//(koubaisuu)
h = B-((e+f)-g)
AA = A - 1
ee = AA//C
ff = AA//D
gg = AA//(koubaisuu)
hh = AA-((ee+ff)-gg)
print(h-hh)

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)
s=(A-1)//C
t=(A-1)//D
u=(A-1)//lcm(C,D)
v=s+t-u
w=A-1-v
S=B//C
T=B//D
U=B//lcm(C,D)
V=S+T-U
W=B-V
print(W-w)

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