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

import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
div_c = B // C
div_d = B // D
cd = lcm(C, D)
div_cd = B // cd
B_ = B - (div_c + div_d - div_cd)

A -= 1
div_c2 = A // C
div_d2 = A // D
div_cd2 = A // cd
A_ = A - (div_c2 + div_d2 - div_cd2)

print(B_ - A_)

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

Solution (Python):
from fractions import gcd

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

c_div = B//C - (A-1)//C
d_div = B//D - (A-1)//D
g = C*D // gcd(C, D)
cd_div = B//g - (A-1)//g
tot = B - A + 1
print(tot - c_div - d_div + cd_div)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    

    a,b,c,d = map(int, input().split())
    def gcd(a, b):
        if b == 0:
            return a
        else:
            return gcd(b, a % b)
    
    def gcd_group(arr):
        g = gcd(arr[0], arr[1])
        for i in range(2, len(arr)):
            g = gcd(g, arr[i])
        return g
    
    def lcm(a, b):
        g = gcd(a, b)
        return (a*b)//g
    
    def lcm_group(arr):
        l = lcm(arr[0], arr[1])
        for i in range(2, len(arr)):
            l = lcm(l, arr[i])
        return l
    a -= 1
    x = a - a//c - 1 - a//d - 1 + a//lcm(c, d) + 1
    y = b - b//c - 1 - b//d - 1 + b//lcm(c, d) + 1
    print(y-x)

if __name__ == '__main__':
    main()

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

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

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

