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


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


def calcLCM(a, b):
    return a * b // calcGCD (a, b)

def countNum(ab, c, d, lcm):
    return ab // c + ab // d - ab // lcm

a, b, c, d = list(map(int, input().split()))
a -= 1
lcm = calcLCM(c, d)
print(b - a - (countNum(b, c, d, lcm) - countNum(a, c, d, lcm)))

Solution (Python):
def gcd(c,d):
    if c%d==0:
        return d
    else:
        return gcd(d,c%d)

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

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

l = lcm(c,d)
res = 0
bal = b-a+1
print(bal - ( (b//c-(a-1)//c) + (b//d-(a-1)//d) - (b//l-(a-1)//l) ))

Solution (Python):
import math
from fractions import gcd
from decimal import *
getcontext().prec = 56

def lcm(x, y):
  a = Decimal(max(x,y)) / Decimal(gcd(x, y))
  b = Decimal(min(x,y)) * Decimal(a)

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

div_c_min = math.ceil(a/c) * c
div_c_max = math.floor(b/c) * c
if div_c_max >= div_c_min:
  div_c_cnt = (div_c_max - div_c_min) / c + 1
else:
  div_c_cnt = 0
  
div_d_min = math.ceil(a/d) * d
div_d_max = math.floor(b/d) * d
if div_d_max >= div_d_min:
  div_d_cnt = (div_d_max - div_d_min) / d + 1
else:
  div_d_cnt = 0

lcm_cd = lcm(c,d)
div_cd_min = math.ceil(a/lcm_cd) * lcm_cd
div_cd_max = math.floor(b/lcm_cd) * lcm_cd
if div_cd_max >= div_cd_min:
  div_cd_cnt = (div_cd_max - div_cd_min) / lcm_cd + 1
else:
  div_cd_cnt = 0


  
print(int(b - a + 1 - (div_c_cnt + div_d_cnt - div_cd_cnt)))

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

bandc = b // c 
bandd = b // d
bandcd = b // lcm(c,d)

aandc = a // c 
aandd = a // d
aandcd = a // lcm(c,d)

totalb = b - bandc - bandd + bandcd
totala = a - aandc - aandd + aandcd


if (a % c != 0) and (a % d != 0):
    ans = totalb - totala + 1
else:
    ans = totalb - totala

print(ans)

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


a,b,c,d = map(int, input().split())
cnt = 0
ansb1 = b // c
ansb2 = b // d
ansb12 = b // (c*d//(gcd(c,d)))
bb = (ansb1 + ansb2) - ansb12

ansa1 = (a-1) // c
ansa2 = (a-1) // d
ansa12 = (a-1) // (c*d//(gcd(c,d)))
aa = (ansa1 + ansa2) - ansa12
print((b-a+1)-(bb-aa))

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

A -= 1

E = lcm(C, D)

CBdiv = B // C
DBdiv = B // D
EBdiv = B // E


CAdiv = A // C
DAdiv = A // D
EAdiv = A // E

ans = B - A - ((CBdiv - CAdiv) + (DBdiv - DAdiv) - (EBdiv - EAdiv))

print(ans)

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 = input().split(' ')

a = int(a)
b = int(b)
c = int(c)
d = int(d)

hoge = lcm(c,d)

counter = 0

counter = counter + ((b // c) + (b // d) - (b // hoge))
counter = counter - (((a - 1) // c) + ((a - 1) // d) - ((a - 1) // hoge))

print(b - a + 1 - counter)

Solution (Python):
import fractions

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

x = B // C
y = B // D
z = B // lcm(C, D)
num1 = (B - (x + y - z))

x = A // C
y = A // D
z = A // lcm(C, D)
num2 = (A - (x + y - z))
  
num = num1 - num2

if A % C != 0 and A % D != 0:
  num += 1
  
print(num)

Solution (Python):
import fractions

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

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

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

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

numl = lcm(C, D)
num1 = B // numl
num2 = (A-1) // numl
num = num1 - num2

tmp1 = c + d - num
tmp2 = B - A + 1
print(tmp2 - tmp1)

Solution (Python):
import fractions

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

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

Solution (Python):
import numpy as np
import fractions

def main():
    abcd = list(map(int, input().split(' ')))
    al = abcd[1] - abcd[0] + 1
    cc = abcd[1] // abcd[2] - (abcd[0]-1) // abcd[2]
    dd = abcd[1] // abcd[3] - (abcd[0]-1) // abcd[3]
    skbs = (abcd[2] * abcd[3]) // fractions.gcd(abcd[2], abcd[3])
    dis = cc + dd - (abcd[1] // skbs - (abcd[0]-1) // skbs)
    print(int(al - dis))

if __name__ == "__main__":
    main()

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


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


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


divAC = A // C
divAD = A // D
divACD = A // lcm(C, D)
divBC = B // C
divBD = B // D
divBCD = B // lcm(C, D)

result = 0
if A % C == 0 or A % D == 0:
    result = (B - divBC - divBD + divBCD) - (A - divAC - divAD + divACD)
else:
    result = (B - divBC - divBD + divBCD) - (A - divAC - divAD + divACD) + 1
print(result)

Solution (Python):

"""
Created on Sat Apr 20 20:47:47 2019

@author: Owner
"""

import numpy as np
import sys
import collections
import scipy.misc
import math
from operator import itemgetter
import itertools
import copy
import bisect
import fractions


def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   

def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

def find_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
    

"""
N, X = map(int, input().split())

x = list(map(int, input().split()))

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())


dp = np.zeros((N+1, 4,4,4))
    
all(nstr.count(c) for c in '753')


ABT = zip(A, B, totAB)
result = 0

sorted(ABT,key=itemgetter(2))
A, B, totAB = zip(*ABT)
A.sort(reverse=True)


(x >> i) & 1


dp = [np.inf]*N
for n in range(N):
    if n == 0:
        dp[n] = 0
    else:
        for k in range(1,K+1):
            if n-k >= 0:
                dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k]))
            else:
                break

add = 1 
res = 0
sums = [0]*(len(nums)+1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]
for i in range(0, len(nums), 2):
    left = i
    right = min(i+add, len(nums))
    tmp = sums[right] - sums[left]
    res = max(tmp, res)


li, ri = bisect.bisect_left(p_ac, l[i]-1), bisect.bisect_right(p_ac, r[i]-1)    


org_list = [3, 1, 4, 5, 2]
new_list = sorted(org_list)
print(org_list)
print(new_list)




    for h in range(0,H):
        for w in range(0,W):
            if h == 0 and w == 0:
                pass
            elif h == 0:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h-1][W-w]+1)
            elif w == 0:   
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1)
            else:
                D[H-h-1][W-w-1]= min(D[H-h-1][W-w-1], D[H-h][W-w-1]+1, D[H-h-1][W-w]+1, D[H-h][W-w]+2)
            d_max = max(D[H-h-1][W-w-1], d_max)

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


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

    
def main():
    res = 0
    tmp = 0
    if A%C == 0:
        tmp+=1    
    NC = B//C - A//C + tmp
    
    tmp = 0
    if A%D == 0:
        tmp+=1


    ND = B//D - A//D + tmp
    
    tmp=0
    lcm_CD =lcm(C, D)

    if A%lcm_CD == 0:
        tmp+=1
    lcm_CD =lcm(C, D) +tmp
    
    NCD= B//lcm_CD - A//lcm_CD 
    res = B-A+1 -(NC+ND-NCD)
    if res == -1:
        return print(0)
    
        
        
        
    
    print(res)
                
                
                
            
            
                

    
if __name__ == '__main__':
    main()

Solution (Python):
a, b, c, d = [int(_) for _ in input().split()]



def C(x):
    return x // c



def D(x):
    return x // d


gcd = lambda x, y: gcd(y, x % y) if x % y else y



def CD(x):
    return x // (c * d // gcd(c, d))



def CorD(x):
    return C(x) + D(x) - CD(x)



def CnorD(x):
    return x - CorD(x)


print(CnorD(b) - CnorD(a - 1))

Solution (Python):
import fractions
a,b,c,d=map(int,input().split())
x=b//c
y=b//d
z=b//(c*d // fractions.gcd(c,d))
xx=(a-1)//c
yy=(a-1)//d
zz=(a-1)//(c*d // fractions.gcd(c,d))
if c==1 or d==1:
    print(0)
elif c==d:
    print(b-x-(a-1-xx))
elif a==b:
    if a%c==0 or a%d==0:
        print(0)
    else:
        print(1)
else:
    print(int(b-x-y+z-(a-1-xx-yy+zz)))

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

import fractions
ccount = ((B//C*C)-((A-1)//C*C +C))//C +1
dcount = ((B//D*D)-((A-1)//D*D +D))//D +1
lcm = (C * D) // fractions.gcd(C, D)
cdcount = ((B//lcm*lcm)-((A-1)//lcm*lcm +lcm))//lcm +1
count = B - A + 1 - ccount - dcount + cdcount
print(int(count))

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

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

a = (A - 1) // C
b = B // C

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

l = lcm(C, D)

e = (A - 1) // l
f = B // l
    
res = (B - A + 1) - ((b - a) + (d - c) - (f - e))

print(res)

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

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

C_comp = (B//C) - ((A-1)//C)
D_comp = (B//D) - ((A-1)//D)
CD_comp = (B//lcm(C,D)) - ((A-1)//lcm(C,D))

print((B - A + 1) - (C_comp + D_comp - CD_comp))

Solution (Python):
def gcd(a, b):
    if a < b:
        temp = a
        a = b
        b = temp

    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b

    return b


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

c_st = (A-1) // C
c_end = B // C
c_num = c_end - c_st

d_st = (A-1) // D
d_end = B // D
d_num = d_end - d_st

E = (C * D) // gcd(C, D)
e_st = (A-1) // E
e_end = B // E
e_num = e_end - e_st

ans = B - A - c_num - d_num + e_num + 1

print(ans)

Solution (Python):
from functools import reduce
A, B, C, D = map(int, input().split())
if C == 1 or D == 1:
  print(0)
  exit()

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

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

result = 0

if A%C == 0 or A%D == 0:
  A += 1

result += (B-A+1) - (B//C - A//C) - (B//D - A//D)
l = lcm(C, D)
result += (B//l - A//l)

print("{}".format(result))

Solution (Python):
import fractions
a, b, c, d = [int(i) for i in input().split()]
gcd = c * d // fractions.gcd(c, d)
num = b - a + 1
num -= b // c - (a - 1) // c
num -= b // d - (a - 1) // d
num += b // gcd - (a - 1) // gcd
print(num)

Solution (Python):
from decimal import Decimal

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


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

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



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

print ('{:.100g}'.format(B - A + 1 - ans))

Solution (Python):
import fractions

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

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

C_bot = C * -(-A//C) 
C_top = C * (B//C) 
D_bot = D * -(-A//D)
D_top = D * (B//D)
CD_bot = lcm_CD * -(-A//(lcm_CD ))
CD_top = lcm_CD  * (B//(lcm_CD ))

num_A_B = B - A + 1
num_divC = (C_top - C_bot)//C + 1
num_divD = (D_top - D_bot)//D + 1
num_divCD = (CD_top - CD_bot)//(lcm_CD ) + 1
ans = num_A_B - num_divC - num_divD + num_divCD


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

dif = b - a + 1



divC = 0
divD = 0

if a % c == 0:
    CF = a
else:
    CF = a + (c - (a % c))

if a % d == 0:
    DF = a
else:
    DF = a + (d - (a % d))

divC = (b - CF) // c + 1
divD = (b - DF) // d + 1


kobai = lcm(c, d)
if a % kobai == 0:
    KF = a
else:
    KF = a + (kobai - (a % kobai))


divKobai = (b - KF) // kobai + 1




print(dif - (divC + divD - divKobai))

Solution (Python):
import fractions

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

sum1=(A-1)//C
sum2=B//C
sum3=sum2-sum1
sum1=(A-1)//D
sum2=B//D
sum4=sum2-sum1
num=(C*D)//fractions.gcd(C,D)
sum1=(A-1)//num
sum2=B//num
sum5=sum2-sum1
sum=sum3+sum4-sum5
sum=B-A+1-sum


print(sum)

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

def main():
    a, b, c, d = map(int, input().split())
    ans = b - a + 1
    if a%c: ans -= b//c-a//c
    else: ans -= b//c+1-a//c

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

    sks = (c*d) // gcd(c, d)
    if a%sks: ans += b//sks-a//sks
    else: ans += b//sks+1-a//sks
    print(ans)

if __name__=="__main__": main()

Solution (Python):
def sol(inp=input):
    a,b,c,d=map(int,inp().split())
    def gcd(x,y):
        while y>0:
            x,y=y,x%y
        return x
    def fix_min(b):
        c_=b//c
        d_=b//d
        cd_=b//(c*d//gcd(c,d))
        return b-c_-d_+cd_
    return fix_min(b)-fix_min(a-1)
print(sol())

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

lcm_num = lcm(C, D)

if A%C == 0:
    cnt_c = A//C - 1
else:
    cnt_c = A//C

if A%D == 0:
    cnt_d = A//D - 1
else:
    cnt_d = A//D

if A%(lcm_num) == 0:
    cnt_cd = A//(lcm_num) - 1
else:
    cnt_cd = A//(lcm_num)



num = (B//C)+(B//D)-(B//(lcm_num)) - (cnt_c + cnt_d - cnt_cd)
print(B-A+1-num)

Solution (Python):
import fractions
import math

def main():
    buf = input()
    buflist = buf.split()
    A = int(buflist[0])
    B = int(buflist[1])
    C = int(buflist[2])
    D = int(buflist[3])
    l = (C * D) // fractions.gcd(C, D)
    cA = A // C * C + (0 if A % C == 0 else C)
    cB = B // C * C
    c_count = max(0, (cB - cA) // C + 1)
    dA = A // D * D + (0 if A % D == 0 else D)
    dB = B // D * D
    d_count = max(0, (dB - dA) // D + 1)
    lA = A // l * l + (0 if A % l == 0 else l)
    lB = B // l * l
    l_count = max(0, (lB - lA) // l + 1)
    selected = c_count + d_count - l_count
    unselected = (B - A + 1) - selected
    print(unselected)

if __name__ == '__main__':
    main()

Solution (Python):
import fractions

def antidiv(x,l,c,d):
    if x == l or x < 0:
        return l - (l // c) - (l // d) + 1
    else:
        return x - (x // c) - (x // d) 

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

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


aq = a // l
ar = a % l
bq = b // l
br = b % l


qq = bq - aq - 1
r1 = antidiv(l,l,c,d) - antidiv(ar-1,l,c,d)
r2 = antidiv(br,l,c,d)

if ar!=0:
    ans = qq * antidiv(l,l,c,d) + r1 + r2
else:
    ans = (qq+1) * antidiv(l,l,c,d) + r1 + r2


print(ans)

Solution (Python):
import fractions

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

s = B - A + 1

if A % C == 0 or A % D == 0:
    A -= 1

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

E = lcm(C, D)

val1 = A // C
val2 = B // C
val3 = A // D
val4 = B // D
val5 = A // E
val6 = B // E

print(s - (val2 - val1 + val4 - val3 - (val6 - val5)))

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

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

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

count1 = (B // C - A // C)
count2 = (B // D - A // D)
count3 = (B // CD - A // CD)

if A % C == 0:
    count1 += 1

if A % D == 0:
    count2 += 1

if A % CD == 0:
    count3 += 1 

print(B - A + 1 - ((count1 + count2) - count3))

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

gcd = c
t = d
while t:
    gcd, t = t, gcd % t

lcm = c * d // gcd
e = b + 1 - a

dif += b // c - (a - 1) // c
dif += b // d - (a - 1) // d
plus += b // lcm - (a -1) // lcm

print(e - dif + plus)

Solution (Python):

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)
def lcm(a, b):
    return a * b // gcd(a,b)
a,b,c,d = map(int,input().split())
canC = int(b//c) -int((a-1)//c)
canD = int(b//d) - int((a-1)//d)
dbCD = int(b//lcm(c,d)) -int((a-1)//lcm(c,d))
print((b-a+1) -canC - canD + dbCD)

Solution (Python):
import sys
import fractions
import numpy as np

def hoge(A,B,g):
    min_r = A//g
    if A%g != 0:
        min_r += 1

    max_r = B//g

    return max_r - min_r + 1

if __name__ == '__main__' :
    A, B, C, D = list(map(int, input().split()))
    
    num_c = hoge(A,B,C)
    num_d = hoge(A,B,D)
    E = (C*D)//fractions.gcd(C,D)
    num_e = hoge(A,B,E)

    print(1+B-A-(num_c + num_d - num_e))

Solution (Python):
A,B,C,D = map(int,input().split())
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
def lcm(a, b):
    return a * b // gcd (a, b)
CD = lcm(C,D)
c_div = 0
d_div = 0
cd_div = 0
ans = 0
if A % C == 0:
    first_C = A
else:
    first_C = A + C -(A%C)
last_C = B -(B%C)
c_div = (last_C - first_C)//C + 1

if A % D == 0:
    first_D = A
else:
    first_D = A + D -(A%D)
last_D = B - (B%D)
d_div = (last_D - first_D)//D + 1

if A % CD == 0:
    first_CD =A
else:
    first_CD = A + CD -(A%CD)
last_CD = B -(B%CD)
cd_div = (last_CD - first_CD)//CD + 1

ans = c_div + d_div -cd_div
print(B-A+1-ans)

Solution (Python):
import fractions

num = input().split()
a, b, c, d = int(num[0])-1, int(num[1]), int(num[2]), int(num[3])

lcd = int(c*d/fractions.gcd(c, d))
warikire = (b//c-a//c)+(b//d-a//d)-(b//lcd-a//lcd)

print(int(b-a-warikire))

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

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


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

c_div_a = (a-1) // c
c_div_all = b // c

d_div_a = (a-1) // d
d_div_all = b // d


cd_div_a = (a-1) // int( lcm(c, d) )

cd_div_all = b // int( lcm(c, d) )



div_a = c_div_a + d_div_a - cd_div_a
div_all = c_div_all + d_div_all - cd_div_all

print( (b - (a-1)) - (div_all - div_a) )

Solution (Python):

import fractions
A, B, C, D = map(int,input().split())
if A%C ==0:
    count = 1 + B//C - A//C
else:
    count = B//C - A//C
if A%D ==0:
    count = 1 + B//D - A//D + count
else:
    count = B//D - A//D + count
lcm = C*D//fractions.gcd(C,D)
if A%lcm ==0:
    count = count - (1 + B//lcm - A//lcm)
else:
    count = count - (B//lcm - A//lcm)
print(B-A+1-count)

Solution (Python):
import fractions
A,B,C,D = map(int,input().split())
def pdw(n):
    i=2
    t=[]
    ta = t.append
    while i*i<=n:
        while n%i==0:
            n//=i
            ta(i)
        i+=1
    if n>1:ta(n)
    return t



bc = fractions.gcd(C,D)
lbc = C*D//bc

t = B - (B//C+B//D-B//lbc)
if A == 1:
    print(t)
else:
    p = A-1
    u = p - (p//C+p//D-p//lbc)
    print(t-u)

Solution (Python):
import fractions
A, B, C, D=list(map(int, input().split()))
counter = 0
lcm = int((C*D)/fractions.gcd(C,D))
cda = (A-1)//C
dda = (A-1)//D
cdb = B//C
ddb = B//D
cdda = int((A-1)//lcm)
cddb = int(B//lcm)
counter = (cdb - cda) + (ddb - dda) - (cddb - cdda)
print(int(B-A+1-counter))

Solution (Python):
a,b,c,d = list(map(int,input().split()))
ans = 0
if b % c == 0:
    ans += b//c
else:
    ans += (b- (b%c)) //c

if (a-1) % c == 0:
    ans -= (a-1)//c
else:
    ans -= ((a-1)- ((a-1)%c)) //c

ans = int(ans)
if b % d == 0:
    ans += b//d
else:
    ans += (b- (b%d)) //d

if (a-1) % d == 0:
    ans -= (a-1)//d
else:
    ans -= ((a-1)- ((a-1)%d)) //d
ans = int(ans)
e = c*d

c_2 = c
d_2 = d
if (c_2< d_2):
    tmp = c_2
    c_2 = d_2
    d_2 = tmp
r = c_2 % d_2
while r != 0:
    c_2 = d_2
    d_2 = r
    r = c_2 % d_2
e = int(e //d_2)

ans2 = 0


if b % e == 0:
    ans2 += b//e
else:
    ans2 += (b- (b%e)) //e
if (a-1) % e == 0:
    ans2 -= (a-1)//e
else:
    ans2 -= ((a-1)- ((a-1)%e)) //e

sa = ans - ans2
print(int(b-a+1-sa))

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


cnt_c = (A-1) // C
cnt_d = (A-1) // D

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

cnt_a = cnt_c + cnt_d - cnt_cd


cnt_c2 = B // C
cnt_d2 = B // D
cnt_cd2 = B // lcm(C, D)

cnt_b = cnt_c2 + cnt_d2 - cnt_cd2

cnt_ab = cnt_b - cnt_a

cnt_n = B - A + 1

print(cnt_n - cnt_ab)

Solution (Python):


import fractions

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

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

a_c = (a-1) // c
a_d = (a-1) // d
a_cd = (a-1) // lcm(c, d)

b_c = b // c
b_d = b // d
b_cd = b // lcm(c, d)


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

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

n = B - A + 1

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

if A == B:
  print(int(not( A%C==0 or A%D==0 )))
elif C != D:
  print( n- ( (B//C - (A-1)//C) + (B//D - (A-1)//D) - (B//CD - (A-1)//CD) ) )
else:
  print( n- ( (B//C - (A-1)//C) ) )

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

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

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

b_num = b - b//c - b//d + b//lcm
a_num = (a-1) - (a-1)//c - (a-1)//d + (a-1)//lcm


ans = b_num - a_num

print(ans)

Solution (Python):

import fractions

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


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

if A%C==0:
    nC_initial=A//C
else:
    nC_initial=A//C+1
    
if A%D==0:
    nD_initial=A//D
else:
    nD_initial=A//D+1

    
if A%E==0:
    nE_initial=A//E
else:
    nE_initial=A//E+1
    
nC_final=B//C
nD_final=B//D
nE_final=B//E
if nC_final<nC_initial:
    nC_final=nC_initial
if nD_final<nD_initial:
    nD_final=nD_initial
if nE_final<nE_initial:
    nE_final=nE_initial
nc=(nC_final-nC_initial+1)
nd=(nD_final-nD_initial+1)
ne=(nE_final-nE_initial+1)
if B<C:
    nc=0
    ne=0
if B<D:
    nd=0
    ne=0
if B<E:
    ne=0




print(ans-nc-nd+ne)

Solution (Python):
mod = 10**9+7
def gcd(c,d):
    
    if d%c == 0:
        return c
    else:
        return gcd(d%c,c)

def main():
    a,b,c,d=map(int,input().split(' '))
    c,d=sorted([c,d])
    lmc = int(c*d/gcd(c,d))
    e = a-1
    ans = int((b-a+1)-(b//c + b//d - b//lmc -1*(e//c + e//d - e//lmc)))
    print(ans)

main()

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

def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y) 
    
e=(c*d)//gcd(c,d)
ans3=b//e-(a-1)//e

ans=b-a+1-ans1-ans2+ans3

print(ans)

Solution (Python):
from fractions import gcd

a, b, c, d = map(int,input().split())
cn_l, cn_r, cn_d = 0,0,0
l_list = []
r_list = []
if a%c == 0:
    start_l = a//c
else:
    start_l = a//c+1

cn_l = b//c+1 - start_l



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

cn_r = b//d+1 - start_r







def lcm(x, y):
    return (x * y) // gcd(x, y)
lcm = lcm(c,d)
if a%lcm == 0:
    start_d = a//lcm
else:
    start_d = a//lcm+1
cn_d = b//lcm+1 - start_d
n = b-a+1
print(n-(cn_l+cn_r-cn_d))

Solution (Python):
import fractions as fr
a,b,c,d = input().split()
a = int(a)
b= int(b)
c = int(c)
d = int(d)
s = b-a+1

cd = c*d // fr.gcd(c,d)
a_ = ((a-1)//c + (a-1)//d) - ((a-1)//cd)
b_ = (b//c + b//d) - (b//cd)
s_ = b_- a_
print(s-s_)

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import random
input = sys.stdin.readline






















def main():

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

	E = lcm(C, D)
	if A == B:
		if A % C == 0 or A % D == 0:
			print(0)
			sys.exit()
		else:
			print(1)
			sys.exit()

	if C == D or C % D == 0 or D % C == 0:
		tmp_C2 = B // C
		tmp_C2ama = B % C

		tmp_C1 = A // C
		tmp_C1ama = A % C

		tmp = (tmp_C2-tmp_C1)

		tmp2 = 0
		if tmp_C1ama == 0:
			tmp2 -=1

		ans = B-A-tmp+1+tmp2
		print(ans)
		sys.exit()

	tmp_C2 = B // C
	tmp_C2ama = B % C
	tmp_D2 = B // D
	tmp_D2ama = B % D
	tmp_C1 = A // C
	tmp_C1ama = A % C
	tmp_D1 = A // D
	tmp_D1ama = A % D

	tmp_E2 = B // E
	tmp_E2ama = B % E
	tmp_E1 = A // E
	tmp_E1ama = A % E

	tmp = (tmp_C2+tmp_D2)-(tmp_C1+tmp_D1)

	tmp2 = 0
	if tmp_C1ama == 0:
		tmp2 -=1
	if tmp_D1ama == 0:
		tmp2 -=1
	if tmp_E1ama == 0:
		tmp2 += 1

	

	ans = B-A-tmp+1+(tmp_E2-tmp_E1)+tmp2

	print(ans)

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

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

if __name__ == "__main__":
	main()

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

Solution (Python):
from sys import stdin
import math
import fractions
from decimal import *

A, B, C, D = list(map(Decimal, stdin.readline().split()))
cd = C * D / Decimal(str(fractions.gcd(C,D)))

Nc = Decimal(B / C).quantize(Decimal("0"), rounding=ROUND_FLOOR) - Decimal(A / C).quantize(Decimal("0"), rounding=ROUND_CEILING) + Decimal("1")
Nd = Decimal(B / D).quantize(Decimal("0"), rounding=ROUND_FLOOR) - Decimal(A / D).quantize(Decimal("0"), rounding=ROUND_CEILING) + Decimal("1")
Ncd = Decimal(B / cd).quantize(Decimal("0"), rounding=ROUND_FLOOR) - Decimal(A / cd).quantize(Decimal("0"), rounding=ROUND_CEILING) + Decimal("1")

print(B-A+Decimal("1")-(Nc+Nd-Ncd))

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

lcm = lcm(C, D)

Ax = A // lcm
Ay = A // C
Az = A // D
AA = Ay + Az -Ax
a = A - AA

Bx = B // lcm
By = B // C
Bz = B // D
BB = By + Bz -Bx
b = B - BB

print(b - a)

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

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

def count(a, b, q):
  _a = min(a, b)
  _b = max(a, b)
  if a % q == 0:
    return _b // q - _a // q + 1
  else:
    return _b // q - _a // q

A, B, C, D = list(map(int, input().split()))
lcm = lcm(max(C, D), min(C, D))
print((B - A + 1) - count(A, B, C) - count(A, B, D) + count(A, B, lcm))

Solution (Python):
import fractions
list = list(map(int,input().split()))
x = (list[2] * list[3]) // fractions.gcd(list[2], list[3])
p = (list[0]-1)//list[2]
q = list[1]//list[2]
r = (list[0]-1)//list[3]
s = list[1]//list[3]
t = (list[0]-1)//x
u = list[1]//x
l = list[1]-list[0]+1
print(l+p-q+r-s-t+u)

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


def calc(x, y, z):
    ny = x // y
    nz = x // z
    nyz = x // (y * z // yaku(y ,z))

    
    

    return (x - ny - nz + nyz)


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

rtn = calc(b,c,d) - calc(a-1,c,d)

print(int(rtn))

Solution (Python):
a, b, c, d = (int(x) for x in input().split())
c_num = b//c - (a - 1)//c
d_num = b//d - (a - 1)//d

big = max(c, d)
small = min(c, d)

while big % small != 0:
    tmp = small
    small = big % small
    big = tmp

x = c * d // small

x_num = b // x - (a - 1) // x

print(int(b - a + 1 - (c_num + d_num - x_num)))

Solution (Python):
from fractions import gcd
A,B,C,D = map(int,input().split())
l = B-A+1
lcm = C//gcd(C,D)*D
b = B//C - A//C
d = B//D - A//D
lc = B//lcm - A//lcm
a1 = l-l//C-l//D+l//lcm
a2 = l-b-d+lc
print(min(a1,a2))

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

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

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

Solution (Python):
from fractions import gcd

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


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


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


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


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


print((B-A+1) - ((Ccount + Dcount - CDcross)))

Solution (Python):
import fractions

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

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

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

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










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

n_c = B//C - A//C
n_d = B//D - A//D
CD = lcm(C,D)
n_cd = B//(CD) - A//(CD)

if(A%C == 0):
    n_c += 1
if(A%D==0):
    n_d += 1
if(A%CD==0):
    n_cd += 1


if (max(C,D)%min(C,D) == 0):
    ans = (B-A+1) - max(n_c, n_d)
else:
    ans = (B-A + 1) - (n_c + n_d - n_cd)





print(ans)

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

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 lcm(x, y):
    return (x * y) // gcd(x, y)

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

Solution (Python):
import fractions

def div(a,b,c):
    val=b//c-(a-1)//c
    return val

A,B,C,D=map(int,input().split())
numC=div(A,B,C)
numD=div(A,B,D)
lcm=int(C*D/(fractions.gcd(C,D)))
numCD=div(A,B,lcm)
ans=B-A+1-numC-numD+numCD
print(ans)

Solution (Python):
import fractions

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

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

s1 = A - ((A//C) + (A//D) - (A//l))
s2 = B - ((B//C) + (B//D) - (B//l))
s3 = 0

if A%C != 0 and A%D != 0:
    s3 = 1

print(s2-s1 + s3)