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

Solution (Python):
import math
A,B,C,D = map(int,input().split())
ans = 0
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 kosu(x):
        global C,D
        return x-(x//C + x//D - x//lcm(C,D))

print(kosu(B)-kosu(A-1))

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

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


ans_C = B//C - (A-1)//C
ans_D = B//D - (A-1)//D
CD = (C*D) // gcd(C, D)
ans_CD = B//CD - (A-1)//CD

print(B-A+1 -(ans_C + ans_D - ans_CD))

Solution (Python):
import fractions
a,b,c,d = map(int,input().split())
cnt = 0
numc = b // c - (a - 1) // c
numd = b // d - (a - 1) // d
com = c * d // fractions.gcd(c,d)
numcd = b // com - (a - 1) // com
print(int(b - a + 1 - (numc + numd - numcd)))

Solution (Python):
from fractions import gcd
from math import ceil,floor
 
A,B,C,D=map(int, input().split())
 
Cans = B//C - (A-1)//C
Dans = B//D - (A-1)//D
 
CD = C * D // gcd(C,D)
CDans = B//CD - (A-1)//CD
 
ans = (B-A+1) - (Cans + Dans - CDans)
 
print(ans)

Solution (Python):

import fractions

def main():

    
    

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

    ac = -(-a // c)
    bc = b // c

    ad = -(-a // d)
    bd = b // d

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

    acd = -(-a // cd)
    bcd = b // cd

    cm_num = bc - ac + 1
    dm_num = bd - ad + 1
    cdm_num = bcd - acd + 1

    count = cm_num + dm_num - cdm_num

    total = b - a + 1

    print(total - count)

if __name__ == '__main__':
    main()

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

ans = 0


c_n = (B // C) - ((A - 1) // C)
d_n = (B // D) - ((A - 1) // D)
cd = C * D // fractions.gcd(C,D)
cd_n = (B // cd) - ((A - 1) // cd)

ans = (B - A + 1) - (c_n + d_n - cd_n)
print(ans)

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

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

ex = A - (A // C + A // D - A // lcm)
num = B - (B // C + B // D - B // lcm)


print(num-ex)

Solution (Python):
import fractions

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

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

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

result = B-A+1-(b-am)
print(result)

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

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

a,b,c,d = map(int,input().split())
t = 0
l = b-a+1

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

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

lc = (c*d) // gcd(c,d)
lc_count = (b // lc) - ((a-1) // lc)

print(l-c_count-d_count+lc_count)

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

lcm_num = lcm(c,d)
c_num = (b//c - a//c) if a%c!=0 else (b//c - a//c + 1)
d_num = (b//d - a//d) if a%d!=0 else (b//d - a//d + 1)
ba_num = b//lcm_num - a//lcm_num if a%lcm_num!=0 else (b//lcm_num - a//lcm_num + 1)

if c == 1 or d == 1:
    print(0)
else:
    print(b-a+1-(c_num+d_num-ba_num) if b-a+1-(c_num+d_num-ba_num) > 0 else 0)

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())
total = B - A + 1

tmp = lcm(C,D) 


C_1 = B//C
C_2 = (A-1)//C
C_total = C_1 - C_2

D_1 = B//D
D_2 = (A-1)//D
D_total = D_1 - D_2

tmp_1 = B//tmp
tmp_2 = (A-1)//tmp
tmp_total = tmp_1 - tmp_2
cnt = C_total + D_total - tmp_total
print(total-cnt)

Solution (Python):
import fractions

s =input().split()
A = int(s[0])
B = int(s[1])
C = int(s[2])
D = int(s[3])

tmp = fractions.gcd(C, D)
CD = (C * D) // tmp
ac = (A-1)//C
bc = B//C
ad = (A-1)//D
bd = B//D
acd = (A-1)//CD
bcd = B//CD

cnt = B - bc - bd + bcd - (A-1) + ac + ad - acd
print(cnt)

Solution (Python):
import sys
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  



s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))


def check(A, B, X):
    x = (A-1) // X
    y = (B-0) // X
    return y - x

import fractions

def main():
    A, B, C, D = i2nn()
    
    n = B - A + 1
    
    n -= check(A, B, C)
    
    n -= check(A, B, D)
    
    E = C * D // fractions.gcd(C, D)
    n += check(A, B, E)
    print(n)

main()

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

Solution (Python):


from fractions import gcd
from math import ceil,floor

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

Cans = B//C - (A-1)//C
Dans = B//D - (A-1)//D

CD = C * D // gcd(C,D)
CDans = B//CD - (A-1)//CD

ans = (B-A+1) - (Cans + Dans - CDans)

print(ans)

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

mi = min(c, d)
ma = max(c, d)

l = int(mi ** (1 / 2))
r = []
s = mi
for i in range(1, l + 1):
    if (mi % i == 0):
        r.append([i, mi // i])
ind = len(r)
for i in range(ind):
    if ((ma * r[i][0]) % mi == 0):
        s = r[i][0]
        break
if (s == mi):
    for i in range(ind):
        if ((ma * r[ind - 1 - i][1]) % mi == 0):
            s = r[ind - 1 - i][1]
            break

e = ma * s
cl = a // c + 1 if a % c != 0 else a // c
dl = a // d + 1 if a % d != 0 else a // d
el = a // e + 1 if a % e != 0 else a // e
ch = b // c
dh = b // d
eh = b // e

print(b - a - (ch - cl + dh - dl) + eh - el)

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

allnum =  B -A +1
E = lcm(C, D)

Cnum = (B//C) - ((A-1)//C)
Dnum = (B//D) - ((A-1)//D)
Enum = (B//E) - ((A-1)//E)

num = allnum - Cnum - Dnum + Enum
print(int(num))

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

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

Solution (Python):
import fractions

a,b,c,d = map(int, input().split())
def sakana(x):
    e = (a-1)//x
    f = b//x
    g = f - e
    return g
m = sakana(c)
n = sakana(d)
o = sakana(c*d//fractions.gcd(c,d))
print(b-a+1-(m+n-o))

Solution (Python):
from fractions import gcd
a,b,c,d=map(int,input().split(' '))
r1=b-b%c
if a%c!=0:
	l1=a-a%c+c
else:
	l1=a
r2=b-b%d
if a%d!=0:
	l2=a-a%d+d
else:
	l2=a
lcm=(c*d)//gcd(c,d)
r3=b-b%lcm
if a%lcm!=0:
	l3=a-a%lcm+lcm
else:
	l3=a
nc=(r1-l1+c)//c
nd=(r2-l2+d)//d
nlcm=(r3-l3+lcm)//lcm
divi=nc+nd-nlcm
out=b-a+1
if c>a and c>b and d>a and d>b:
	print(0)
else:
	print(out-divi)

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

Bans = B - B // C - B // D + B // ((C*D) // gcd)
Am1 = A-1
Aans = Am1 - Am1 // C - Am1 // D + Am1 // ((C*D) // gcd)
print(Bans-Aans)

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

Solution (Python):
import sys
from io import StringIO
import unittest
import fractions
import math



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


def resolve():
    A, B, C, D = map(int, input().split())
    size = B - A + 1
    C_warikire = B // C - (A - 1) // C
    D_warikire = B // D - (A - 1) // D
    C_and_D_warikire = B // lcm(C, D) - (A - 1) // lcm(C, D)
    print(size - C_warikire - D_warikire + C_and_D_warikire)
resolve()

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)

s =input().split()
A = int(s[0])
B = int(s[1])
C = int(s[2])
D = int(s[3])

CD = lcm(C,D)
ac = (A-1)//C
bc = B//C
ad = (A-1)//D
bd = B//D
acd = (A-1)//CD
bcd = B//CD

cnt = B - bc - bd + bcd - (A-1) + ac + ad - acd
print(cnt)

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())
diff = B - A + 1
count_c = B//C - (A-1)//C
count_d = B//D - (A-1)//D
cd = lcm(C,D)
count_cd = B//cd - (A-1)//cd
print(diff - count_c - count_d + count_cd)

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

a,b,c,d =map(int,input().split())
e = (c*d)//gcd(c,d)
ac = a//c   
if a % c == 0:
    ac-=1
bc = b//c 

ad=a//d 
if a % d == 0:
    ad-=1
bd= b//d


acd = a//e 
if a % e == 0:
    acd-=1
bcd= b//e 


count = b-a+1
count -= bc-ac
count -= bd-ad
count += bcd-acd

print(count)

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

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

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

cd=b//c-(a-1)//c
dd=b//d-(a-1)//d
cdd=b//(cdlcm)-(a-1)//(cdlcm)

print(b-a+1-(cd+dd-cdd))

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

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

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

cd=b//c-(a-1)//c
dd=b//d-(a-1)//d
cdd=b//(cdlcm)-(a-1)//(cdlcm)

print(round(b-a+1-(cd+dd-cdd)))

Solution (Python):
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b

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

divc = B // C - (A - 1) // C
divd = B // D - (A - 1) // D
dive = B // E  - (A - 1) // E

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

Solution (Python):
import fractions
a,b,c,d=map(int,input().split())
 
s=c*d//fractions.gcd(c, d)
bk=0
ak=0
ans=0
bc=b//c
ac=a//c    
bd=b//d
ad=a//d
bsa=b//s
asa=a//s
bk=b-bc-bd+bsa
ak=a-ac-ad+asa
ans=bk-ak
if a%c==0 or a%d==0 or a%s==0:
    ans=ans
else:
    ans+=1
print(ans)

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 lcm(a,b):
      return a*b//gcd(a,b)
e = lcm(c, d)
wareru = b//c + b//d - b//e - ((a-1)//c + (a-1)//d - (a-1)//e)
print(b-a+1-wareru)

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd


def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    a, b, c, d = MAP()

    l = c * d // gcd(c, d)
    a -= 1

    kukan1 = b - b // c - b // d + b // l
    kukan2 = a - a // c - a // d + a // l

    print(kukan1 - kukan2)


if __name__ == '__main__':
    main()

Solution (Python):
a,b,c,d= map(int,input().split())
def gcd(c,d):
    while d!=0:
        c,d=d,c%d
    return c


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

a1=0
a2=0
b1=0
b2=0
e1=0
e2=0


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

if(a-1>=c):
    a1=(a-1)//c
else:
    a1=0

if(b>=c):
    b1=b//c
else:
    b1=0

if(a-1>=d):
    a2=(a-1)//d
else:
    a2=0

if(b>=d):
    b2=b//d
else:
    b2=0

if(a-1>=e):
    e1=(a-1)//e
else:
    e1=0

if(b>=e):
    e2=b//e
else:
    e2=0
        
ans=b-a+1-(b1-a1)-(b2-a2)+(e2-e1)

print(ans)

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

import fractions

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

com = lcm(c,d)

total = b - a + 1
fir = b//c - (a-1)//c
sec = b//d - (a-1)//d
rem = b//com - (a-1)//com

result = total - (fir + sec -rem)
  
print(result)

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(" "))
min_C = B//C-(A-1)//C
min_D = B//D-(A-1)//D
lcm = lcm(C,D)
min_lcm = B//lcm-(A-1)//lcm
print(B-A-min_C-min_D+min_lcm+1)

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

div_a_c = (A-1) // C
div_b_c = B // C
div_c = div_b_c - div_a_c

div_a_d = (A-1) // D
div_b_d = B // D
div_d = div_b_d - div_a_d

lcm = C * D // gcd(C, D)
div_a_l = (A-1) // lcm
div_b_l = B // lcm
div_l = div_b_l - div_a_l

divs = div_c + div_d - div_l
print(B-A+1-divs)

Solution (Python):
import fractions

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

cd = (C * D) // fractions.gcd(C, D)
bc = int(B // C)
bd = int(B // D)
bcd = int(B // cd)
ac = int((A-1) // C)
ad = int((A-1) // D)
acd =int((A-1) // cd)

print((B - bc - bd + bcd) - (A-1 - ac - ad + acd))

Solution (Python):
import fractions

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

ans = 0

if c % d == 0:
    print((b - a + 1) - ((b)//d - (a-1)//d))
elif d % c == 0:
    print((b - a + 1) - ((b)//c - (a-1)//c))
else:
    tot = c * d // fractions.gcd(c, d)
    tmp = ((b) // d - (a-1) // d) + ((b)//c - (a-1)//c) - ((b) // tot - (a-1) // tot)
    print(int((b - a + 1) - tmp))

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

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
  
cd = lcm(c,d)
dab = b -a

if a%c != 0:
  cf = a + (c - a%c)
else:
  cf = a
ce = b - (b%c)
if a%d != 0:
  df = a + (d - a%d)
else:
  df = a
de = b - (b%d)
if a%cd != 0:
  cdf = a + (cd - a%cd)
else:
  cdf = a
cde = b - (b%cd)
nc = (ce-cf + c )//c
nd = (de-df + d )//d
ncd = (cde-cdf + cd )//cd
print(dab-(nc+nd-ncd)+1)

Solution (Python):
from fractions import gcd

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

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


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

Solution (Python):

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

MofD = 0
MofCD = 0

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

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


def sten(st, en, x):
    if x == 1:
        return en - st + 1
    st = st // x if st % x == 0 else st // x + 1
    en = en // x
    st *= x
    en *= x
    if en >= st:
        n = (en - st) // x + 1
    else:
        n = 0
    return n


MofC = sten(A, B, C)
if C != D:
    MofD = sten(A, B, D)
    MofCD = sten(A, B, lcm(C, D))

print(U-MofC-MofD+MofCD)

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

Solution (Python):
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a
lis=list(map(int,input().split()))
A=lis[0]
B=lis[1]
C=lis[2]
D=lis[3]
sum=B-A+1

times=C*D//gcd(C,D)
restac=A%C
restbc=B%C
restad=A%D
restbd=B%D
restat=A%times
restbt=B%times
x=int((B-restbc)//C)-int((A-restac)//C)
y=int((B-restbd)//D)-int((A-restad)//D)
z=int((B-restbt)//times)-int((A-restat)//times)
if restac==0:
  x+=1
if restad==0:
  y+=1
if(restat==0):
  z+=1
print(sum-x-y+z)

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)


def abc():
    from math import ceil
    a, b, c, d = map(int, input().split())

    cnt_all = b - a + 1

    l = ceil(a / c)
    r = b // c
    cnt_c = r - l + 1

    l = ceil(a / d)
    r = b // d
    cnt_d = r - l + 1

    cd = lcm(c, d)
    l = ceil(a / cd)
    r = b // cd
    cnt_cd = r - l + 1

    ans = cnt_all - (cnt_c + cnt_d - cnt_cd)
    if ans < 0:
        ans = 0
    print(ans)


abc()

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 lcm(a,b):
  return a*b//gcd(a,b)
a=lcm(C,D)
ans=B-A+1-(-((A-1)//(C))+B//(C)-((A-1)//(D))+B//(D)-(B//(a))+(A-1)//(a))
print(ans)

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

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

e=int(c/u(c,d)*d)
a=a-1
ac = (a // c)
bc = (b // c)
ad = (a // d)
bd = (b // d)
ae = (a // e)
be = (b // e)

print(b - a - (bc - ac + bd - ad ) + (be - ae))

Solution (Python):
A, B, C, D = map(int, input().split())
CC = B // C - A // C + (A % C == 0)
DD = B // D - A // D + (A % D == 0)
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)
EE = B // E - A // E + (A % E == 0)
print(B - A + 1 - CC - DD + EE)

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


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



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

s1 = (a-1)//c
s2 = b//c
s3 = (a-1)//d
s4 = b//d
k = lcm(c, d)
s5 = (a-1)//(k)
s6 = b//(k)

s = (s2-s1)+(s4-s3)-(s6-s5)
print(b-a+1-s)

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

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


a, b, c, d = (int(i) for i in input().split())
m = int(lcm(c, d))

print((b-a+1)-(int((b)//c)-int((a-1)//c))-(int((b)//d)-int((a-1)//d))+(int((b)//(m))-int((a-1)//(m))))

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

def div (n):
    rt = lg // n
    if lg % n != 0 and ((n- a%n) + (b%n) < n):
        rt += 1
    return(rt)

cm = c * d // fractions.gcd(c,d)
ans = lg - div(c) - div(d) + div(cm)
print(ans)

Solution (Python):
a,b,c,d=map(int,input().split())
gcdc=[]
gcdd=[]
for i in range(1,int(c**(1/2))+1):
  if c%i==0:
    gcdc.append(i)
    if i!=c//i:
      gcdc.append(c//i)
for i in range(1,int(d**(1/2))+1):
  if d%i==0:
    gcdd.append(i)
    if i!=d//i:
      gcdd.append(d//i)
E=[]
for i in range(len(gcdc)):
  if gcdc[i] in gcdd:
    E.append(gcdc[i])
e=max(E)
f=c*d//e
if a%c==0:
  g=b//c-a//c+1
else:
  g=b//c-a//c
if a%d==0:
  h=b//d-a//d+1
else:
  h=b//d-a//d
if a%f==0:
  i=b//f-a//f+1
else:
  i=b//f-a//f
print(b-a+1-g-h+i)

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

Solution (Python):
a,b,c,d=map(int, input().split())
count = 0
num=b-a+1
import fractions
lcm= int(c*d / fractions.gcd(c,d))
ac=(a-1)//c
ad=(a-1)//d
acd=(a-1)//lcm

bc=b//c
bd=b//d
bcd=b//lcm

print(num-((bc+bd-bcd)-(ac+ad-acd)))

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

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

num_lesser_than_A_non_dividable_by_C = (A - 1) - (A - 1) // C
num_lesser_than_A_non_dividable_by_D = (A - 1) - (A - 1) // D
num_lesser_than_A_non_dividable_by_C_or_D = (A - 1) - (A - 1) // lcm
num_lesser_than_A_dividable_by_C_nor_D = num_lesser_than_A_non_dividable_by_C + num_lesser_than_A_non_dividable_by_D - num_lesser_than_A_non_dividable_by_C_or_D
num_not_larger_than_than_B_non_dividable_by_C = B - B // C
num_not_larger_than_than_B_non_dividable_by_D = B - B // D
num_not_larger_than_than_B_non_dividable_by_C_or_D = B - B // lcm
num_not_larger_than_than_B_dividable_by_C_nor_D = num_not_larger_than_than_B_non_dividable_by_C + num_not_larger_than_than_B_non_dividable_by_D - num_not_larger_than_than_B_non_dividable_by_C_or_D
print(num_not_larger_than_than_B_dividable_by_C_nor_D - num_lesser_than_A_dividable_by_C_nor_D)

Solution (Python):
import fractions

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

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

num = b - a + 1
divc_b = b // c
divd_b = b // d
divcd_b = b // (lcm(c, d))
divc_a = (a-1) // c
divd_a = (a-1) // d
divcd_a = (a-1) // (lcm(c, d))

print(num - (divc_b + divd_b - divcd_b) + (divc_a + divd_a - divcd_a))

Solution (Python):
def gcd(m,n):
    x= max(m,n)
    y= min(m,n)
    if x%y == 0:
        return y
    else:
        while not x%y==0:
            z=x%y
            x= y
            y=z
        else:
            return z
def lcm(x, y):
    return (x * y) // gcd(x, y)


a,b,c,d = map(int,input().split())
lcm = lcm(c,d)
c_i=a//c
d_i=a//d
cd_i=a//lcm
if not a%c==0:
    c_i+=1
if not a%d==0:
    d_i+=1
if not a%lcm==0:
    cd_i+=1

c_f=b//c
d_f=b//d
cd_f=b//lcm

print(b-a-(c_f+d_f-cd_f-c_i-d_i+cd_i+1)+1)

Solution (Python):
import fractions

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

A -= 1

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

excluded = A - (A // C + A // D - A // lcm)
num = B - (B // C + B // D - B // lcm)

print(num - excluded)

Solution (Python):
import fractions


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


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

AtoB = (B - (A - 1))

divisible_C = (B // C) - ((A - 1) // C)
divisible_D = (B // D) - ((A - 1) // D)

minLcm = lcm(C, D)

divisible_CD = (B // minLcm) - ((A - 1) // minLcm)

divisible_CtoD = (divisible_C + divisible_D) - divisible_CD

answer = AtoB - divisible_CtoD

print(answer)

Solution (Python):
import fractions
A,B,C,D = map(int,input().split())
F = fractions.gcd(C,D)
E = C*D//F
if (A%C == 0) or (A%D == 0) :
  print((B-A)-(B//C)-(B//D)+(B//E)+(A//C)+(A//D)-(A//E))
else :
  print((B-A+1)-(B//C)-(B//D)+(B//E)+(A//C)+(A//D)-(A//E))

Solution (Python):
from fractions import gcd

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

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

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

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

print(b_sum - a_sum)

Solution (Python):
from decimal import *

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


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


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


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

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

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

N = B - A + 1;
result = N - divC - divD + divCD;
print('{:.50g}'.format(result));

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

A,B,C,D = map(int, input().split())
E = lcm(C,D)
cnt_C = 0
cnt_D = 0
cnt_E = 0
if A % C == 0:
    cnt_C += 1
if A % D == 0:
    cnt_D += 1
if A % E == 0:
    cnt_E += 1

diff = B-A
cnt_C += (diff+A%C)//C
cnt_D += (diff+A%D)//D
cnt_E += (diff+A%E)//E

print(B-A+1-cnt_C-cnt_D+cnt_E)

Solution (Python):

from math import ceil, floor
from fractions import gcd
A, B, C, D = map(int, input().split())
lcm = (C * D) // gcd(C, D)
c_cnt = B // C - (A-1) // C
d_cnt = B // D - (A-1) // D
cd_cnt = B // (lcm) - (A-1) // (lcm)
minus = c_cnt + d_cnt - cd_cnt
print(B - A - minus + 1)

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())
CD = lcm(C,D)
C_num = B//C-A//C+(1 if A%C==0 else 0)
D_num = B//D-A//D+(1 if A%D==0 else 0)
CD_num = B//CD-A//CD+(1 if A%CD==0 else 0)
print(B-A+1-C_num-D_num+CD_num)

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)

def akarabmadecde(a: int, b: int, c: int):
    if a % c == 0:
        return (b // c) - (a // c) + 1
    else:
        return (b // c) - (a // c + 1) + 1

def main():
    input_str = input()

    a, b, c, d = input_str.split()

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

    x = akarabmadecde(a, b, c)
    y = akarabmadecde(a, b, d)
    z = akarabmadecde(a, b, lcm(c, d))

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

if __name__=="__main__":
    main()

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

def gcd(i,j):
    i, j = max(i,j), min(i,j)
    if i % j == 0:
        return j
    else:
        while i % j !=0:
            k = i % j
            i, j = j, k
        else:
            return k

def solve(a, b, c, d):
    def how_many(to, divide):
        if to >= divide:
            n = to // divide
        else:
            n = 0
        return n
    count_by_c = how_many(b, c) - how_many(a - 1, c)
    count_by_d = how_many(b, d) - how_many(a - 1, d)
    
    count_by_cd = how_many(b, int(c*d / gcd(c,d))) - how_many(a - 1, int(c*d / gcd(c,d)))
    result = b - a + 1 - (count_by_c + count_by_d - count_by_cd)
    

    
    return result

def main():
    a, b, c, d = input_from_console()
    print(solve(a, b, c, d))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  
        check_cases()
    else:
        main()

Solution (Python):

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

def divisor(n): 
    i = 1
    table = []
    while i * i <= n:
        if n%i == 0:
            table.append(i)
            table.append(n//i)
        i += 1
    table = list(set(table))
    return table



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


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


sais = lcm(C, D)

buff_a = A - 1


sais_mx = B // sais
sais_mm = buff_a // sais
sais_count = sais_mx - sais_mm
if sais_mx > 0:
    sais_count = sais_mx - sais_mm
else:
    sais_count = 0




c_mx = B // C
c_mm = buff_a // C
c_count = c_mx - c_mm

d_mx = B // D
d_mm = buff_a // D
d_count = d_mx - d_mm



zentai = c_count + d_count


zentai = zentai - (sais_count)

b_a = B - A
ans = b_a - zentai

print(ans+1)

Solution (Python):
def g(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
from decimal import Decimal
A,B,C,D=map(lambda x:Decimal(int(x)),input().split())



if A%C==0:
    x=B//C-A//C+1
else:
    x=B//C-A//C
if A%D==0:
    y=B//D-A//D+1
else:
    y=B//D-A//D
E=Decimal(C*D//g(C,D))
if A%E==0:
    z=B//E-A//E+1
else:
    z=B//E-A//E
print(B-A+1-(x+y-z))

Solution (Python):
a, b, c, d = (int(i) for i in input().split())
e = 0
f = 0
g = 0
h = 0
j = 0
k = 0
l = 0
m = 0
n = 0
o = 0
count = 0

e = -(-a//c)
f = b // c 
g = f - e + 1 

h = -(-a//d)
j = b // d 
k = j - h + 1 

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(m,n):
  return int(m*n / gcd(m,n))

o = lcm(c,d)
l = -(-a//o)
m = b // o 
n = m - l + 1 

print(b-a+1-g-k+n)

Solution (Python):
import math
import fractions

def main():

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

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

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

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

    count_CD = B//int(CD) - (A-1)// int(CD)
    

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

if __name__ == '__main__':
    main()

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

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

under_c = (A - 1) // C
under_d = (A - 1) // D
under_cd = (A - 1) // lcm_cd

top_c = B // C
top_d = B // D
top_cd = B // lcm_cd

ans = (B -
       (A - 1)) - (top_c - under_c) - (top_d - under_d) + (top_cd - under_cd)

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 = list(map(int,input().split(" ")))
e = lcm(c,d)
p=(a-1)//c
o=b//c
i=(a-1)//d
u=b//d
y=(a-1)//e
t=b//e
C = max([0,o-p])
D = max([0,u-i])
E = max([0,t-y])
print( (b-a+1)-(C+D-E) )

Solution (Python):
import fractions


def main():
    A, B, C, D = map(int, input().split())
    lcm = (C * D) // fractions.gcd(C, D)
    bc = B // C
    ac = A // C
    if A % C == 0:
        ac -= 1
    bd = B // D
    ad = A // D
    if A % D == 0:
        ad -= 1
    blcm = B // lcm
    alcm = A // lcm
    if A % lcm == 0:
        alcm -= 1
    print(B - A + 1 - ((bc - ac) + (bd - ad) - (blcm - alcm)))


main()

Solution (Python):
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
abcd = input().split(" ")
a = int(abcd[0])
b = int(abcd[1])
c = int(abcd[2])
d = int(abcd[3])
cd = lcm(c, d)
print(b-a-(b//c-((a-1)//c + 1))-(b//d - ((a-1)//d + 1))+(b//cd-((a-1)//cd + 1)))

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())
c1 = (b // c) - (a // c)
if a % c == 0:
    c1 += 1
d1 = (b // d) - (a // d)
if a % d == 0:
    d1 += 1
lam1 = lcm(c, d)
cd = (b // (lam1)) - (a // (lam1))
if a % lam1 == 0:
    cd += 1
print((b - a + 1) - ((c1 + d1) - cd))

Solution (Python):

import sys;
import string;
sys.setrecursionlimit(20000) 

import copy
import itertools

from functools import lru_cache


PPP = 1000000007






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

























import re
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
  
def count(c):
  return b // c - (a-1) // c

def doit():
  res = b + 1 - a
  print(res, file=sys.stderr)
  res -= count(c)
  print(res, file=sys.stderr)
  res -= count(d)
  print(res, file=sys.stderr)
  l = lcm(c, d)
  print(l, file=sys.stderr)
  res += count(l)
  print(res, file=sys.stderr)
  return res




try:
  print(doit())
except Exception as e:
  print("e", e, file=sys.stderr)

Solution (Python):
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 int((x * y) / gcd(x, y))
a,b,c,d = map(int,input().split())
count = 0
if c == 1 or d == 1:
    print(0)
else:
    lcm = lcm(c,d)
    ans1 = (b // c - (a-1) // c + b // d - (a-1) // d - b // lcm + (a-1) // lcm)
    print(b - a + 1 - ans1)

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

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


nc = B // C - (A - 1) // C
nd = B // D - (A - 1) // D
CD = C * D // gcd(C, D)
ncd = B // CD - (A - 1) // CD
ans = (B - A + 1) - nc - nd + ncd

print(ans)

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

Solution (Python):
import math
import fractions


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


if __name__ == '__main__':
    a, b, c, d = map(int, input().split())
    warikireru_c = b // c - (- (-a // c)) + 1
    warikireru_d = b // d - (-(-a // d)) + 1
    lcm_1 = lcm(c, d)
    warikireru_lcm = b // lcm_1 - (- (-a // lcm_1)) + 1
    num = b - a + 1
    print(num - (warikireru_c + warikireru_d - warikireru_lcm))