Problem p02995 (python)

Fixed submission:
#https://atcoder.jp/contests/abc131/tasks/abc131_c

import fractions

def gcd(x, y):
    if x < y:
        x, y = y, x
    while y > 0:
        x, y = y, x % y
    return x
'''
def lcm(a,b):
	return a*b//gcd(a,b)
'''
a,b,c,d = [int(i) for i in input().split()]
'''
C = 0
D = 0
E = 0
'''
'''
for i in range(int(a / max(c,d,lcm(c,d))),b-a):
    print(i)
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
    if C != 0 and D != 0 and E != 0:
        break
'''
'''
for i in range(int(a / lcm(c,d)) - 1,int(b / lcm(c,d))+2):
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
        break

for i in range(int(a / c) - 1,int(b / c)+2):
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
        break

for i in range(int(a / d) - 1,int(b / d)+2):
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
        break
'''
#print(lcm(c,d))
#print('')
#print(C,D,E)
'''
c2 = int(b / c) - int(C / c)+1
d2 = int(b / d) - int(D / d)+1
e2 = int(b / lcm(c,d)) - int(E / lcm(c,d))+1
'''
'''
if int(b / c) == b/c:
	print('c')
	c2 += 1
if int(b / d) == b/d:
	print('d')
	d2 += 1
if int(b / lcm(c,d)) == b/lcm(c,d):
	print('e')
	e2 += 1
'''
c2 = b // c - (a-1) // c
d2 = b // d - (a-1) // d
lcm = c * d // fractions.gcd(c,d)
e2 = b // lcm - (a-1) // lcm
'''
if a % c == 0:
	#print('c')
	c2 += 1
if a % d == 0:
	#print('d')
	d2 += 1
if a % lcm(c,d) == 0:
	#print('e')
	e2 += 1
'''
'''
if E == 0:
	e2 = 0
if C == 0:
	c2 = 0
if D == 0:
	d2 = 0
'''

#print(b-a+1,c2,d2,e2)
'''
c1 = list(range(C,b+1,c))
d1 = list(range(D,b+1,d))
e1 = list(range(E,b+1,lcm(c,d)))
print(b-a+1,len(c1),len(d1),len(e1))

print(c1,d1,e1)

print(b-a+1-len(c1)-len(d1)+len(e1))
'''
print(int(b-a+1 - c2 - d2 + e2))

Buggy submission (verdict: Runtime Error):
#https://atcoder.jp/contests/abc131/tasks/abc131_c

import math

def gcd(a, b):
    if b == 0:
        return a
    else:
        while b:
            a, b = b, a % b
        return a
'''
def lcm(a,b):
	return a*b//gcd(a,b)
'''
a,b,c,d = [int(i) for i in input().split()]
'''
C = 0
D = 0
E = 0
'''
'''
for i in range(int(a / max(c,d,lcm(c,d))),b-a):
    print(i)
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
    if C != 0 and D != 0 and E != 0:
        break
'''
'''
for i in range(int(a / lcm(c,d)) - 1,int(b / lcm(c,d))+2):
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
        break

for i in range(int(a / c) - 1,int(b / c)+2):
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
        break

for i in range(int(a / d) - 1,int(b / d)+2):
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
        break
'''
#print(lcm(c,d))
#print('')
#print(C,D,E)
'''
c2 = int(b / c) - int(C / c)+1
d2 = int(b / d) - int(D / d)+1
e2 = int(b / lcm(c,d)) - int(E / lcm(c,d))+1
'''
'''
if int(b / c) == b/c:
	print('c')
	c2 += 1
if int(b / d) == b/d:
	print('d')
	d2 += 1
if int(b / lcm(c,d)) == b/lcm(c,d):
	print('e')
	e2 += 1
'''
c2 = int(b / c) - int((a-1) / c)
d2 = int(b / d) - int((a-1) / d)
e2 = int(b / (c*d/math.gcd(c,d))) - int((a-1)/(c*d/math.gcd(c,d)))
'''
if a % c == 0:
	#print('c')
	c2 += 1
if a % d == 0:
	#print('d')
	d2 += 1
if a % lcm(c,d) == 0:
	#print('e')
	e2 += 1
'''
'''
if E == 0:
	e2 = 0
if C == 0:
	c2 = 0
if D == 0:
	d2 = 0
'''

#print(b-a+1,c2,d2,e2)
'''
c1 = list(range(C,b+1,c))
d1 = list(range(D,b+1,d))
e1 = list(range(E,b+1,lcm(c,d)))
print(b-a+1,len(c1),len(d1),len(e1))

print(c1,d1,e1)

print(b-a+1-len(c1)-len(d1)+len(e1))
'''
print(int(b-a+1 - c2 - d2 + e2))

Buggy submission (verdict: Runtime Error):
#https://atcoder.jp/contests/abc131/tasks/abc131_c
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

a,b,c,d = [int(i) for i in input().split()]
C = 0
D = 0
E = 0
'''
for i in range(int(a / max(c,d,lcm(c,d))),b-a):
    print(i)
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
    if C != 0 and D != 0 and E != 0:
        break
'''
for i in range(int(a / lcm(c,d)),b-a):
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
        break

for i in range(int(a / c),b-a):
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
        break

for i in range(int(a / d),b-a):
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
        break
#print('')
#print(C,D,E)
c2 = int(b / c) - int(C / c)+1
d2 = int(b / d) - int(D / d)+1
e2 = int(b / lcm(c,d)) - int(E / lcm(c,d))+1
#print(b-a+1,c2,d2,e2)
'''
c1 = list(range(C,b+1,c))
d1 = list(range(D,b+1,d))
e1 = list(range(E,b+1,lcm(c,d)))
print(b-a+1,len(c1),len(d1),len(e1))

print(c1,d1,e1)

print(b-a+1-len(c1)-len(d1)+len(e1))
'''
print(int(b-a+1 - c2 - d2 + e2))

Buggy submission (verdict: Runtime Error):
#https://atcoder.jp/contests/abc131/tasks/abc131_c
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

a,b,c,d = [int(i) for i in input().split()]
C = 0
D = 0
E = 0
'''
for i in range(int(a / max(c,d,lcm(c,d))),b-a):
    print(i)
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
    if C != 0 and D != 0 and E != 0:
        break
'''
for i in range(int(a / lcm(c,d)),b-a):
    if lcm(c,d) * i >= a and lcm(c,d) * i <= b and E == 0:
        E = lcm(c,d) * i
        break

for i in range(int(a / c),b-a):
    if c * i >= a and c * i <= b and C == 0:
        C = c * i
        break

for i in range(int(a / d),b-a):
    if d * i >= a and d * i <= b and D == 0:
        D = d * i
        break
print('')
#print(C,D,E)
c2 = int(b / c) - int(C / c)
d2 = int(b / d) - int(D / d)
e2 = int(b / lcm(c,d)) - int(E / lcm(c,d))
print(c2,d2,e2)
'''
c1 = list(range(C,b,c))
d1 = list(range(D,b,d))
e1 = list(range(E,b,lcm(c,d)))
print(len(c1),len(d1),len(e1))
'''
#print(c1,d1,e1)

#print(b-a-len(c1)-len(d1)+len(e1))
print(int(b-a - c2 - d2 + e2))