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 io
import sys
from fractions import gcd


def IN_S():
  return input()


def IN_I():
  return int(input())


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


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


def STR_SPLIT(s, n):
  for l in range(0, len(s), n):
    yield s[0 + l:n + l]


def T_IN():
  global test_str
  sys.stdin = io.StringIO(test_str[1:-1])


test_str = '''
314159265358979323 846264338327950288 419716939 937510582
'''


def MAIN():
  
  A()


def A():
  a, b, c, d = IN_L_I()

  divc = (b // c) - ((a - 1) // c)
  divd = (b // d) - ((a - 1) // d)
  cd = c * d // gcd(c, d)
  divcd = (b // cd) - ((a - 1) // cd)


  total = (b + 1 - a) - divc - divd + divcd

  print(total)
  return None
 

def B():
  return None


def C():
  return None


MAIN()

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

A,B,C,D = LI()

'''
cでもdでも割り切れない
逆に割り切れる数を計算する
Bまでで割り切れる個数
Aまでで割り切れる個数
B'-A'でA-Bまでの割り切れる個数が出せる
'''
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

BC = B//C
BD = B//D
BCD = B//lcm(C,D)


B_count = B - (BC + BD - BCD)

A = A - 1
AC = A//C
AD = A//D
ACD = A//lcm(C,D)


A_count = A - (AC + AD - ACD)


print(B_count - A_count)

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

lcm = (C*D) // fractions.gcd(C,D)
Bnondiv = B - (B//C + B//D - B//lcm)
Anondiv = (A-1) - ((A-1)//C + (A-1)//D - (A-1)//lcm)
print(int(Bnondiv-Anondiv))

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

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


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

if(a%c==0):
    first = (b//c) - (a//c)+1
else:
    first = (b//c) - (a//c)
if(a%d==0):
    second = (b//d) - (a//d) + 1
else:
    second = (b//d) - (a//d)
lcm = (c*d)//gcd(c,d)
if(a%lcm==0):
    common = (b//lcm) - (a//lcm) + 1
else:
    common = (b//lcm) - (a//lcm)
print((b-a+1)-(first+second-common))

Solution (Python):
from fractions import gcd

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


def cnt_div(a, b, x):
    q, r = divmod(a, x)

    if r == 0:
        m = q - 1
    else:
        m = q

    n = b // x

    return n - m


num_c = cnt_div(a, b, c)
num_d = cnt_div(a, b, d)
lcm = int((c * d) / gcd(c, d))
num_c_d_gcd = cnt_div(a, b, lcm)

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

Solution (Python):
import math
from fractions import gcd

def count(n, A, B):
    n_upper = B // n
    n_lower = (A-1) // n
    n_count = n_upper - n_lower
    return n_count

def main():
    A, B, C ,D  = list(map(int, input().split(" ")))
    C_count = count(C, A, B)
    D_count = count(D, A, B)
    s = (C * D) // gcd(C, D)
    CD_count = count(s, A ,B)
    fcount = (B - A + 1) - (C_count + D_count - CD_count)
    print(fcount)

if __name__ == '__main__':
    main()

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

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

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

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

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

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]
def mi(n): return [wi() for _ in range(n)]
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

a, b, c, d = wi()

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

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


s = ((a + c - 1) // c) * c
t = (b // c) * c
kosu1 = (t - s) // c + 1

u = ((a + d - 1) // d) * d
v = (b // d) * d
kosu2 = (v - u) // d + 1

e = lcm_base(c, d)
w = ((a + e - 1) // e) * e
x = (b // e) * e
kosu3 = (x - w) // e + 1

print(b - a + 1 - (kosu1 + kosu2 - kosu3))

Solution (Python):
from fractions import gcd

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

f = lambda n: n - (n // c + n // d - n // l)

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

Solution (Python):
import fractions

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

U = B - A + 1

def number(R):
        return ((B // R) - ((A - 1) // R))

possible_C = number(C)
possible_D = number(D)
possible_CD = number((C * D) // fractions.gcd(C, D))

print(U - (possible_C + possible_D) + possible_CD)

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

Solution (Python):

from fractions import gcd

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


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


def solve():
    A,B,C,D = list(map(int, input().split()))
    A -= 1
    ans = (B - (f(B,C)+f(B,D)-f(B,lcm(C,D)))) - (A - (f(A,C)+f(A,D)-f(A,lcm(C,D))))
    print(ans)

if __name__ == "__main__":
    solve()

Solution (Python):


import fractions

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


count_c=b//c - (a-1)//c
count_d=b//d - (a-1)//d
count_cd=b//(c*d//fractions.gcd(c,d)) - (a-1)//(c*d//fractions.gcd(c,d))

print(b-a+1-count_c-count_d+count_cd)

Solution (Python):
def bbb(x, y):
    return (x * y) // aaa(x, y)
 
 
def aaa(x, y):
    if x == 0:
        return y
    elif y == 0:
        return x
    else:
        return aaa(y, x % y)
 
 
a, b, c, d = map(int, input().split())
bc = b // c
bd = b // d
ac = (a-1) // c
ad = (a-1) // d
aw = (a-1) // bbb(c, d)
bw = b // bbb(c, d)
print((b - a + 1) - (bc - ac) - (bd - ad) + (bw - aw))

Solution (Python):
from fractions import gcd

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

def f(n, C, D):
    x = n // C
    y = n // D
    z = n // lcm(C, D)
    return n - x - y + z


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

Solution (Python):
from fractions import gcd

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

def f(n, C, D):
    x = n // C
    y = n // D
    z = n // lcm(C, D)
    return n - x - y + z


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

Solution (Python):
from fractions import gcd

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


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

Solution (Python):
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
logging.disable(logging.CRITICAL)


def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if x % y == 0:
        return y
    else:
        while x % y != 0:
            z = x % y
            x = y
            y = z
        else:
            return z

def main():
    A, B, C_, D_ = (int(x) for x in input().split())

    if C_ > D_:
        C, D = D_, C_
    else:
        C, D = C_, D_
        

    lcm_CD = int(C * D / gcd(C, D))
    
    div_C = int(B // C) - int((A - 1) // C)
    div_D = int(B // D) - int((A - 1) // D)
    div_lcm_CD = int(B // lcm_CD) - int((A - 1) // lcm_CD)
    
    answer = B - A + 1 - div_C - div_D + div_lcm_CD

    if A == 0:
        print(answer - 1)
    else:
        print(answer)

if __name__ == "__main__":
    main()

Solution (Python):
def bbb(x, y):
    return (x * y) // aaa(x, y)


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


a, b, c, d = map(int, input().split())
bc = b // c
bd = b // d
ac = (a-1) // c
ad = (a-1) // d
aw = (a-1) // bbb(c, d)
bw = b // bbb(c, d)
print((b - a + 1) - (bc - ac) - (bd - ad) + (bw - aw))

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





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


b_ika=b-(b//c+b//d-b//lcm(c,d))
a_mimann=a-1-((a-1)//c+(a-1)//d-(a-1)//lcm(c,d))

ans=b_ika-a_mimann

print(ans)

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

if C<D:
  c, d = C, D
else:
  c, d = D, C
while d%c!=0:
  c, d = d%c, c
lcm = C*D//c

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

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

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

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

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


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


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


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

Solution (Python):
from fractions import gcd
def lcm(a, b):
    return a * b // gcd(a,b)

a,b,c,d = map(int, input().split())
x1 = b // c
x2 = b // d
e = lcm(c,d)
x3 = b // e
y1 = b - x1 -x2 + x3
x4 = (a-1) // c
x5 = (a-1) // d
x6 = (a-1) // e
y2 = a -1 - x4 - x5 + x6
print(y1 - y2)

Solution (Python):
import fractions

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

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

Solution (Python):
import fractions
def kansu(x, c, d):
  c_baisu = x//c
  d_baisu = x//d
  l = (c * d) // fractions.gcd(c, d)
  juhuku = x//l
  return x - c_baisu - d_baisu + juhuku
  
a, b, c, d = (int(i) for i in input().split())
print(kansu(b,c,d)-kansu(a-1,c,d))

Solution (Python):
import fractions

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

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

lcm_cd = lcm(C, D)

mod_ac = A % C
mod_ad = A % D
mod_lcm = A % lcm_cd

c_num = 0
d_num = 0
cd_num = 0

if mod_ac == 0:
    c_num = B//C - A//C + 1
else:
    c_num = B//C - A//C

if mod_ad == 0:
    d_num = B//D - A//D + 1
else:
    d_num = B//D - A//D

if mod_lcm == 0:
    cd_num = B//lcm_cd - A//lcm_cd + 1
else:
    cd_num = B//lcm_cd - A//lcm_cd

ans = B - A + 1 - (c_num + d_num - cd_num)
print(ans)

Solution (Python):
import fractions

a = list(map(int,input().split()))
A = a[0]
B = a[1]
C = a[2]
D = a[3]
CD = C*D//fractions.gcd(C, D)


AC = (A-1)//C
AD = (A-1)//D
ACD = (A-1)//CD


BC = B//C
BD = B//D
BCD = B//CD

ans = (B-A+1) - ((BC-AC)+(BD-AD)-(BCD-ACD)) 

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

def query(a,b,k):
    r = a % k
    res = (b-(a-r))//k + (-r//k) + 1
    return res

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

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

Solution (Python):
def GCD(s, t):
    if s < t:
        s, t = t, s
    if t == 0:
        return s
    else:
        return GCD(t,s%t)

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

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

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

def count_baisuu(A, B, x): 
    start = A
    stop = B
    modx_a = A % x
    if modx_a == 0:
        start = 0
        stop = B-A
    else:
        start = A-modx_a+x
        stop = B-start
        start = 0
    if stop < 0:
        return 0
    else:
        return 1 + stop // x 

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

divc = count_baisuu(A, B, C)
divd = count_baisuu(A, B, D)
divcd = count_baisuu(A, B, cd_lcm)
print(str((B-A+1)-(divc+divd-divcd)))

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

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

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


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

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


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

A,B,C,D = map(int,input().split())
if A%C !=0:
  C_left = (A//C) + 1
else:
  C_left = (A//C)
C_right = (B//C)

if A%D !=0:
  D_left = (A//D) + 1
else:
  D_left = (A//D)
D_right = (B//D)
CD = lcm(C,D)
if A%(CD) !=0:
  CD_left = (A//(CD)) + 1
else:
  CD_left = (A//(CD))
CD_right = (B//(CD))

C_=C_right - C_left +1
D_=D_right - D_left +1
CD_=CD_right - CD_left +1
print(B-A+1-C_-D_+CD_)

Solution (Python):
from fractions import gcd
def gcm(a,b):
    k = a * b / gcd(a,b)
    return int(k)

A,B,C,D = map(int,input().split())
bk = B - B//C - B//D + B//gcm(C,D)
ak = (A-1) - (A-1)//C - (A-1)//D + (A-1)//gcm(C,D)

print(bk-ak)

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


def s_b_n(x, y):
    return x * y // s_y_n(x, y)


a, b, c, d = map(int, input().split())
bc = b // c
ac = (a-1) // c
bd = b // d
ad = (a-1) // d
bco = b // s_b_n(c, d)
aco = (a-1) // s_b_n(c, d)

print((b-a+1) - (bc-ac) - (bd-ad) + (bco-aco))

Solution (Python):
import math
import fractions

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

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


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


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


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


divCorD = divC + divD - divCD


AB = B - A + 1

print(AB - divCorD)

Solution (Python):

import math
import fractions

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

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


lcm = lcm(C, D)

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

print(ans)

Solution (Python):
import fractions

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

def func(n, p, q):
    return n - (n//p + n//q - n//lcm(p, q))

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

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

Solution (Python):
import fractions

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


c_and_d=C*D

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


count_C =0
count_D =0
count_C_and_D =0

count_C = int(B//C)-int((A-1)//C)
count_D = int(B//D)-int((A-1)//D)
count_C_and_D = int(B//lcm)-int((A-1)//lcm)

print(int((B-A+1) -(count_C + count_D - count_C_and_D)))

Solution (Python):


import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
    
a, b, c, d = map(int,input().split())
def cal(i, j, k):
    return i // j + i // k - i // lcm(j, k)

if a % c == 0 or a % d == 0:
    print(b - a - (cal(b, c, d) - cal(a, c, d)))
else:
    print(b - a - (cal(b, c, d) - cal(a, c, d)) + 1)

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 fai(n,x,y): 
    g=gcd(x,y) 
    l=x//g*y 
    return n-(n//x)-(n//y)+(n//l)
    

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

Solution (Python):
import fractions

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

answer_C = B // C - (A-1) // C
answer_D = B // D - (A-1) // D
answer_lcm = B // lcm_number - (A-1) // lcm_number

print(all_number - answer_C - answer_D + answer_lcm)

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

def gcd(c, d):
    c, d= max(c, d), min(c, d)
    if c % d == 0:
        return d
    else :
        while c % d != 0:
            c, d = d, c % d
        return d

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

ans = B - A - ( B // C - A // C + B // D - A // D - B // L + A // L)
print(ans)

Solution (Python):
import fractions

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

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

Solution (Python):
import fractions

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

divc = B//C - (A-1)//C
divd = B//D - (A-1)//D
lcm = C*D // fractions.gcd(C, D)
divgcd = B//lcm - (A-1)//lcm

print((B-A+1) - divc - divd + divgcd)

Solution (Python):
import math


def lem(A, B):
    AB = A * B
    while B != 0:
        A, B = B, A % B
    return AB // A


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


bsep = (b // c) + (b // d) - (b // lem(c, d))
asep = (a-1) // c + (a-1) // d - (a-1) // lem(c, d)

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

Solution (Python):
import math


def lem(A, B):
    AB = A * B
    while B != 0:
        A, B = B, A % B
    return AB // A


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) // lcm(C, D)
z2 = B // lcm(C, D)
z = z2 - z1




not_d = B - (A+1) - (x + y - z)
print(int(not_d))
 """

total = b - a+1
bsep = (b // c) + (b // d) - (b // lem(c, d))
asep = (a-1) // c + (a-1) // d - (a-1) // lem(c, d)

ans = total - bsep + asep
print(ans)

Solution (Python):
""" import math


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


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) // lcm(C, D)
z2 = B // lcm(C, D)
z = z2 - z1




not_d = B - (A+1) - (x + y - z)
print(int(not_d))
 """

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

total = b - a + 1


def lem(A, B):
	AB = A * B
	while B != 0:
		A, B = B, A % B
	return AB // A


bsep = (b // c) + (b // d) - (b // lem(c, d))
asep = (a-1) // c + (a-1) // d - (a-1) // lem(c, d)

ans = total - bsep + asep
print(ans)

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

total = b - a + 1

def lem(A,B):
	AB = A * B
	while B != 0:
		A,B = B,A%B
	return AB // A

bsep = (b // c) + (b // d) - (b // lem(c,d))
asep = (a-1) // c + (a-1) // d - (a-1) // lem(c,d)

ans = total - bsep + asep
print(ans)

Solution (Python):
def gcd(i,j):
  if i > j:
    tmp = i
    i = j
    j = tmp
  while i != 0:
    tmp = i
    i = j%i
    j = tmp
  return j
def lcm(i,j):
  return i*j//gcd(i,j)  
def main():
  a,b,c,d = map(int,input().split())
  ans = b-a+1
  ans -= (b//c - (a-1)//c) + (b//d - (a-1)//d) - (b//lcm(c,d) -(a-1)//lcm(c,d))
  print(ans)


main()

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 lcm(x,y):
    return x*y//gcd(x,y)
p=lcm(c,d)
def f(n):
    return b//n-(a-1)//n
ans=b-a+1-f(c)-f(d)+f(p)
print(ans)

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

gcd = fractions.gcd(C, D)
lcm = C * D // gcd
a_c = (A-1) // C
a_d = (A-1) // D
b_c = B // C
b_d = B // D
a_lcm = (A-1) // lcm
b_lcm = B // lcm
count = (B - A + 1) - ((b_c - a_c) + (b_d - a_d) - (b_lcm - a_lcm))
print(int(count))

Solution (Python):
def gcd(a: int, b: int) -> int:
    return gcd(b, a % b) if b > 0 else a


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


def calc(n: int, div: int) -> int:
    assert div > 0
    return n // div


def calc2(l: int, r: int, div: int) -> int:
    assert r >= l
    assert div > 0
    return calc(r, div) - calc(l - 1, div)


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

k = calc2(A, B, C) + calc2(A, B, D) - calc2(A, B, lcm)
print(B - A + 1 - k)

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

A,B,C,D = map(int, input().split())
B_C = B // C
B_D = B // D
g = gcd(C,D)
l = C * D // g
duplicate_B = B // l
answer_B = B - B_C - B_D + duplicate_B

A_C = (A - 1) // C
A_D = (A - 1) // D
duplicate_A = (A - 1) // l
answer_A = (A - 1) - A_C - A_D + duplicate_A
print(int(answer_B - answer_A))

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

b_c = b // c
s_c = (a-1) // c
C = b_c - s_c

b_d = b // d
s_d = (a-1) // d
D = b_d - s_d

b_cd = b // f
s_cd = (a-1) // f
CD = b_cd - s_cd

ans = b - a + 1 - (C + D - CD)
print(ans)

Solution (Python):
import sys
import fractions
input = sys.stdin.readline
def lcm(x, y):
  return (x * y) // fractions.gcd(x, y)


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


print(ans)

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

def judge(a,c,d):
  x = a//c
  y = a//d
  z = a//lcm(d,c)
  return a-x-y+z

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


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

Solution (Python):
import fractions

a, b, c, d = map(int, input().split())
one_to_b = b // c + b // d
one_to_a = (a - 1) // c + (a - 1) // d
l = c * d // fractions.gcd(c, d)
dup = b // l - (a - 1) // l
dividable = one_to_b - one_to_a - dup
print(b - a + 1 - dividable)

Solution (Python):
import fractions

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

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

Solution (Python):
import fractions

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

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

cc=b//c - (a-1)//c
dc=b//d - (a-1)//d
cdc=b//cd - (a-1)//cd

print(b-(a-1)-cc-dc+cdc)

Solution (Python):
import fractions

d = [int(x) for x in input().split()]
lcm = int(d[2] * d[3] // fractions.gcd(d[2], d[3]))
b = d[1] - int(d[1] // d[2]) - int(d[1] // d[3]) + int(d[1] // lcm)
a_1 = d[0] - 1
a = a_1 - int(a_1 // d[2]) - int(a_1 // d[3]) + int(a_1 // lcm)
print(b - a)

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

def eg(a, b):
    amari = max(a,b)%min(a,b)
    if amari == 0:
        return min(a,b)
    return eg(min(a,b), amari)

g = eg(C, D)
l = C*D // g

countB = B - (B // C + B // D) + B // l
countA = (A-1) - ((A-1) // C + (A-1) // D) + (A-1) // l

print(countB - countA)

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

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

a_cnt = (A -1) - (((A - 1) // C) + ((A - 1) // D) - ((A - 1) // (lcm)))
b_cnt = B - ((B // C) + (B // D) - (B // (lcm)))

print(b_cnt - a_cnt)

Solution (Python):
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def euclid(a, b):
    if a > b:
        large = a
        small = b
    else:
        large = b
        small = a

    while small != 0:
        r = large % small
        large = small
        small = r

    return large


def solve(inputs):
    [A, B, C, D] = string_to_int(inputs[0])
    cd_gcd = euclid(C, D)
    cd_lcm = (C*D) // cd_gcd

    a_c = (A-1) // C
    a_d = (A-1) // D
    a_cd = (A-1) // cd_lcm

    b_c = B // C
    b_d = B // D
    b_cd = B // cd_lcm

    under_a = int((A-1) - a_c - a_d + a_cd)
    under_b = int(B - b_c - b_d + b_cd)

    return under_b - under_a


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)

Solution (Python):
import fractions

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

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

ans_A = int((A-1) - (A-1)//C - (A-1)//D + (A-1)//lcm)
ans_B = int(B - B//C - B//D + B//lcm)
print(ans_B - ans_A)

Solution (Python):

import fractions

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

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

maxC = B // C
maxD = B // D
maxlcm = B // lcm

minC = (A-1) // C
minD = (A-1) // D
minlcm = (A-1) // lcm

lcmB = (maxC + maxD - maxlcm)
lcmA = (minC + minD - minlcm)

print((B-A+1)-(lcmB-lcmA))

Solution (Python):
def gcd(x,y):
    amari = max(x,y)%min(x,y)
    if amari == 0:
        return min(x,y)
    return gcd(min(x,y), amari)
def calc_scm(x,y,gcd):
    return x*y//gcd

A,B,C,D = list(map(int, input().split()))
scm = calc_scm(C,D,gcd(C,D))
low = (A-1)//C + (A-1)//D - (A-1)//scm
high = B//C + B//D - B//scm
print(B - A + 1 - (high - low))

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

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

def f(X, a, b):
    return (X - X//a - X//b + X//lcm(a, b))

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

Solution (Python):
a,b,c,d = map(int,input().split())
a-=1
def lcm(a, b): 
    def gcd(a, b): 
        a = abs(a)
        b = abs(b)
        while b:
            a, b = b, a % b
        return a
    return a * b // gcd(a, b) 

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

Solution (Python):

import sys


def great_common_divisor(a, b):
    '''Calculate the great common divisor (GCD) of a and b
    Args:
        a (int) : value
        b (int) : value
    Returns:
        gcd (int) : the great common divisor of a and b
    Note:
        This function uses Euclidean Algorithm
    '''
    while b != 0:
        a, b = b, a % b
    return a

def least_common_multiple(a, b):
    '''Calculate the least common multiple (LCM) of a and b
    Args:
        a (int) : value
        b (int) : value
    Returns:
       lcm (int) : the least common multiple of a and b
    '''
    return (a * b) // great_common_divisor(a, b)



def solve(A: int, B: int, C: int, D: int):
    lcm = least_common_multiple(C, D)

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

    
    A = A - 1
    num_A = (A - A // C - A // D + A // lcm)

    print(num_B - num_A)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    D = int(next(tokens))  
    solve(A, B, C, D)

if __name__ == '__main__':
    main()

Solution (Python):
def gcd(p, q):
    if p % q == 0:
        return q
    else:
        return gcd(q, p % q)


def lcm(p, q):
    return p * q // gcd(p, q)


a, b, c, d = list(map(int, input().split()))
a_ = a - 1
bn = b - (b // c + b // d - b // lcm(c, d))
an = a_ - (a_ // c + a_ // d - a_ // lcm(c, d))
n = bn - an
print(n)

Solution (Python):
def calc_gcd(x,y):
    amari = max(x,y)%min(x,y)
    if amari == 0:
        return min(x,y)
    return calc_gcd(min(x,y), amari)
def calc_scm(x,y,gcd):
    return x*y//gcd

A,B,C,D = list(map(int, input().split()))
scm = calc_scm(C,D,calc_gcd(C,D))
low = (A-1)//C + (A-1)//D - (A-1)//scm
high = B//C + B//D - B//scm
print(B - A + 1 - (high - low))

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 math
a,b,c,d=map(int, input().split())
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
	
e = round(c * d / gcd(c, d))

f=(a-1)//c
g=b//c
h=(a-1)//d 
k=b//d
l=(a-1)//e
m=b//e

x=(b-a+1)
y=g-f
z=k-h
w=m-l

print(x-y-z+w)

Solution (Python):
import fractions


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


if __name__ == '__main__':
    A, B, C, D = map(int, input().split())

    
    between_A_B = B - A + 1

    
    max_num_diveided_by_C = (B//C)

    
    min_num_diveided_by_C = ((A-1)//C)

    
    max_num_diveided_by_D = (B//D)

    
    min_num_diveided_by_D = ((A-1)//D)

    
    max_num_diveided_by_CD = (B//lcm(C, D))

    
    min_num_diveided_by_CD = ((A-1)//lcm(C, D))

    answer = between_A_B - ((max_num_diveided_by_C - min_num_diveided_by_C) + (max_num_diveided_by_D - min_num_diveided_by_D) - (max_num_diveided_by_CD - min_num_diveided_by_CD))
    print(answer)

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


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


def count(n, c, d):
    n_c = n // c
    n_d = n // d
    g = gcd(c, d)
    n_cd = n // (c * d // g)
    return n - n_c - n_d + n_cd


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

Solution (Python):
a,b,c,d = map(int,input().split())
c_=0
d_=0
n=0
if a%c!=0 and b>c:
    c_= a+c-a%c 
    n+=1
elif b<c:
    c_= b
else:
    c_ = a
    n+=1
if a%d!=0 and b>d:
    d_= a+d-a%d 
    n+=1
elif b<d:
    d_= b
else:
    d_ = a
    n+=1
r = max(c,d)%min(c,d)
b_=min(c,d)
while r!=0:
    a_= b_
    b_=r
    r=a_%b_
e = int(c*d/b_)
if a%e!=0 and b>e:
    e_= a+e-a%e 
    n-=1
elif b<e:
    e_= b
else:
    e_ = a
    n-=1
n += (b-c_)//c+(b-d_)//d-(b-e_)//e
print(1+b-a-n)

Solution (Python):
import fractions

abcd = list(map(int, input().split()))
a = abcd[0]
b = abcd[1]
c = abcd[2]
d = abcd[3]

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

a_count = aa - (aa // c) - (aa // d) + (aa // lcm)
b_count = b - (b // c) - (b // d) + (b // lcm)

print(b_count - a_count)

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

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

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

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

divC = B//C - (A-1)//C
divD = B//D - (A-1)//D
divCD = B//CD - (A-1)//CD

cnt = B-A+1 - divC - divD + divCD
print(cnt)

Solution (Python):
import math
a,b,c,d=map(int, input().split())
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
	
e = (c * d // gcd(c, d))
 
 
f=(a+c-1)//c
g=b//c
h=(a+d-1)//d
k=b//d
l=(a+e-1)//e
m=b//e
x=b-a+1
y=g-f+1
z=k-h+1
w=m-l+1
print(x-y-z+w)