Problem:
 Score : 300 points    Problem Statement You are given four integers A , B , C , and D . Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D .      Constraints  1\leq A\leq B\leq 10^{18}  1\leq C,D\leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  A  B  C  D       Output Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D .        Sample Input 1 4 9 2 3      Sample Output 1 2  5 and 7 satisfy the condition.       Sample Input 2 10 40 6 8      Sample Output 2 23       Sample Input 3 314159265358979323 846264338327950288 419716939 937510582      Sample Output 3 532105071133627368

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

Ans = []

for i in [B,A-1]:
  N = i
  E = (C * D) // gcd(C,D)
  modC = N//C
  modD = N//D
  modE = N//E
  Ans += [N - modC - modD + modE]
  
print(Ans[0]-Ans[1])

Solution (Python):

"""
Created on Fri May 15 00:52:33 2020

@author: Kanaru Sato
"""

import fractions

def divnum(x,y,n):
    if x%n == 0:
        return y//n - x//n + 1
    else:
        return y//n - x//n

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

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


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

def cnt(A, B, K):
    a = (A // K + (A % K != 0)) * K
    b = (B // K) * K
    if a > b:
        return 0
    else:
        return (b - a) // K + 1


lcm = c * d // gcd(c, d)
res1 = cnt(a, b, c)
res2 = cnt(a, b, d)
res3 = cnt(a, b, lcm)
print(b - a + 1 - res1 -res2 + res3)

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

"""
両方に関して割り切れるものの個数から公倍数で割り切れるものを除けばいい
"""
import fractions

C = b//c - (a-1)//c
D = b//d - (a-1)//d


CD = fractions.gcd(c, d)
lcm = (c * d) // CD

G = b//lcm - (a-1)//lcm


count = C + D - G
print(b - a + 1 - count)

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

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

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

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

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

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

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

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

ansB = (B-B//C)+(B-B//D)-(B-B//X)
ansA = ((A-1)-((A-1)//C))+((A-1)-(A-1)//D)-((A-1)-(A-1)//X)

print(ansB-ansA)

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


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

X = lcm(C, D)

ansB = (B-B//C)+(B-B//D)-(B-B//X)
ansA = ((A-1)-((A-1)//C))+((A-1)-(A-1)//D)-((A-1)-(A-1)//X)

print(ansB-ansA)

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


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

print(b-a+1-ans)

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

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

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

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))

from fractions import gcd
import math
def resolve():
    A,B,C,D=pin()
    E=(C*D)//gcd(C,D)
    a=[]
    for x in [C,D,E]:
        s=((A-1)%x)+1
        t=(B)%x
        
        a.append((B-A+s-t)//x)
    
    print(B-A+1-a[0]-a[1]+a[2])
    
    

resolve()

Solution (Python):
from math import floor

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

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

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

def n(n):
    c = n // C
    d = n // D
    cd = n // lcm(C,D)
    return n - c - d + cd

print(n(B)-n(A-1))

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


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

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

a1 = A // C if (A // C) * C >= A else A // C + 1
a2 = (B // C + 1) if (B // C + 1) * C <= B else B // C
a = a2 - a1 + 1

b1 = A // D if (A // D) * D >= A else A // D + 1
b2 = (B // D + 1) if (B // D + 1) * D <= B else B // D
b = b2 - b1 + 1

E = lcm(C, D)
c1 = A // E if (A // E) * E >= A else A // E + 1
c2 = (B // E + 1) if (B // E + 1) * E <= B else B // E
c = c2 - c1 + 1

ans = (B - A + 1) - (a + b - c)
print(ans)

Solution (Python):
def wareru(x,y,z):
  if x%z == 0:
    return y//z - x//z + 1
  else:
    return y//z - x//z

import fractions

a,b,c,d = map(int,input().split())
e = c*d//(fractions.gcd(c,d))
c_hoge = wareru(a,b,c)
d_hoge = wareru(a,b,d)
e_hoge = wareru(a,b,e)
print(b-a+1-c_hoge-d_hoge+e_hoge)

Solution (Python):
from fractions import gcd

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

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

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

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

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

def gcd(i, j):
    while j:
        i, j = j, i % j
    return i

def num_d(x, y, z):
    l, m, n = x//y, x//z, x//(y*z//(gcd(y, z)))
    return x - l - m + n


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

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

def gcd(i, j):
    while j:
        i, j = j, i % j
    return i

def num_d(x, y, z):
    l, m, n = x//y, x//z, x//(y*z//(gcd(y, z)))
    return l + m - n


X = b - num_d(b, c, d)
Y = a - num_d(a, c, d)

print(X - Y)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod=10**9+7

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

def lcm(a, b):
    return a // gcd(a, b) * b
a,b,c,d=MI()
u=lcm(c,d)


'''def div(x,y):
    return int(x/y) '''
'''ansb=b-div(b,c)-div(b,d)+div(b,u)
ansa=a-1-div(a-1,c)-div(a-1,d)+div(a-1,u)'''
ansb=b-b//c-b//d+b//u
ansa=a-1-(a-1)//c-(a-1)//d+(a-1)//u

print(ansb-ansa)
mod = 10**9 + 7

Solution (Python):


from fractions import gcd


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


def lcm(x: int, y: int) -> int:
    """最小公倍数を求めるメソッド

    Arguments:
        x {int} -- 整数
        y {int} -- 整数

    Returns:
        int -- 最小公倍数
    """
    return (x * y) // gcd(x, y)


def main() -> None:
    """Entry point
    """
    
    """
    CでもDでも割り切れないものの個数
    = 全体 - Cで割り切れる個数 - Dで割り切れる個数 + lcm(C,D)で割り切れる個数
    """
    div_C = (B // C) - ((A - 1) // C)
    div_D = (B // D) - ((A - 1) // D)
    lcm_CD = lcm(C, D)
    div_lcm_CD = (B // lcm_CD) - ((A - 1) // lcm_CD)
    result = (B - A + 1) - div_C - div_D + div_lcm_CD

    
    print(result)


if __name__ == "__main__":
    main()

Solution (Python):


from fractions import gcd


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


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



div_C = (B // C) - ((A - 1) // C)
div_D = (B // D) - ((A - 1) // D)
lcm_CD = lcm(C, D)
div_lcm_CD = (B // lcm_CD) - ((A - 1) // lcm_CD)
result = (B - A + 1) - div_C - div_D + div_lcm_CD


print(result)

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    A, B, C, D = readnums()
    g = C * D // gcd(C, D)
    c1 = B // C
    c2 = (A - 1) // C
    d1 = B // D
    d2 = (A - 1) // D
    g1 = B // g
    g2 = (A - 1) // g
    print(B - A + 1 + (g1 - g2) - (c1 - c2) - (d1 - d2))


if __name__ == "__main__":
    main()

Solution (Python):
import itertools
import math
import fractions
import string
import collections
from collections import Counter
from collections import deque
from operator import itemgetter
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


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


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


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
a, b, c, d = map(int, input().split())


def func(n, c, d):
    x = n//c
    y = n//d
    z = n//((c * d) // fractions.gcd(c, d))
    zz = (x+y)-z
    return zz


tmp = b-func(b, c, d)
tmp2 = (a-1)-func(a-1, c, d)
print(tmp-tmp2)

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

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

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

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

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

    def count(x, c, d) -> int:
        return x - x // c - x // d + x // lcm(c, d)

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from fractions import gcd

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

Solution (Python):













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

def lcm(a):
    LCM = a[0]
    n = len(a)
    for i in range(1, n):
        LCM = LCM * a[i] // gcd(LCM, a[i])
    return LCM

lcm = lcm([c,d])


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


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

modCD = b//(lcm) - (a-1)//(lcm)

ans = b - a + 1 - modC - modD + modCD



print(ans)

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

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

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

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

from fractions import gcd

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

A -= 1

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

def calc(x):
    count = 0
    count += x // C
    count += x // D
    count -= x // lcm
    return x - count

ans = calc(B) - calc(A)
print (ans)

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

Solution (Python):
from fractions import gcd

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

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

l = c * d // gcd(c, d)
res = f(b, c) + f(b, d) - f(b, l)
res -= f(a - 1, c) + f(a - 1, d) - f(a - 1, l)
print(b - a + 1 - res)

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

import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
A -= 1
s = lcm(C,D)
X =  A - (A//C + A //D - A//s)
Y =  B - (B//C + B //D - B//s)
print(Y-X)

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

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


A, B, C, D = MAP()

def f(N):
    a = N // C
    b = N // D
    c = N // lcm(C, D)
    
    return N - (a + b - c)
print(f(B)-f(A-1))

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

a,b,c,d = inpl()
a -= 1
e = c*d//fractions.gcd(c,d)
B = b - (b//c + b//d - b//e)
A = a - (a//c + a//d - a//e)
print(B - A)

Solution (Python):
from sys import stdin
from fractions import gcd
def main():
    
    readline=stdin.readline
    a,b,c,d=map(int,readline().split())
    a-=1
    l=c*d//gcd(c,d)
    print((b-(b//c+b//d-b//l))-(a-(a//c+a//d-a//l)))
if __name__=="__main__":
    main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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 resolve():
    a, b, c, d = map(int, input().split())

    l = lcm(c, d)

    def calc(n):
        res = n // c + n // d - n // l
        return n - res

    print(calc(b) - calc(a - 1))


if __name__ == '__main__':
    resolve()

Solution (Python):
from fractions import gcd

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

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
import fractions
a,b,c,d = iim()

def antiDiv(n):
    return n-n//c-n//d+n//(c*d//fractions.gcd(c,d))
print(antiDiv(b)-antiDiv(a-1))

Solution (Python):
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

a,b,c,d = map(int,input().split())
C = b//c-(a-1)//c
D = b//d - (a-1)//d
def gcd(a, b):
    while(b != 0):
        a, b = b, a % b
    return a
    
def lcm(m,n):
    return (m*n)//gcd(m,n)
cd = lcm(c,d)
CD = b//cd - (a-1)//cd
print(b-a+1-C-D+CD)

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

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

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

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


def lcm(a, b):
    return a * b // gcd (a, b)
E = lcm(C,D)
print( (B-A+1)-((B//C-A//C + (A%C==0)) + (B//D-A//D+ (A%D==0)) - (B//E-A//E+ (A%E==0))))

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

Solution (Python):
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
a, b, c, d = map(int, 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):
import fractions
A,B,C,D = (int(x) for x in input().split())
c = 0
d = 0
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
M = lcm(C,D)
c = B//C - A//C
if A%C == 0:
  c = c+1
d = B//D - A//D
if A%D == 0:
  d = d+1
m = B//M - A//M
if A%M == 0:
  m = m+1
print(B-A+1-(c+d-m))

Solution (Python):
from functools import reduce

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

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

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

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

lcm = lcm_list([c,d])

res1 = a - 1 - ((a - 1) // c + (a - 1) // d - (a - 1) // lcm)
res2 = b - (b // c + b // d - b // lcm)

print(res2 - res1)

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

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

from fractions import gcd
a,b,c,d = iim()


def num(i):
    n = c*d//gcd(c,d)
    return i-i//c-i//d+i//n

print(num(b)-num(a-1))

Solution (Python):
A,B,C,D = list(map(int,input().split()))
X = B//C - (A-1)//C
Y = B//D - (A-1)//D
x = max(C,D)
y = min(C,D)

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

L = (C*D)//gcd(x,y)
Z = B//L - (A-1)//L
  
print(B-A+1-X-Y+Z)

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

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

LCM = lcm(C,D)
BC = B // C
BD = B // D
BCD = B // LCM
AC = (A-1) // C
AD = (A-1) // D
ACD = (A-1) // LCM
x = B - BC - BD + BCD
y = (A-1) - AC - AD + ACD
print(x - y)

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

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

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

z=lcm(c,d)
cddead=b//z-(a-1)//z
print(total-cdead-ddead+cddead)

Solution (Python):
from fractions import gcd


def main():
    A, B, C, D = [int(x) for x in input().split()]
    l = (C * D) // gcd(C, D)
    b = B - anti(B, C, D, l)
    a = (A - 1) - anti((A - 1), C, D, l)
    print(int(b - a))


def anti(x, y, z, l):
    return (x // y) + (x // z) - (x // l)


if __name__ == "__main__":
    main()

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

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

num_b_c = b//c
num_b_d = b//d
num_b_cd = b//lcm

if a%c==0:
    num_a_c = a//c - 1
else:
    num_a_c = a//c

if a%d==0:
    num_a_d = a//d - 1
else:
    num_a_d = a//d

if a%lcm==0:
    num_a_cd = a//lcm - 1 
else:

    num_a_cd = a//lcm


alls = b - a + 1


c_div = num_b_c - num_a_c
d_div = num_b_d - num_a_d

cd_div = num_b_cd -num_a_cd


print(alls - ( c_div + d_div - cd_div))

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

def unko(x,C,D):
  import fractions
  t=int(fractions.gcd(C,D))
  t_=C*D//t
  return x-(x//C+x//D-x//t_)
print(int(unko(B,C,D)-unko(A-1,C,D)))

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):
def solve():
    A, B, C, D = map(int, input().split())
    ans = B-A+1
    gcd_CD = gcd(C, D)
    CD = (C//gcd_CD)*(D//gcd_CD)*gcd_CD
    ans -= (B//C-(A-1)//C)
    ans -= (B//D-(A-1)//D)
    ans += (B//CD-(A-1)//CD)
    print(ans)


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


solve()

Solution (Python):
from fractions import gcd
A,B,C,D = map(int,input().split())
def Num_div(num,div):
    return num//div
    
def NoDiv(num,div1,div2):
    return num-Num_div(num,div1)-Num_div(num,div2)+Num_div(num,div1*div2//gcd(div1,div2))
    
print(NoDiv(B,C,D)-NoDiv(A-1,C,D))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd

import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


a, b, c, d = Is()
def calc(num, x, y):
    l = (x*y)//gcd(x, y)
    return num - num//x - num//y + num//l

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

Solution (Python):
a,b,c,d = map(int,input().split())
a -= 1
from fractions import gcd
divac = a // c
divad = a // d
divbc = b // c
divbd = b // d
divacd = a // (c*d//gcd(c,d))
divbcd = b // (c*d//gcd(c,d))
Acd = divac + divad - divacd
Bcd = divbc + divbd - divbcd
wareru = Bcd - Acd
ans = b-a-wareru
print(ans)

Solution (Python):
from fractions import gcd

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

div_c = b//c - a//c
if a%c == 0: div_c += 1
div_d = b//d - a//d
if a%d == 0: div_d += 1
lcm_cd = c*d//gcd(c, d)
div_cd = b//lcm_cd - a//lcm_cd
if a%lcm_cd == 0: div_cd += 1
print(b - a + 1 - div_c - div_d + div_cd)

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

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

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

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

print(ans_b - ans_a)

Solution (Python):
from fractions import gcd
a,b,c,d=map(int,input().split())
startC=(a-1)//c*c+c
startD=(a-1)//d*d+d
n=b-a+1
clength=b-startC
dlength=b-startD
cdnum=0
if clength>=0:
    cdnum+=1+clength//c
if dlength>=0:
    cdnum+=1+dlength//d
cd=c*d//gcd(c,d)
startCD=(a-1)//cd*cd+cd
cdlength=b-startCD
if cdlength>=0:
    cdnum-=1+cdlength//cd
print(n-cdnum)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): return k.join(list(map(str, lst)))
INF = float('inf')

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

def f(x, c, d):
    r = x
    r -= x // c
    r -= x // d
    r += x // lcm(c, d)
    return r

def f2(n, C, D):
    return n - n//C - n//D + n//lcm(C, D)

def solve():
    a, b, c, d = MI()

    g = lcm(c, d)
    bb = b - (b // c) - (b // d) + (b // g)
    a = a - 1
    aa = a - (a // c) - (a // d) + (a // g)
    print(bb - aa)
    
    


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): return k.join(list(map(str, lst)))
INF = float('inf')

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

def f(x, c, d):
    r = x
    r -= x // c
    r -= x // d
    r += x // lcm(c, d)
    return r

def f2(n, C, D):
    return n - n//C - n//D + n//lcm(C, D)

def solve():
    a, b, c, d = MI()

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


if __name__ == '__main__':
    solve()

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

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

c = B // C - (A - 1) // C 
d = B // D - (A - 1) // D 
e = B // E - (A - 1) // E
ans = B - A - c - d + e + 1
print(ans)

Solution (Python):
from fractions import gcd

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


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

print(ans)

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

Solution (Python):


def euclid(m, n):
    m0 = m = abs(m)
    n0 = n = abs(n)
    while n != 0:
        m, n = n, m % n
    qm = m0 // m
    lcm = qm * n0
    return lcm


def check(a, b, c):
    x = a // c
    y = b // c
    if x * c < a:
        x += 1
    return y - x + 1

a, b, c, d = map(int, input().split())
nc = check(a, b, c)
nd = check(a, b, d)
ncd = check(a, b, euclid(c, d))
print(b - a + 1 -(nc + nd - ncd))

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

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

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

Solution (Python):


from fractions import gcd

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

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

total_C = B // C
total_D = B // D
total_CxD = B // CxD

sub_C = (A - 1) // C
sub_D = (A - 1) // D
sub_CxD = (A - 1) // CxD

print((B - (A - 1)) -
        (total_C + total_D - total_CxD) + (sub_C + sub_D - sub_CxD))

Solution (Python):
import sys






stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18



a,b,c,d = map(int, stdin.readline().rstrip().split())


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

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


cd = lcm(c,d)


cwari = b//c
dwari = b//d
cdwari = b//cd
ub = b-cwari-dwari+cdwari


cwari = (a-1)//c
dwari = (a-1)//d
cdwari = (a-1)//cd
ua = (a-1)-cwari-dwari+cdwari

print(ub-ua)

Solution (Python):
import fractions
A,B,C,D=map(int,input().split())
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
E=lcm(C,D)
a=A-1
ans=(a//C)+(a//D)-(a//E)
ans2=(B//C)+(B//D)-(B//E)
print(B-A+1-(ans2-ans))

Solution (Python):
def gcd(a, b):
  while b:
    a, b = b, a % b
  return a
def lcm(x, y):
  return (x * y) // gcd(x, y)
A, B, C, D = map(int, input().split(' '))
can_dived_c = B // C - (A - 1) // C
can_dived_d = B // D - (A - 1) // D
can_dived_both = B // lcm(C, D) - (A - 1) // lcm(C, D)
res = B - (A - 1) - can_dived_c - can_dived_d + can_dived_both
print(res)

Solution (Python):
import sys
input = sys.stdin.readline
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

def main():
    A, B, C, D = map(int, input().split())
    g = gcd(max(C,D),min(C,D))
    l = C*D//g
    NB = B - B//C - B//D + B//l
    NA = (A-1) - (A-1)//C - (A-1)//D + (A-1)//l
    ans = NB - NA
    print(int(ans))

if __name__ == '__main__':
    main()

Solution (Python):
import fractions

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

def count(n):
  r=b//n
  l=a//n
  if a%n!=0:
     l+=1
  return r-l+1

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

print(ans)

Solution (Python):
import fractions

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

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


def f(n):
    return b//n - (a-1)//n
from fractions import gcd
lc = c*d//gcd(c,d)

print((b-a+1) - (f(c)+f(d)-f(lc)))

Solution (Python):
import fractions

a, b, c, d = map(int, input().split())
lcm_cd = (c * d) // fractions.gcd(c, d)
total = b - a + 1
d_c = b // c - (a - 1) // c
d_d = b // d - (a - 1) // d
d_cd = b // lcm_cd - (a - 1) // lcm_cd
print(int(total - (d_c + d_d - d_cd)))

Solution (Python):
a,b,c,d=map(int,input().split())
lcm=0
cc=min(c,d)
dd=max(c,d)

while cc>0:
  amari=dd%cc
  dd=cc
  cc=amari

lcm=c*d//dd
  
bika=b-b//c-b//d+b//lcm
amiman=a-1-(a-1)//c-(a-1)//d+(a-1)//lcm
print(bika-amiman)

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)

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

Solution (Python):


import sys
read = sys.stdin.buffer.read 
readline = sys.stdin.buffer.readline 
readlines = sys.stdin.buffer.readlines 
sys.setrecursionlimit(10**7) 

"""

n = readline().rstrip() 
lis = readline().rstrip().split() 


n = int(readline().rstrip()) 
a, b, c = map(int,readline().rstrip().split()) 
A = list(map(int,readline().rstrip().split()))  
lis = [readline().rstrip().split() for _ in range(n)] 


s = sys.stdin.readlines()
n, k=map(int, s[0].split())
d=[]
A=[]
for i in s[1:2*k:2]:
    d.append(int(i))
for i in s[2:2*k+1:2]:
    A.append(list(map(int, i.split())))
"""

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

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


shou1=b//c
shou2=b//d
shou3=b//f2
num_b=b-(shou1+shou2-shou3)


shou4=(a-1)//c
shou5=(a-1)//d
shou6=(a-1)//f2
num_a=(a-1)-(shou4+shou5-shou6)

print(num_b-num_a)

Solution (Python):
from fractions import gcd  


def main():
    A, B, C, D = list(map(int, input().split(' ')))
    gcd_cd = gcd(C, D)
    lcm_cd = C * D // gcd_cd
    A -= 1
    a = A - A // C - A // D + A // lcm_cd
    b = B - B // C - B // D + B // lcm_cd
    print(b - a)


if __name__ == '__main__':
    main()

Solution (Python):
import fractions

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