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 math import gcd

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


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

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

print(tmp1-tmp2)

Solution (Python):
from math import gcd
a, b, c, d = map(int, input().split())
whole = b - a + 1
sum_c = b//c - (a-1)//c
sum_d = b//d - (a-1)//d
div = (c*d)//gcd(c, d)
sum_c_d = b//div - (a-1)//div
print(whole - ((sum_c + sum_d) - sum_c_d))

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

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

def cal(x, a, b):
    cnt = b//x - a//x
    if a%x == 0:
        cnt += 1
    return cnt

ans = cal(c, a, b)
ans += cal(d, a, b)
ans -= cal(lcm(c, d), a, b)
print(n - ans)

Solution (Python):
import math
A, B, C, D = map(int, input().split())
total = B-A+1
A -= 1
waruC = (B//C)-(A//C)
waruD = (B//D)-(A//D)
CD = C * D // math.gcd(C, D)
waruCD = (B//CD)-(A//CD)
print(total-waruC-waruD+waruCD)

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

E = B//C + (-A//C) + 1
F = B//D + (-A//D) + 1
G = B//lcm(C,D) + (-A//lcm(C,D)) + 1
H = E + F - G
print(B - A + 1 - H)

Solution (Python):
import math
l = list(map(int,input().split()))

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

l[0] -= 1
la = l[0] - l[0] // l[2] - l[0] // l[3] + l[0] // lcm(l[2], l[3])
lb = l[1] - l[1] // l[2] - l[1] // l[3] + l[1] // lcm(l[2], l[3])
print(lb - la)

Solution (Python):
import numpy as np

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

cdlcm = np.lcm(c,d)

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

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

print(ans)

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

Solution (Python):
from math import gcd
a, b, c, d = map(int, input().split())
lcm = c*d//gcd(c, d)
cc = b//c - (a//c - (a%c==0))
cd = b//d - (a//d - (a%d==0))
clcm = b//lcm - (a//lcm - (a%lcm==0))
print(b-a+1 - (cc + cd - clcm))

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

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

Solution (Python):


from math import gcd

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

def solve(a,b,c,d):
  def countNonDivisor(n,c,d):
    return n - n//c - n//d + n//lcm(c,d)
  return countNonDivisor(b,c,d) - countNonDivisor(a-1,c,d)



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

  return

if __name__ == '__main__':
  main()

Solution (Python):
def common_multiple(x, y):
    for i in range(2, min(x, y)+1):
        x0, y0 = x % i, y % i
        if x0 == 0 or y0 == 0:
            return common_multiple(x // i if x0 == 0 else x, y // i if y0 == 0 else y) * i
    return x * y

a = input().split(" ")
A, B, C, D = int(a[0]) - 1, int(a[1]), int(a[2]), int(a[3])

M = common_multiple(C, D)
B0 = B // C + B // D - B // M
A0 = A // C + A // D - A // M
print(B - A - B0 + A0)

Solution (Python):
def common_multiple(x, y):
    for i in range(2, min(x, y)+1):
        if x % i == 0 or y % i == 0:
            return common_multiple(x // i if x % i  == 0 else x, y // i if y % i == 0 else y) * i
    return x * y

a = input().split(" ")
A, B, C, D = int(a[0]) - 1, int(a[1]), int(a[2]), int(a[3])

M = common_multiple(C, D)
B0 = B // C + B // D - B // M
A0 = A // C + A // D - A // M
print(B - A - B0 + A0)

Solution (Python):
def common_multiple(x, y):
    for i in range(2, min(x, y)+1):
        mod_x = x % i
        mod_y = y % i
        if (mod_x == 0 and mod_y == 0):
            return common_multiple(x // i, y // i) * i
        if (mod_x == 0):
            return common_multiple(x // i, y) * i
        if (mod_y == 0):
            return common_multiple(x, y // i) * i
    return  x * y

a = input().split(" ")
A,B,C,D=int(a[0]) - 1,int(a[1]),int(a[2]),int(a[3])

M = common_multiple(C,D)
B0 = B // C + B // D - B // M
A0 = A // C + A // D - A // M
print(B - A - B0 + A0)

Solution (Python):
import math
from typing import List

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

def gcd(c, d):
    mod = 1
    while mod > 0:
        mod = c % d
        c = d
        d = mod
    return c

def f(x: int, c: int, d: int):
    return x - x // c - x // d + x // (c * d // gcd(c, d))

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

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

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

c_num = f(a, b, c)
d_num = f(a, b, d)

cp = c
dp = d
mod = 1
while mod > 0:
    mod = cp % dp
    cp = dp
    dp = mod

cd_num =  f(a, b, (c * d) // cp)
print(b - a + 1 - c_num - d_num + cd_num)

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

c_f = (a + c - 1) // c
c_e =  b // c
d_f = (a + d - 1) // d
d_e =  b // d

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

num = b - a + 1 - (c_e - c_f + 1 + d_e - d_f + 1 - c_d_num)
 
print(num)

Solution (Python):
a,b,c,d=list(map(int,input().split()))
import math
def yakusuu(n):
  return n//c+n//d-n//(c*d//math.gcd(c,d))


print(int(b-a+1-(yakusuu(b)-yakusuu(a-1))))

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

import numpy as np


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

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

Solution (Python):
import math

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

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

b_n = b // c + b // d - b // cd
a_n = a // c + a // d - a // cd
print((b - b_n) - (a - a_n))

Solution (Python):

import collections as cl
import math
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


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


def find_target(x, c, d):
    num_c = x // c
    num_d = x // d
    lcm_cd = lcm(c, d)
    num_dip = x // lcm_cd
    return x - (num_c + num_d - num_dip)


def main():
    a, b, c, d = MI()
    target_in_a = find_target(a - 1, c, d)
    target_in_b = find_target(b, c, d)
    print(target_in_b - target_in_a)


main()

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

def div(z):
    p = (z//c) + (z//d) - (z//e)
    return p

x = a-1 - div(a-1)
y = b - div(b)
print(y-x)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import gcd
def main():
    a, b, c, d = map(int, input().split())
    if a == b:
        if a % c != 0 and a % d != 0:
            print(1)
            sys.exit()
        else:
            print(0)
            sys.exit()
    atob = b - a + 1
    divc = b // c - (a - 1) // c
    divd = b // d - (a - 1) // d
    cd = c * d // gcd(c, d)
    divcd = b // cd - (a - 1) // cd
    r = atob - divc - divd + divcd
    print(r)


if __name__ == '__main__':
    main()

Solution (Python):











































global FLAG_LOG
FLAG_LOG = False


def log(value):
    
    
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    
    
    A, B, C, D = list(map(int, lines[0].split()))
    
    
    
    
    
    
    
    

    import math

    cd = (C*D) // math.gcd(C, D)
    ac = (A-1) // C
    ad = (A-1) // D
    acd = (A-1) // cd
    a = (A-1) - ac - ad + acd
    log(f'a=[{a}]')
    bc = B // C
    bd = B // D
    bcd = B // cd
    b = B - bc - bd + bcd
    log(f'b=[{b}]')

    result = b-a

    return [result]



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['4 9 2 3']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['10 40 6 8']
        lines_export = [23]
    if pattern == 3:
        lines_input = ['314159265358979323 846264338327950288 419716939 937510582']
        lines_export = [532105071133627368]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    a, b, c, d = I2()
    
    cnt = b - a + 1
    cnt -= b//c
    cnt += (a-1)//c
    cnt -= b//d
    cnt += (a-1)//d
    cnt += b//(c*d//math.gcd(c,d))
    cnt -= (a-1)//(c*d//math.gcd(c,d))
    print(cnt)

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

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from math import gcd

a,b,c,d=nii()

a_c=(a-1)//c
b_c=b//c

a_d=(a-1)//d
b_d=b//d

lcm=(c*d)//gcd(c,d)
a_lcm=(a-1)//lcm
b_lcm=b//lcm

print((b-a+1)-(b_c-a_c)-(b_d-a_d)+(b_lcm-a_lcm))

Solution (Python):
from math import gcd

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

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

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

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

Solution (Python):
import math

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

a = A - 1
bn = B - B//C - B//D + (B//((C*D)//math.gcd(C,D)))
an = a - a//C - a//D + (a//((C*D)//math.gcd(C,D)))
print(bn - an)

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

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

xa = math.ceil(a/c)-1
xb = b//c
ya = math.ceil(a/d)-1
yb = b//d
za = math.ceil(a/e)-1
zb = b//e
ans = (b-a+1)-(xb-xa)-(yb-ya)+(zb-za)
if ans >=0:
    print(ans)
else:
    print(0)

Solution (Python):
from math import gcd

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

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

def n_mul(c):
  if A%c==0 and B%c==0:return(B//c-A//c+1)
  elif A%c==0 and B%c!=0:return(B//c-A//c+1)
  elif A%c!=0 and B%c==0:return(B//c-A//c)
  else:return(B//c-A//c)

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

Solution (Python):
A,B,C,D =map(int,input().split())
x = C
y = D
while y>0:
    x,y = y,x%y
E = (C//x)*D
bc = B//C
bd = B//D
be = B//E
b = B-(bc+bd-be)
ac = (A-1)//C
ad = (A-1)//D
ae = (A-1)//E
a = A-1-(ac+ad-ae)
print(b-a)

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

A, B, C, D = mapint()
from math import gcd

m = C*D//gcd(C, D)
Ccnt = B//C-(A-1)//C
Dcnt = B//D -(A-1)//D
Mcnt = B//m - (A-1)//m

print((B-A)-(Ccnt+Dcnt-Mcnt)+1)

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

def calc(X, a, b):
    return X - (X // a + X // b - X // ((a * b) // gcd(a, b)))

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

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

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

Solution (Python):

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

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

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

Solution (Python):
from math import gcd

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

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

s = b - ((b//c)+(b//d)) + b//((c*d)//gcd(c,d))
t = (a-1) - (((a-1)//c)+((a-1)//d)) + (a-1)//((c*d)//gcd(c,d))
print(s-t)

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

E = C*D//math.gcd(C,D)
all = B - A + 1
Cmod = B//C - (A-1)//C
Dmod = B//D - (A-1)//D
CDmod = B//E - (A-1)//E

print(all - Cmod - Dmod + CDmod)

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

import math
g = math.gcd(c, d)
l = int(c * d / g)

less_b = b - (b // c + b // d - b // l)
less_a = (a-1) - ((a-1)//c + (a-1) // d - (a-1)//l)
print(less_b-less_a)

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

if C == 1 or D == 1:
    print(0)
    exit()

def check(A, B, x):
    return B//x - (A-1)//x

print((B - A + 1) - (check(A, B, C) + check(A, B, D) - check(A, B, C*D//gcd(C, D))) )

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

lcm = C * D // gcd(C, D) 
x = (A - 1) - (A - 1) // C - (A - 1) // D + (A - 1) // lcm
y = B - B // C - B // D + B // lcm

print(y - x)

Solution (Python):
import math

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


def not_multiple_of_c_and_d(x: int, c: int, d: int):
    lcm_cd = c * d // math.gcd(c, d)
    c_no_baisu = x // c
    d_no_baisu = x // d
    cd_no_baisu = x // lcm_cd
    return x - (c_no_baisu + d_no_baisu - cd_no_baisu)


ans = not_multiple_of_c_and_d(B, C, D) - not_multiple_of_c_and_d(A-1, C, D)
print(ans)

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

def main(a,b,c,d):
    lcm=c*d//math.gcd(c,d)
    
    return b-a+1 - ( b//c-(a-1)//c + b//d-(a-1)//d - ( b//lcm - (a-1)//lcm ) )


if __name__=='__main__':
    a,b,c,d=readinput()
    ans=main(a,b,c,d)
    print(ans)

Solution (Python):
from math import gcd

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

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

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

X=f(B,C)-f(A-1,C)
Y=f(B,D)-f(A-1,D)
Z=f(B,E)-f(A-1,E)
print(B-A+1-(X+Y-Z))

Solution (Python):
import math

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


def count(n, d):
    return n // d


c_dividable = count(B, C) - count(A-1, C)
d_dividable = count(B, D) - count(A-1, D)
cd = C*D // math.gcd(C, D)
cd_dividable = count(B, cd) - count(A-1, cd)

print((B-A+1) - (c_dividable+d_dividable-cd_dividable))

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

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

s_c = b//c - (a-1)//c
s_d = b//d - (a-1)//d
s_cd = b//cd - (a-1)//cd
print((b-a+1)-s_c-s_d+s_cd)

Solution (Python):
import math

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

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

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

print(B - a - c - d + e)

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

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

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

def gcd(C,D):
    a=min([C,D])
    b=max([C,D])
    tmp=a
    a=b%a
    b=tmp
    if a==0:
        return tmp
    else:
        return gcd(a,b)

CD=C*D//gcd(C,D)
if A%CD==0:
    cd=B//CD-A//CD+1
else:
    cd=B//CD-A//CD

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

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

Solution (Python):
import math
A, B, C, D = map(int, input().split())
L = C * D //math.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())
import math
ans=b-a+1
e=(b//c)-(a-1)//c
f=(b//d)-(a-1)//d
g=(b//(c*d//math.gcd(c,d)))-((a-1)//(c*d//math.gcd(c,d)))
h=int(e+f-g)
print(ans-h)

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

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

Solution (Python):
import math
def main():
    a,b,c,d = map(int,input().split())
    bc = b // c
    bd = b // d
    bx = b // (c * d // math.gcd(c,d))
    B = b - bc - bd + bx
    
    a -= 1
    ac = a // c
    ad = a // d
    ax = a // (c * d // math.gcd(c,d))
    A = a - ac - ad + ax
    
    print(B-A)
    
if __name__ == "__main__":
    main()

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


    kosuu = b - a + 1
    

    a_c = a // c
    a_d = a // d 
    b_c = b // c
    b_d = b // d
    
    lcm_cd = int(c// math.gcd(c,d) * d)
    a_cd = int(a // lcm_cd)
    b_cd = int(b // lcm_cd)


    if a % c == 0:
        kosuu_c = b_c - a_c + 1
    else:
        kosuu_c = b_c - a_c
    
    if a % d == 0:
        kosuu_d = b_d - a_d + 1
    else:
        kosuu_d = b_d - a_d

    if a % lcm_cd == 0:
        kosuu_cd = b_cd - a_cd + 1
    else:
        kosuu_cd = b_cd - a_cd


    print(kosuu - kosuu_c - kosuu_d +kosuu_cd)




if __name__ == "__main__":
    main()

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

bf = b//f2
af = (a-1)//f2

cb = b//c
ca = (a-1)//c

db = b//d
da = (a-1)//d

print(b-a+1-(cb-ca)-(db-da)+(bf-af))

Solution (Python):
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

from fractions import gcd

A, B, C, D = rl()

def f(n, d):
	return n // d

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

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

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)
ans = B-A
print(b-a+1-ans)

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

l1 = a % c
r1 = b % c

if l1 > r1:
    cnt1 = (b - a)//c + 1
else:
    cnt1 = (b - a)//c
if l1 == 0:
    cnt1 += 1

l2 = a % d
r2 = b % d

if l2 > r2:
    cnt2 = (b - a)//d + 1
else:
    cnt2 = (b - a)//d
if l2 == 0:
    cnt2 += 1

e = (c*d)//math.gcd(c,d)
l3 = a % e
r3 = b % e

if l3 > r3:
    cnt3 = (b - a)//e + 1
else:
    cnt3 = (b - a)//e
if l3 == 0:
    cnt3 += 1

print((b - a + 1) - cnt1 - cnt2 + cnt3)

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

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

def divc(x):
  """ [A, B]にxの倍数がいくつあるかを返す"""
  b = B // x
  r = B % x
  a = A // x
  s = A % x
  res = b - a
  if s == 0:
    res += 1
  return res

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

print(ans)

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

Solution (Python):
a, b, c, d = map(int, input().split())
cc, dd, e = c, d, 1
for i in range(2, int(min(cc, dd)**0.5) + 1):
    if cc%i == 0 and dd%i == 0:
        while cc%i == 0 and dd%i == 0:
            cc //= i
            dd //= i
            e *= i
e *= cc*dd
print(b-a+1 - b//c + (a-1)//c - b//d + (a-1)//d + b//e - (a-1)//e)

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd





def solve():
    a, b, c, d = [int(x) for x in input().split()]
    cd = c * d // gcd(c, d)
    x = ((b // c) - (a + c - 1) // c + 1)
    y = ((b // d) - (a + d - 1) // d + 1)
    z = ((b // cd) - (a + cd - 1) // cd + 1)
    print(b-a+1-(x+y-z))



        

t = 1

for case in range(1,t+1):
    ans = solve()


"""



"""

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

if a%c==0:
  abc=b//c-a//c+1
else:
  abc=b//c-a//c  
  

if a%d==0:
  abd=b//d-a//d+1
else:
  abd=b//d-a//d

e=(c*d)//math.gcd(c, d)
if a%e==0:
  abcd=b//e-a//e+1
else:
  abcd=b//e-a//e

print(b-a+1-(abc+abd-abcd))

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

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

if a%c == 0:
    x = a//c
else:
    x = a//c + 1

if a%d == 0:
    y = a//d
else:
    y = a//d + 1

if a%lcm(c,d) == 0:
    z = a//lcm(c,d)
else:
    z = a//lcm(c,d) + 1
    
X = b//c
Y = b//d
Z = b//lcm(c,d)
    
print((b-a+1) - ((X-x+1) + (Y-y+1) - (Z-z+1)))

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


def gcd(x, y):
    if x > y:
        x, y = y, x
    while y != 0:
        tmp = x % y
        x = y
        y = tmp
    return x
    
A = A - 1
lcm_CD = C * D // gcd(C, D)
cnt_B = B - (B//C + B//D) + (B//lcm_CD)
cnt_A = A - (A//C + A//D) + (A//lcm_CD)

ans = cnt_B - cnt_A
print(ans)

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

num =(((a-1)//c)+(((a-1)//d))-((a-1)//l))
num2 =((b//c)+(b//d))-(b//l)
print(b-a-(num2-num)+1)

Solution (Python):
import math
A,B,C,D = map(int,input().split())
count1 = 0
count2 = 0
ans1 = B-B//C-B//D+B//((C*D)//math.gcd(C,D))
A = A-1
ans2 = A-A//C-A//D+A//((C*D)//math.gcd(C,D))
print(ans1-ans2)

Solution (Python):
import math

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

x=(a-1)//c

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

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

p=(a-1)//e

q=b//e

print(b-a+1-y+x-w+z+q-p)

Solution (Python):







__giturl__ = "$URL$"


import math
from sys import stdin
input = stdin.readline


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

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

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

  lcm = C * D // math.gcd(C, D)
  
  dcd = B//lcm - (A-1)//lcm

  
  
  
  
  
  print((B-A+1) - dc - dd + dcd)


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

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

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

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

L = lcm(C,D)

lenB = B-(B//C+B//D-B//L)
A-=1
lenA = A-(A//C+A//D-A//L)
print(lenB-lenA)

Solution (Python):
import math


def input2():
	return map(int,input().split())

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


A,B,C,D=input2()
lenC=B//C
lenD=B//D
lenCD=B//lcm(C,D)

ans=0
ans=B-(lenC+lenD-lenCD)

A-=1
lenC=A//C
lenD=A//D
lenCD=A//lcm(C,D)

ans-=A-(lenC+lenD-lenCD)

print(ans)

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

a,b,c,d = I()
x = lcm(c,d)

if a == b:
    if a % c != 0 and a % d != 0:
        print(1)
        sys.exit()
    else:
        print(0)
        sys.exit()

aa = a-1+(a-1)//x - (a-1)//c - (a-1)//d
bb = b+b//x - b//c - b//d
ans = bb-aa

print(ans)

Solution (Python):
import math

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

ans_lt_b = b - (b // c + b // d - b // lcm_cd)
ans_lt_a = (a - 1) - ((a - 1) // c + (a - 1) // d - (a - 1) // lcm_cd)
print(ans_lt_b - ans_lt_a)

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations,product

from heapq import heapify,heappop,heappush


from functools import reduce,lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    

    
    A,B,C,D = map(int, input().split())
    
    
    
    
    c1=(A-1)//C
    d1=(A-1)//D
    c2=(B)//C
    d2=(B)//D
    
    lca=C*D//math.gcd(C,D)
    x1=(A-1)//lca
    x2=(B)//lca

    ans=B-A+1-(c2-c1)-(d2-d1)+(x2-x1)

    print(ans)



if __name__ == "__main__":
    main()

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

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

ans_a = a - (a // c + a // d - a // a_b)
ans_b = b - (b // c + b // d - b // a_b)

print(int(ans_b-ans_a))

Solution (Python):
import math
A,B,C,D = map(int, input().split())
ans1 = B-B//C-B//D
g = math.gcd(C, D)
l = C*D//g
ans1 = ans1+B//l
ans2 = A-1-(A-1)//C-(A-1)//D
ans2 = ans2+(A-1)//l
ans = ans1-ans2
print(int(ans))

Solution (Python):
import math

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

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

dif = b-a+1
div_c = b//c - (a-1)//c
div_d = b//d - (a-1)//d
div_cd = b//lcm - (a-1)//lcm

ans = dif - (div_c + div_d) + div_cd
print(int(ans))

Solution (Python):


import sys
import math

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

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

def main():
    input = sys.stdin.readline
    a, b, c, d = map(int, input().split())
    a_1 = a - 1
    lcm_cd = lcm(c,d)
    
    a_1_c_count =  a_1 // c
    a_1_d_count =  a_1 // d
    a_lcm_count =  a_1 // lcm_cd

    a_1_result = a_1 - (a_1_c_count + a_1_d_count - a_lcm_count)


    b_c_count = b // c
    b_d_count = b // d
    b_lcm_count = b // lcm_cd

    b_result = (b) - (b_c_count + b_d_count - b_lcm_count)

    result = b_result - a_1_result

    print(result)
    return


if __name__ == '__main__':
    main()

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


ans = 0
tmp1, tmp2 = C, D
while 1:
  if tmp1 % tmp2 == 0:
    ans = tmp2
    break
  else:
    tmp = tmp1 % tmp2
    tmp1 = tmp2
    tmp2 = tmp
ans = int(C*D/ans)


num1 = B//C
num2 = (A-1)//C
num_C = num1-num2

num1 = B//D
num2 = (A-1)//D
num_D = num1-num2

num1 = B//ans
num2 = (A-1)//ans
num_CD = num1-num2

print(B-A+1-num_C-num_D+num_CD)

Solution (Python):
a, b, c, d = map(int, input().split())
 
def gcd(x, y):
    if x < y:
        x, y = y, x
    while True:
        if x % y == 0:
            return y
        x, y = y, x%y
 
x = b - a + 1
cx = b // c - (a-1) // c
dx = b // d - (a-1) // d
cdx = b // (c*d // gcd(c, d)) - (a-1) // (c*d // gcd(c, d))
print(x - cx - dx + cdx)

Solution (Python):
from collections import defaultdict
from collections import deque
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

a,b,c,d = readInts()

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

print(ans)

Solution (Python):
import sys
from operator import itemgetter
import math

sys.setrecursionlimit(10**9)
input = sys.stdin.readline

def ii(): return int(input())
def mi(): return map(int, input().split())
def lmi(): return list(map(int, input().split()))
def lmif(n): return [list(map(int, input().split())) for _ in range(n)]
def ss(): return input().split()

def main():
    A, B, C, D = mi()
    A -= 1
    CD_lcm = C * D // math.gcd(C, D)
    ans = B - B//C - B//D + B//CD_lcm
    ans -= A - A//C - A//D + A//CD_lcm
    print(ans)
    return

main()

Solution (Python):
def resolve():
    '''
    code here
    '''
    import math
    A, B, C, D = [int(item) for item in input().split()]

    if B//C == 0:
        num_C = 0
    elif B//C >= 1 and A % C == 0:
        num_C = B // C - A //C + 1
    elif B//C >= 1 and A % C != 0: 
        num_C = B // C - A //C
    else:
        num_C = B // C

    if B//D == 0:
        num_D = 0
    elif B//D >= 1 and A % D == 0:
        num_D = B // D - A //D + 1
    elif  B//D >= 1 and A % D != 0:
        num_D = B // D - A //D
    else:
        num_D = B // D

    lcm = C * D // math.gcd(C, D)
    if B//lcm == 0:
        num_CD = 0
    elif B//lcm > 1 and A  % lcm == 0:
        num_CD = B // lcm - A //lcm + 1
    elif B//lcm > 1 and A  % lcm != 0:
        num_CD = B // lcm - A //lcm
    else:
        num_CD = B // lcm

    
    print(B - A + 1 - (num_C + num_D - num_CD))


if __name__ == "__main__":
    resolve()

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

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

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

al = b-a+1

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

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

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

print(al - cnt_c - cnt_d + cnt_t)