x = B - ((B//C) + (B//D) - (B//l))
A = A-1
y = A - ((A//C) + (A//D) - (A//l))

print(x - y)

Solution (Python):
def gcd(a, b):
    while b > 0:
        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())

def res(n):
  return n - n // c - n // d + n // lcm(c, d)

print(res(b) - res(a-1))

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

if a % c == 0:
    c_div = (b//c) - (a//c) + 1
else:
    c_div = (b//c) - (a//c)
if a % d == 0:
    d_div = (b//d) - (a//d) + 1
else:
    d_div = (b//d) - (a//d)
if a % lcm == 0:
    cd_div = (b//lcm) - (a//lcm) + 1
else:
    cd_div = (b//lcm) - (a//lcm)

print(b-a+1 - (c_div + d_div - cd_div))

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

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

Solution (Python):

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



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


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

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

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

P=[c,d,lcm(c,d)]
A=[]
for p in P:
    if a%p==0:
        left=a//p
    else:
        left=a//p+1
    right=b//p
    if left<=right:
        A.append(right-left+1)
    else:
        A.append(0)
print(b-a+1-A[0]-A[1]+A[2])

Solution (Python):
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

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

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

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

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

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

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

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


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


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  a, b, c, d = input_list()
  dc = b // c
  dd = b // d
  dcd = b // (lcm_base(c,d))
  bb = b - dc - dd + dcd
  
  ac = (a-1)//c
  ad = (a-1)//d
  acd = (a-1)//(lcm_base(c,d))
  aa = (a-1) - ac - ad + acd
  
  print(bb-aa)
  
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Solution (Python):

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

def calc_num(n, C, D):
    """
    0~Nまでで、C or Dで割り切れない数の個数
    """
    div_C = n//C
    div_D = n//D
    div_CD = n//(lcm(C, D))
    return n-div_C-div_D+div_CD


def solve():
    A, B, C, D = map(int, input().split())
    return calc_num(B, C, D)-calc_num(A-1, C, D)

print(solve())

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

num = (C*D)//(fractions.gcd(C, D))
if(A%C==0):
    num1 = A//C-1
else:
    num1 = A//C
if(A%D==0):
    num2 = A//D-1
else:
    num2 = A//D
if(A%num == 0):
    num3 = A//num-1
else:
    num3 = A//num
print(int(B-A+1-((B//C-num1)+(B//D-num2)-(B//num-num3))))

Solution (Python):
import math
import fractions

def ceil(x):
    return math.ceil(x)
def floor(x):
    return math.floor(x)

def countX(X) :
    if X > B :
        return 0
    minX = ceil(fractions.Fraction(A, X))
    maxX = floor(fractions.Fraction(B, X))

    return maxX - minX + 1

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

ans = B - A + 1


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

ans -= countX(C)
ans -= countX(D)
ans += countX(Z)

print(int(ans))

Solution (Python):
from fractions import gcd
A,B,C,D = (int(x) for x in input().split())
E = A-1
F = (C * D) // gcd(C,D)
nc = B // C
nc2 = E // C
nd = B // D
nd2 = E // D
ncd = int(B // F)
ncd2 = int(E // F)
print(B-A+1-nc+nc2-nd+nd2+ncd-ncd2)

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

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

print(B-A-(B//C-(-(-A//C))+B//D-(-(-A//D))-(B//X-(-(-A//X)))))

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

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

def count(n):
    c = n//C
    d = n//D
    g = gcd(C, D)
    l = C*D//g
    return n -c -d +n//l

print(count(B) - count(A-1))

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

ca = (A-1)//C 
cb = B//C

da = (A-1)//D
db = B//D

lcm = C * D //gcd(C,D)
cda = (A-1)//lcm
cdb = B//lcm

print(B-A+1-((cb-ca)+(db-da)-(cdb-cda)))

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
from fractions import gcd
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

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

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

A, B, C, D = inp_list()


lcm_cd = lcm(C, D)
B_ans = B - B // C - B // D + B // lcm_cd
A1 = A - 1
A1_ans = A1 - A1 // C - A1 // D + A1 // lcm_cd
ans = B_ans - A1_ans
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):
import fractions
a, b, c, d = map(int, input().split())
ans = 0
nc = b//c - (a-1)//c
nd = b//d - (a-1)//d
cd = c*d // fractions.gcd(c,d)
ncd = b//cd - (a-1)//cd
ans = b-a+1-nc-nd+ncd
print(ans)

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

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

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

def slice(n,c,d):
    ans=n
    ans-=(n//c)
    ans-=(n//d)
    ans+=(n//lcb(c,d))
    return ans

kai=slice(b,c,d)-slice(a-1,c,d)
print(kai)

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

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

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

max_num=lcb(c,d)
ans=(b-a+1)-((b//c)+(b//d)-(b//max_num))+(((a-1)//c)+((a-1)//d)-((a-1)//max_num))

print(ans)

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

l=C*D//fractions.gcd(C,D)
underB = B- (B//C+B//D-B//l)
underA = (A-1)- ((A-1)//C+(A-1)//D-(A-1)//l)

print(underB-underA)

Solution (Python):

def inputlist(): return [int(j) for j in input().split()]
from collections import Counter


A,B,C,D = inputlist()
from fractions import gcd
def lcm(x,y):
    return (x*y)//gcd(x,y)
cd = lcm(C,D)
de = B-A+1
dec = (B//C)-((A-1)//C)
ded = (B//D)-((A-1)//D)
decd = (B//cd)-((A-1)//cd)
print(de-dec-ded+decd)

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

Solution (Python):
import fractions

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

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





x = (b//c)-(a//c)
if a%c == 0:
    x += 1


y = (b//d)-(a//d)
if a%d == 0:
    y += 1


z = (b//lcm(c,d))-(a//lcm(c,d))
if a%(c*d) == 0:
    z += 1

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

Solution (Python):

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

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

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

Solution (Python):
import fractions
A,B,C,D = map(int,input().split())
C_wareru = B//C-(A-1)//C
D_wareru = B//D-(A-1)//D
def lcm(a,b):
    return ((a*b) // fractions.gcd(a,b))
CD = lcm(C,D)
CD_wareru = B//CD-(A-1)//CD
print(B-A+1-(C_wareru+D_wareru-CD_wareru))

Solution (Python):
import fractions
from functools import reduce

A,B,C,D=[int(x) for x in input().rstrip().split()]
cnt=0

import math

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

    
baisuu=lcm(C,D)

u_A=(A-1)-(A-1)//C-(A-1)//D+(A-1)//baisuu

u_B=B-B//C-B//D+B//baisuu

print(u_B-u_A)

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)



s = B - A + 1



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


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


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


cord = c + d - candd

s -= cord

print(s)

Solution (Python):
import fractions

def ceil(n, m):
    if n % m == 0:
        ans = n // m
    else:
        ans = (n + m) // m
    return ans

q = [0] * 3
nmul = [0] * 3

lower, upper, q[0], q[1] = [int(x) for x in input().split()]
q[2] = (q[0] * q[1]) // fractions.gcd(q[0], q[1])

for i in range(3):
    left = lower if (lower % q[i] == 0) else lower + q[i] - (lower % q[i])
    nmul[i] = ceil( upper - left + 1, q[i])

ans = upper - lower + 1 - nmul[0] - nmul[1] + nmul[2]

print(ans)

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

c_count = b//c
d_count = b//d

c_d = b//(c*d//gcd(c, d))

print(b - c_count - d_count + c_d - (a-1 - (a-1)//c - (a-1)//d + (a-1)//(c*d//gcd(c, d))))

Solution (Python):

import sys
import fractions

def solve(A: int, B: int, C: int, D: int):
    numCount = B - A + 1
    initialDivideC = A-A%C + C if A%C != 0 else A
    initialDivideD = A-A%D + D if A%D != 0 else A
    lcmCD = C*D//fractions.gcd(C,D)
    initialDivideLCM = A-A%lcmCD + lcmCD if A%lcmCD != 0 else A
    countC = (B - initialDivideC)//C + 1
    if initialDivideC > B:
        countC = 0
    countD = (B - initialDivideD)//D + 1
    if initialDivideD > B:
        countD = 0
    countLCM = (B - initialDivideLCM)//lcmCD + 1
    if initialDivideLCM > B:
        countLCM = 0
    print(numCount - countC - countD + countLCM)
    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):
from fractions import gcd
def lcm(a,b):
    return (a * b) // gcd(a, b)

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

E=lcm(C,D)

num_C=B//C-(A-1)//C
num_D=B//D-(A-1)//D
num_CD=B//E-(A-1)//E

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

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

def multiple(a, b):
    return a*b // euclid(a, b)

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

all = B - A + 1
x = B//C - (A-1)//C
y = B//D - (A-1)//D
lcm = multiple(C,D)
z = B//lcm - (A-1)//lcm

print(all - x - y + z)

Solution (Python):

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


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

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

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

print(x - y - z + w)

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

Solution (Python):
from fractions import gcd

A, B, C, D = map(int, input().split())
N = B - A + 1
lcm_CD = (C * D) // gcd(C, D)

sumB = B - B // C - B // D + B // lcm_CD
sumA = (A - 1) - (A - 1) // C - (A - 1) // D + (A - 1) // lcm_CD

print(sumB - sumA)

Solution (Python):
import fractions
A, B, C, D = map(int, input().split())
cnt = 0
ac = (A-1) // C
bc = B // C
ad = (A-1) // D
bd = B // D
gcd_b = B // (C*D//fractions.gcd(C, D))
gcd_a = (A-1) // (C*D//fractions.gcd(C, D))
total = B - A + 1
a = bc - ac
b = bd - ad
gcdx = gcd_b - gcd_a

print(total-a-b+gcdx)

Solution (Python):
import math
from decimal import Decimal

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

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

def count(a, c, d):
    tmp_a= a - math.floor(a/c) - math.floor(a/d)
    tmp_a += math.floor(a/((c*d)/gcd(c, d)))
    return tmp_a

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

Solution (Python):


from fractions import gcd









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

def lcm(x:int, y:int)->int:
    """
    return Least common multiple (最小公倍数)
    """
    return (x * y) // gcd(x, y)

e = lcm(c,d)

if c == 1 or d ==1:
    ans = 0
else:
    ans = b-a+1
    minus1 = b//c - (a-1)//c
    minus2 = b//d - (a-1)//d
    plus = b//e - (a-1)//e
    ans = ans -minus1 -minus2 +plus

print(ans)

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

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

Solution (Python):

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

ac=(A-1)//C
ad=(A-1)//D
bc=B//C
bd=B//D
ae=(A-1)//E
be=B//E

s= bc-ac
t= bd-ad
u= be-ae

print(B-A+1 - s -t +u)

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

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

ac=(A-1)//C
ad=(A-1)//D
bc=B//C
bd=B//D
ae=(A-1)//E
be=B//E

s= bc-ac
t= bd-ad
u= be-ae

print(B-A+1 - s -t +u)

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

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


a, b, c, d = [int(_) for _ in input().split()]
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):
a,b,c,d = map(int,input().split())
e = b - a + 1
ans_ca = a//c
ans_cb = b//c
ans_c = ans_cb - ans_ca
if a%c== 0:
  ans_c += 1
  
ans_da = a//d
ans_db = b//d
ans_d = ans_db - ans_da
if a%d == 0:
  ans_d += 1

x = c
y = d
for i in range(b):
  n = y%x
  if n == 0:
    break
  y = x
  x = n
z = int(c*d/x)
ans_cda = a//z
ans_cdb = b//z
ans_cd = ans_cdb - ans_cda
if a%z== 0:
  ans_cd += 1
  
print(e-(ans_c+ans_d-ans_cd))

Solution (Python):
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

Solution (Python):

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

import fractions

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


a-=1
x = a//c
y = a//d
z = a//f2
ac = x+y-z


x = b//c
y = b//d
z = b//f2
bc = x+y-z

print(b-a-(bc-ac))

Solution (Python):
import sys
from heapq import heappush, heappop
from fractions import gcd


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

if __name__ == '__main__':
    main()

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

Solution (Python):
import fractions

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

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

a -= 1

tmp_b = b - (b//c + b//d - b//c_d)
tmp_a = a - (a//c + a//d - a//c_d)

print(tmp_b-tmp_a)

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

def solve(tmp,c,d):
    num=lcm(c,d)
    ans = tmp//c + tmp//d - tmp//num
    return tmp-ans

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

Solution (Python):


import fractions







def resolve():
    A, B, C, D = map(int, input().split())
    A = A - 1
    cntA = A - A // C - A // D + A // (C * D // fractions.gcd(C, D))
    cntB = B - B // C - B // D + B // (C * D // fractions.gcd(C, D))
    print(cntB - cntA)


resolve()

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

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

c_min = a + (c - a % c) if (a % c) != 0 else a
c_max = b - b % c
d_min = a + (d - a % d) if (a % d) != 0 else a
d_max = b - b % d
e_min = a + (e - a % e) if (a % e) != 0 else a
e_max = b - b % e

n_c = (c_max - c_min) // c + 1 if c_min <= c_max else 0
n_d = (d_max - d_min) // d + 1 if d_min <= d_max else 0
n_e = (e_max - e_min) // e + 1 if e_min <= e_max else 0

ans = b - a + 1 - n_c - n_d + n_e

print(ans)

Solution (Python):
import fractions

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


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


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

Solution (Python):

from fractions import gcd
a,b,c,d=map(int,input().split())
lcm=c*d//gcd(c,d)
c_n=b//c-(a-1)//c
d_n=b//d-(a-1)//d
lcm_n=b//lcm-(a-1)//lcm
print(int(b-a+1-c_n-d_n+lcm_n))

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

B_C = B // C
B_D = B // D
B_CD = B // ((C * D) // fractions.gcd(C, D))

B_count = B - B_C - B_D + B_CD

A_C = (A - 1) // C
A_D = (A - 1) // D
A_CD = (A - 1) // ((C * D) // fractions.gcd(C, D))

A_count = (A - 1) - A_C - A_D + A_CD

print(B_count - A_count)

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

def cnt_val(n):
    cdiv = n // c
    ddiv = n // d
    ldiv = n // lcm
    return n - cdiv - ddiv + ldiv

print(cnt_val(b) - cnt_val(a-1))

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

import fractions

def calc(x,y,z):
    div_y = x//y
    div_z = x//z
    div_yz = x//( y*z//fractions.gcd(y,z) )
    return( x - div_y - div_z + div_yz  )

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

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

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

cd = lcm(c, d)

b_c = b//c
b_d = b//d
b_cd = b//cd
bb = b - (b_c + b_d - b_cd)

a_c = (a-1)//c
a_d = (a-1)//d
a_cd = (a-1)//cd
aa = (a-1) - (a_c + a_d - a_cd)

print(bb-aa)

Solution (Python):
import fractions

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

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

C_count = B//C - (A-1)//C
D_count = B//D - (A-1)//D
CD_count = B//CD - (A-1)//CD

result = total - C_count - D_count + CD_count

print(result)

Solution (Python):
from fractions import gcd

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

def solve():
    A,B,C,D = map(int, input().split())
    c = B//C - (A-1)//C
    d = B//D - (A-1)//D
    r = lcm(C,D)
    e = B//r - (A-1)//r
    ans = (B-A+1) - (c+d-e)
    return ans

print(solve())

Solution (Python):
def gcd(p, q):
    if p < q:
        p, q = q, p
    while q > 0:
        p, q = q, p%q
    return p

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

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


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


def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)


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

def calc_kukan_num(mod):
    start = A if A % mod == 0 else A + mod - (A+mod) % mod
    return (B - start) // mod + 1

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

Solution (Python):
from fractions import gcd

a, b, c, d = [int(i) for i in input().split()]
a-=1
ans = b - a
ans -= b // c - a//c
ans -= b // d - a // d
e = c * d // gcd(c, d)
ans += b // e - a // e
print(ans)

Solution (Python):
from fractions import gcd

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



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

by_C = B // C - (A - 1) // C
by_D = B // D - (A - 1) // D
by_LCM = B // LCM - (A - 1) // LCM
    
print(B - A + 1 - by_C - by_D + by_LCM)

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

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

e = int(c*d/gcd(c,d))
cde = [c,d,e]

cde1 = []
for i in cde:
    a0 = 0 if a%i != 0 else 1
    a1 = b//i - a//i + a0
    cde1.append(a1)

x,y,z = cde1
print((b-a+1) - x - y + z )

Solution (Python):
import sys
from fractions import gcd

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

A, B, C, D = lr()

def F(x):
    cnt_C = x // C
    cnt_D = x // D
    cnt_CD = x // (C*D//gcd(C, D))
    return x - cnt_C - cnt_D + cnt_CD

print(F(B) - F(A-1))

Solution (Python):
from fractions import gcd
A, B, C, D = map(int, input().split())
result = B+1 - A
def lcm(a, b):
    return (a * b) // gcd(a, b)
def wareru(min_num, max_num, waru):
    return (max_num)//waru - (min_num-1)//waru
result -= wareru(A, B, C) + wareru(A, B, D) - wareru(A, B, lcm(C,D))
print(result)

Solution (Python):
from fractions import gcd
A, B, C, D = [int(i) for i in input().split()]
result = B+1 - A
def lcm(a, b):
    return (a * b) // gcd(a, b)
def wareru(min_num, max_num, waru):
    return (max_num)//waru - (min_num-1)//waru
result -= wareru(A, B, C) + wareru(A, B, D) - wareru(A, B, lcm(C,D))
print(result)

Solution (Python):
import fractions
A, B, C, D = [int(i) for i in input().split()]
result = B+1 - A
def lcm(a, b):
    return (a * b) // fractions.gcd(a, b)
def wareru(min_num, max_num, waru):
    return (max_num)//waru - (min_num-1)//waru
result -= wareru(A, B, C) + wareru(A, B, D) - wareru(A, B, lcm(C,D))
print(result)

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

bc=b//c
bd=b//d
bcd=b//(cd)
b_cnt=b-(bc+bd-bcd)

ac=(a-1)//c
ad=(a-1)//d
acd=(a-1)//(cd)
a_cnt=a-1-(ac+ad-acd)

print(b_cnt-a_cnt)

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


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

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


a_f = a // c + a // d - a // lcm(c,d)
b_f = b // c + b // d - b // lcm(c,d)

print((b-b_f) - (a-a_f))

Solution (Python):
from fractions import gcd

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

def lcm(c,d):
  return c*d//gcd(c,d)
a -= 1
lcm_ = lcm(c,d)
print(b-a - (b//c + b//d - a//c - a//d - (b//lcm_ -a//lcm_)))

Solution (Python):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

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

A, B, C, D = lr()
cnt_divided_C = B // C - (A-1) // C
cnt_divided_D = B // D - (A-1) // D
x = C * D // gcd(C, D)
cnt_divided_CD = B // x - (A-1) // x
answer = B - (A-1) - (cnt_divided_C + cnt_divided_D - cnt_divided_CD)
print(answer)

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

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

Solution (Python):
from fractions import gcd

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

ok1=b-a+1
a-=1
ng1=b//c-a//c
ng2=b//d-a//d
lcm=(c*d)//gcd(c,d)
ok2=b//lcm-a//lcm 

print(ok1-ng1-ng2+ok2)

Solution (Python):
from fractions import gcd

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

ok1=b-a+1
ng1=b//c-a//c if a%c!=0 else b//c-a//c+1
ng2=b//d-a//d if a%d!=0 else b//d-a//d+1
lcm=(c*d)//gcd(c,d)
ok2=b//lcm-a//lcm if a%lcm!=0 else b//lcm-a//lcm+1

print(ok1-ng1-ng2+ok2)

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

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

def anti_division(n, c, d):
    return n - (n // c) - (n // d) + (n // lcm(c, d))

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

Solution (Python):
import sys
input = sys.stdin.readline
def linput():
    return list(map(int, input().split()))

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

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

def main():
    
    A,B,C,D = linput()
    
    
    
    U = B-A+1
    E = lcm(C, D)
    tC = (B//C - A//C) + (A%C==0)
    tD = (B//D - A//D) + (A%D==0)
    tE = (B//E - A//E) + (A%E==0)

    
    

    res = U - tC - tD + tE
    print(res)

main()

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

Solution (Python):

from fractions import gcd

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

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

x1 = b//c - (a-1)//c
x2 = b//d - (a-1)//d
x3 = b//cd_lcm - (a-1)//cd_lcm

print((b-(a-1))-x1-x2+x3)

Solution (Python):
import fractions

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

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

Solution (Python):
from fractions import gcd

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

cnt = B//C -(A - 1)//C + B//D - (A - 1)//D - ((B//((C * D) // gcd(C, D))) - (A - 1)//((C * D) // gcd(C, D)))
cnt1 = B - (A - 1) -cnt

print(cnt1)

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


x = b // c
y = b // d


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


z = b // e

x2 = (a-1) // c
y2 = (a-1) // d


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


z2 = (a-1) // e2


print((b - (x + y - z)) - (a - 1 - (x2 + y2 - z2)))

Solution (Python):
from fractions import gcd

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

def solve(n):
  res = n - (n // c) - (n // d) + (n // lcm)
  return res

print(int(solve(b) - solve(a-1)))

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

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

div = (c+d-j)
print(W-div)

Solution (Python):

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



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


A, B, C, D = map(int, input().split())
l = lcm(C, D)
div_C = B // C - (A - 1) // C
div_D = B // D - (A - 1) // D
div_lcm = B // l - (A - 1) // l
div = div_C + div_D - div_lcm
ans = (B - A + 1) - div
print(ans)

Solution (Python):
def cnt(A, B, X):
    A1 = -(-A // X)
    B1 = B // X
    return B1 - A1 + 1

def gcd(A, B):
    mul = A * B
    if A < B:
        A, B = B, A
    while B:
        A, B = B, A % B
    return mul // A 

A, B, C, D = map(int,input().split())
print((B - A + 1) - cnt(A, B, C) - cnt(A, B, D) + cnt(A, B, gcd(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())
ans = 0
ans += b // c - (a-1) // c
ans += b // d - (a-1) // d
l = lcm(c,d)
ans -= b // l - (a-1) // l
print(b-a+1-ans)

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

Solution (Python):
from sys import stdin
from fractions import gcd

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

a, b, c, d = (int(x) for x in stdin.readline().rstrip().split())

lcm = lcm(c, d)

total = b - (a - 1)
mc = b // c - (a - 1) // c
md = b // d - (a - 1) // d
mlcm = b // lcm - (a - 1) // lcm

print(total - (mc + md - mlcm))