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 fractions

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

print(b-a+1-int(b//c)+int((a-1)//c)-int(b//d)+int((a-1)//d)+int(b//l)-int((a-1)//l))

Solution (Python):
import fractions

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

print(b-a+1-int(b//c)+int((a-1)//c)-int(b//d)+int((a-1)//d)+int(b//l)-int((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 lcm(x, y):
    return (x * y) // gcd(x, y)

e = lcm(c, d)

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

print(x - y - z + w)

Solution (Python):
import fractions

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

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

Solution (Python):
import sys
from fractions import gcd
A,B,C,D=map(int, sys.stdin.readline().split())
C_cnt=B/C-(A-1)/C
D_cnt=B/D-(A-1)/D
CD_gcd=gcd(C,D)
CD_lcm=C/CD_gcd*D/CD_gcd*CD_gcd
CD_lcm_cnt=B/CD_lcm-(A-1)/CD_lcm
print B-(A-1)-C_cnt-D_cnt+CD_lcm_cnt

Solution (Python):
import fractions as math

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


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


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


lcm = c*d//(math.gcd(c,d))
num_cd = (b//lcm) - ((a-1)//lcm)

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

Solution (Python):
import fractions

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

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

print((b - a + 1) - (b_c + d_c - cd_c))

Solution (Python):
import fractions

def main():
    A,B,C,D = map(int, input().split())  
    lc = lcm(C, D)
    Bsum = B - (B//C) - (B//D) + (B//lc)
    A1 = A-1
    Asum = A1 - (A1//C) - (A1//D) + (A1//lc)
    print(Bsum-Asum)
    
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)



if __name__ == '__main__':
    main()

Solution (Python):
import fractions


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


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


print(all - part)

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

Solution (Python):
from fractions import gcd
 
def ceil(a, b):
  return -(-a//b)
 
a, b, c, d = map(int, input().split())
cd_lcm = c*d//gcd(c, d)
c_left, c_right = ceil(a,c), b//c
d_left, d_right = ceil(a,d), b//d
cd_left, cd_right = ceil(a,cd_lcm), b//(cd_lcm)
cv, dv, cdv = c_right - c_left + 1, d_right - d_left + 1, cd_right - cd_left + 1
print((b - a + 1) - (cv + dv - cdv))

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

a, b, c, d = map(int, input().split())
if c == 1:
  print(0)
  sys.exit()
cd_lcm = c*d//gcd(c, d)
c_left, c_right = ceil(a/c), b//c
d_left, d_right = ceil(a/d), b//d
cd_left, cd_right = ceil(a/cd_lcm), b//(cd_lcm)
cv, dv, cdv = c_right - c_left + 1, d_right - d_left + 1, cd_right - cd_left + 1
print((b - a + 1) - (cv + dv - cdv))

Solution (Python):
import fractions
def main():
    A, B, C, D = map(int, input().split())
    num = (B - A + 1)  - (get_num(B, C, D) - get_num(A-1, C, D))
    print(num)

def get_num(x, C, D):
    num_C = x // C
    num_D = x // D
    f = fractions.gcd(C, D)
    f2 = C*D//f
    num_CD = x // f2
    return num_C  + num_D - num_CD

if __name__  == '__main__':
    main()

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

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

a_c = (a-1) // c
a_d = (a-1) // d
a_c_d = (a-1) // c_d
a_ans = (a-1) - a_c - a_d + a_c_d


b_c = b // c
b_d = b // d
b_c_d = b // c_d
b_ans = b - b_c - b_d + b_c_d



print(math.floor(b_ans-a_ans))

Solution (Python):
from fractions import gcd

a, b, c, d = map(int, input().split())
a= a-1
a_c = a // c
b_c = b // c
a_d = a // d
b_d = b // d
e = c*d // gcd(c, d)

print((b - b_c - b_d + b//e) - (a - a_c - a_d + a//e))

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


print(n1 - n2)

Solution (Python):
import fractions

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

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

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

print(B_count-A_count)

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

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


from fractions import gcd


a1=a-1
bn=b-(b//c+b//d-b//(c*d//gcd(c,d)))
an=a1-(a1//c+a1//d-a1//(c*d//gcd(c,d)))

print(bn-an)

Solution (Python):
from fractions import gcd

lcm = lambda x, y: x*y // gcd(x, y)

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

b_ = b
b_ -= b // c
b_ -= b // d
b_ += b // lcm(c, d)

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


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

Solution (Python):

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

def divisor(x):
	
	x_c = x//c
	x_d = x//d
	
	x_lcm = x//lcm
	
	return x-x_c-x_d+x_lcm

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

Solution (Python):
import fractions

A,B,C,D=map(int,input().split())
E=C*D//fractions.gcd(C,D)
C1 = (A-1)//C
D1 = (A-1)//D
E1 = (A-1)//E
C2 = B//C
D2 = B//D
E2 = B//E


W1 = C1 + D1 - E1


W2 = C2 + D2 - E2


W = W2 - W1

print(B-(A-1)-W)

Solution (Python):
import fractions

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

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

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

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

Solution (Python):
A,B,C,D = [int(x) for x in input().split()]
def gdc(x,y):
  if x%y == 0:
    return y
  else:
    return gdc(y,x%y)
g = C*D//gdc(D,C)
count= B-A+1-B//C+(A-1)//C-B//D+(A-1)//D+B//g-(A-1)//g
print(count)

Solution (Python):
import fractions

A, B, C, D = [int(i) for i in input().split()]
A = A - 1
l = (C * D) // fractions.gcd(C,D)

b_sum = B -(B//C) - (B//D) + (B//l)
a_sum = A - (A//C) - (A//D) + (A//l)

print(b_sum - a_sum)

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

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

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

def main():
    A, B, C, D = map(int, readline().rstrip().split())
    CD = lcm_base(C, D)
    ans = B - (B//C + B//D - B//CD)
    ans -= ((A-1) - ((A-1)//C + (A-1)//D - (A-1)//CD))
    print(ans)

if __name__ == '__main__':
    main()

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

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

def main():
    A, B, C, D = map(int, readline().rstrip().split())
    ans = B - (B//C + B//D - B//lcm_base(C, D))
    ans -= ((A-1) - ((A-1)//C + (A-1)//D - (A-1)//lcm_base(C, D)))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd
a,b,c,d = map(int,input().split())
lcm = c*d//gcd(c,d)
dis = b-a
l = [c,d,lcm]
ans = []
for i in range(3):
  if a % l[i] == 0:
    ans.append(dis//l[i]+ 1)
  elif l[i] < a:
    ans.append((dis + a%l[i])//l[i])
  elif a < l[i] <= b:
    ans.append((b-l[i])//l[i] + 1)
  else:
    ans.append(0)
print(dis + 1 - ans[0] - ans[1] + ans[2])

Solution (Python):


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

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

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

s = b // c - a // c
t = b // d - a // d
LCM = lcm(c, d)
both = b // LCM - a // LCM
ans = (b - a) - s - t + both
print(ans)

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

Solution (Python):
from fractions import gcd

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

Solution (Python):
from fractions import gcd

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

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


A1 = (a-1) - (a-1)//c -(a-1)//d +(a-1)//(c*d//cd)
 

B = b - b//c - b//d +b//(c*d//cd)
 
print(B-A1)

Solution (Python):
from fractions import gcd


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


A, B, C, D = map(int, input().split())
SUM = B - A + 1
c = (B // C - (A - 1) // C)
d = (B // D - (A - 1) // D)
cd = (B // lcm(C, D) - (A - 1) // lcm(C, D))
print(B - A + 1 - (B // C - (A - 1) // C + B // D - (A - 1) // D - (B // lcm(C, D) - (A - 1) // lcm(C, D))))

Solution (Python):
from fractions import gcd


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


A, B, C, D = map(int, input().split())
SUM = B - A + 1
c = (B // C - (A - 1) // C)
d = (B // D - (A - 1) // D)
cd = (B // lcm(C, D) - (A - 1) // lcm(C, D))
print(B - A + 1 - (B // C - (A - 1) // C + B // D - (A - 1) // D - cd))

Solution (Python):
from fractions import gcd


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


A, B, C, D = map(int, input().split())
SUM = B - A + 1
c = (B // C - (A - 1) // C)
d = (B // D - (A - 1) // D)
cd = (B // lcm(C, D) - (A - 1) // lcm(C, D))
print(SUM - (c + d - cd))

Solution (Python):
from fractions import gcd

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

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

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

Solution (Python):
import math
import fractions
def main():
    A, B, C, D = map(int, input().split())
    
    n_U = (B-A+1)
    n_C = B//C - (A-1)//C
    n_D = B//D - (A-1)//D
    LCM = (C*D)//fractions.gcd(C, D)
    n_CD = B//LCM - (A-1)//LCM
    
    print(n_U - n_C - n_D + n_CD)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd 
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) 






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

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

Solution (Python):
from fractions import gcd

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

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

under_a = (a-1)//c + (a-1)//d - (a-1)//lcm_cd
under_b = b//c + b//d - b//lcm_cd
print(ans-(under_b-under_a))

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

cd = C*D//gcd(D,C)
f = lambda x: B//x - ((A-1)//x)
print(B - A + 1 - (f(C) + f(D) - f(cd)))

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

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

underA = (A-1) // C + (A-1) // D - (A-1) // int(lcm(C,D))
underB = B // C + B // D - B // lcm(C,D)
N = underB - underA
print(B-A+1-N)

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





un = B - A + 1

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

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

E = C * D / int(gcd(C, D)) 
E = int(E)
en = B//E - (A-1)//E

ans = un - (cn + dn) + en
print(int(ans))

Solution (Python):

import math
from  fractions import gcd

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

A,B,C,D = map(int,input().split())
d_cnt = B//D-(A-1)//D
c_cnt = B//C-(A-1)//C
cd = lcm(C,D)
cd_cnt = B//cd-(A-1)//cd
print(B-A+1-(c_cnt+d_cnt-cd_cnt))

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

from fractions import gcd
def f(n):
    
    x = n//c
    x += n//d
    
    lcm = c*d // gcd(c,d)
    x -= n // lcm
    
    return n-x

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

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

Solution (Python):
from fractions import gcd

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








e=B//C
f=B//D
g=(A-1)//C
h=(A-1)//D
i=e-g
j=f-h

P=gcd(C,D)
L=C*D//P
k=(A-1)//L
m=B//L
n=m-k

print(B-A+1-i-j+n)

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

total = b-a+1
lcm = int((c*d)/gcd(c, d))

d1 = b//c - a//c
d2 = b//d - a//d
add = b//lcm - a//lcm
if a % c == 0:
    d1 += 1
if a % d == 0:
    d2 += 1
if a % lcm == 0:
    add += 1

print(total-d1-d2+add)

Solution (Python):
def gcd(x, y):
  while y!=0:
    x, y = y, x%y
  return x
def lcd(x, y):
    return x*y//gcd(x,y)
def C():
    a,b,c,d = list(map(int, input().split()))
    c_divide = b//c - (a-1)//c
    d_divide = b//d - (a-1)//d
    cd_lcd = lcd(c,d)
    lcd_divide = b//cd_lcd - (a-1)//cd_lcd
    print(b-a+1 - c_divide - d_divide + lcd_divide)
C()

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

Solution (Python):
'''
Accepted    　:No
difficult   　: 
ペナルティ　  　:5分
実際の回答時間  :分
WAの回数　     :回
合計時間　　  　:分
'''
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def myAnswer(A:int,B:int,C:int,D:int) -> int:
   divC = B//C - (A - 1)//C 
   divD = B//D - (A - 1)//D
   divCD = B//lcm(C,D) - (A - 1)//lcm(C,D) 
   
   return (B - A + 1) - (divC + divD - divCD)

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

if __name__ == '__main__':
   main()

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

Solution (Python):
import math
def gcd(a,b):
	if a==0:
		return b
	else:
		return gcd(b%a,a)
def lcm(a,b):
	return (a*b)//gcd(a,b)

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

main()

Solution (Python):


import sys
















from fractions import gcd                    


def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    a, b, c, d = mi()
    m = c * d // gcd(c, d)
    one_to_a_minus_1 = (a-1) - ((a-1)//c + (a-1)//d - (a-1)//m)
    one_to_b = b - (b//c + b//d - b//m)
    print(one_to_b - one_to_a_minus_1)


if __name__ == "__main__":
    main()

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

Solution (Python):
import fractions

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


t = b//c + b//d - b//(c*d//g)
l = (a - 1)//c + (a - 1)//d - (a - 1)//(c*d//g)
ans = b - (a - 1) - t + l

print(ans)

Solution (Python):
import fractions

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

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

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

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

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


def calc(x, n):
    return x // n


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


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

Solution (Python):






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

cd_lcm = (c * d) // gcd(c, d)
ab_c = b//c - (a-1)//c
ab_d = b//d - (a-1)//d
ab_cd_both = b//cd_lcm - (a-1)//cd_lcm
ab_cd_either = ab_c + ab_d - ab_cd_both
ab_cd_neither = b-a+1 - ab_cd_either

print(ab_cd_neither)

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

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

def lcm(a, b):
  return a * b // fractions.gcd (a, b)
e = lcm(c, d)
r = b//e - (a-1)//e 
f = b - a + 1
p = f - p
q = f - q
r = f - r

print((p+q-r))

Solution (Python):
from fractions import gcd
a,b,c,d = map(int, input().split())
x = (c*d) // gcd(c,d)
c1, d1, cd1 = b // c, b // d, b // x
c2, d2, cd2 = (a-1) // c, (a-1) // d, (a-1) // x
ans = (c1+d1-cd1) - (c2+d2-cd2)
print(b-a+1-ans)

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

Solution (Python):

import sys
input = sys.stdin.readline




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




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

def f(x):
    return x - (x//C + x//D - x//lcm(C,D))

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

Solution (Python):
from fractions import gcd

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

c_num = b // c - (a - 1) // c
d_num = b // d - (a - 1) // d

c_d_lcm = c * d // gcd(c, d)
c_d_num = b // c_d_lcm - (a - 1) // c_d_lcm

ans = b - a + 1 - c_num - d_num + c_d_num
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)

def div_n(a,b,n):
    if a%n != 0:
        marker1 = a + (n - a%n)
    else:
        marker1 = a
        
    if b%n != 0:
        marker2 = b - (b%n)
    else:
        marker2 = b
    return (marker2 - marker1)//n + 1

all_cnt = (b-a)+1
a_cnt = div_n(a,b,c)
b_cnt = div_n(a,b,d)
c_cnt = div_n(a,b,lcm(c,d))

print(all_cnt - a_cnt - b_cnt + c_cnt)

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

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

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

C_count = B // C - A // C
if A % C == 0:
    C_count += 1
D_count = B // D - A // D
if A % D == 0:
    D_count += 1
CD_count = B // lcm(C, D) - A // lcm(C, D)
if A % lcm(C, D) == 0:
    CD_count += 1
div_count = C_count + D_count - CD_count
ans = B - A + 1 - div_count
print(ans)

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

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

lcm=(c*d)//fractions.gcd(c,d)
def q(n):
    sc=n//c
    sd=n//d
    s=n//lcm
    w=sc+sd-s
    return n-w

print(q(b)-q(a-1))

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

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

ans_a = a-1 - (a-1) // c - (a-1) // d + (a-1) // lcm

ans_b = b - b // c - b // d + b // lcm
print(ans_b - ans_a)

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

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


print(ans+1)

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

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

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

L = lcm(C, D)

def f(X):
    return X - (X//C + X//D - X//L)

num = f(B) - f(A-1)

print(num)

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

def gcd(a, b):
    """
    Greatest Common Divisor
    Euclidean Algortihm
    """
    if a < b:
        gcd(b, a)
    while b != 0:
        a, b = b, a % b
    return a

INF = float('inf')
NIL = - 1


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


A, B, C, D = map(int, input().split())
total = B - A + 1

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

if A % D != 0:
    d_count = (B // D) - (A // D)
else:
    d_count = (B // D) - (A // D) + 1


lcm = lcm(C, D)
if A % lcm != 0:
    lcm_count = (B // lcm) - (A // lcm)
else:
    lcm_count = (B // lcm) - (A // lcm) + 1


print(total - c_count - d_count + lcm_count)

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

a, b, c, d = [int(i) for i in input().split()]
def gcd(x,y):
    while y:
        x,y = y, x%y
    return x
def lcm(x,y):
    return x*y // gcd (x,y)
def count(A,B,C):
    D=A//B
    E=A//C
    F=A // lcm(B,C)
    return A-D-E+F
ans = count(b, c, d) - count(a-1, c, d)



    
print(ans)

Solution (Python):

a, b, c, d = [int(i) for i in input().split()]
def gcd(x,y):
    while y:
        x,y = y, x%y
    return x
def lcm(x,y):
    return x*y // gcd (x,y)
def count(A,B,C):
    D=A//B
    E=A//C
    F=A // lcm(B,C)
    return A-D-E+F
ans = count(b, c, d) - count(a-1, c, d)


    
print(ans)

Solution (Python):

a, b, c, d = [int(i) for i in input().split()]
def gcd(x,y):
    while y:
        x,y = y, x%y
    return x
def lcm(x,y):
    return x*y // gcd (x,y)
def count(A,B,C):
    D=A//B
    E=A//C
    F=A // lcm(B,C)
    return A-D-E+F
ans = count(b, c, d) - count(a, c, d)

if(a % c != 0 and a % d != 0):
    ans += 1
print(ans)

Solution (Python):
import fractions
import math
a,b,c,d = map(int, input().split())
x1 = (a-1)//c
x2 = b//c
x = x2-x1
y1 = (a-1)//d
y2 = b//d
y = y2-y1
z1 = (a-1)//((c*d)//fractions.gcd(c,d))
z2 = b//((c*d)//fractions.gcd(c,d))
z = z2-z1
print(int((b-a+1)-x-y+z))

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

Solution (Python):
def main():
    import fractions
    a,b,c,d=map(int, input().split())
    def lcm(c,d):
        return c*d//fractions.gcd(c,d)
    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)
    
if __name__=='__main__':
    main()

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


x = B - (B // C) - (B // D)

l = fractions.gcd(C, D)

temp = C * D // l 

x += (B // temp)


a = A - 1
y = a - (a // C) - (a // D)
y += (a // temp)

ans = x - y
print(ans)

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

L = C*D//gcd(C,D)
a = A - ((A//C)+(A//D)-(A//L))
b = B - ((B//C)+(B//D)-(B//L))
print(int(b - a))

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):
A,B,C,D = map(int,input().split())

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

def F(N,C,D):
  x = N
  x -= N//C
  x -= N//D
  lcm = C*D//gcd(C,D)
  x += N//lcm
  return x

answer = F(B,C,D) - F(A-1,C,D)
print(answer)

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

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

x = c * d // euclid(c, d)

bd = b // d
bc = b // c
bx = b // x
bsum = b - bd - bc + bx

ad = (a - 1) // d
ac = (a - 1) // c
ax = (a - 1) // x
asum = (a - 1) - ad - ac + ax

print(bsum - asum)

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


def baisuNum(n,m):
    return n // m

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

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



CC = baisuNum(B,C) - baisuNum(A-1,C)

DD = baisuNum(B,D) - baisuNum(A-1,D)

LCM = lcm(C,D)
CD = baisuNum(B, LCM) - baisuNum(A-1, LCM)

print(B-A+1-CC-DD+CD)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

A, B, C, D = MAP()

Icm = C*D//gcd(C, D)
a = B//C - (A-1)//C
b = B//D - (A-1)//D
I = B//Icm - (A-1)//Icm
print((B-A+1) - (a+b-I))

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

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

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

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

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

Solution (Python):
import fractions

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

cd = (c * d) // fractions.gcd(c, d)
b_c = b // c
b_d = b // d
b_cd = b // cd
a_c = (a-1) // c
a_d = (a-1) // d
a_cd = (a-1) // cd

ans = b - b_c - b_d + b_cd - (a-1) + a_c + a_d - a_cd

print(ans)

Solution (Python):
import fractions

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

cd = (c * d) // fractions.gcd(c, d)
b_c = b // c
b_d = b // d
b_cd = b // cd
a_c = a // c
a_d = a // d
a_cd = a // cd

ans = b - b_c - b_d + b_cd - a + a_c + a_d - a_cd

if a % c != 0 and a % d!= 0:
    ans += 1

print(ans)

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

Solution (Python):
def ABC162C(a,b):
    while b != 0:
        a %= b
        if a == 0:break
        b %= a
    return max(a,b)
    

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

Solution (Python):
def countDivBy(d,a,b):
    resB=b//d
    resA=(a-1)//d
    res = resB-resA
    return res

def gcd(c,d):
    if(d>c):
        c,d = d,c
    a,b=c,d
    r=a%b
    while (r>0):      
        a = b
        b = r 
        r = a%b
    return b
  
A = list(map(int, input().split()))
a, b, c, d = A
g = gcd(c,d)
lcm = (c*d) // g
ans = b - a + 1 - countDivBy(c,a,b) - countDivBy(d,a,b) + countDivBy(lcm,a,b)
print(ans)

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
divL = B//lcm - (A-1)//lcm

print((B-A+1) - divC - divD + divL)

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

Solution (Python):
from fractions import gcd
lcm = lambda a, b: a * b // gcd(a, b)
num_of_multi = lambda n: b // n - a // n - bool(a % n) + 1

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

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

U = B - A + 1
U_c = (B // C) - (A // C) + int(A % C == 0)
U_d = (B // D) - (A // D) + int(A % D == 0)
U_cd = (B // CD) - (A // CD) + int(A % CD == 0)

ans = U - (U_c + U_d - U_cd)
print(ans)

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

Solution (Python):

import io
import fractions

n, m, k, j = map(int, input().split())
n = n-1
data = int(k*j/fractions.gcd(k, j))
result = (m-m//k-m//j+m//data)-(n-n//k-n//j+n//data)
print(result)

Solution (Python):
import fractions

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

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

L = lcm(C, D)

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

print(b - a)

Solution (Python):
from fractions import gcd


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


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

L = lcm(C, D)
numB = B - (B // C + B // D - B // L)
numA_1 = (A - 1) - ((A - 1) // C + (A - 1) // D - (A - 1) // L)
print(numB - numA_1)