Problem p02995 (python)

Fixed submission:
import fractions


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


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

c_multiple_last = b // c
d_multiple_last = b // d
cd_multiple_last = b // lcm(c, d)

c_multiple_first = (a-1) // c
d_multiple_first = (a-1) // d
cd_multiple_first = (a-1) // lcm(c, d)

c_count = c_multiple_last - c_multiple_first
d_count = d_multiple_last - d_multiple_first
cd_count = cd_multiple_last - cd_multiple_first

a_to_b_count = b - a + 1
ans = a_to_b_count - (c_count + d_count - cd_count)
print(ans)

Buggy submission (verdict: Runtime Error):
a, b, c, d = map(int, input().split())
c_flag = 0
d_flag = 0
cd_flag = 0

def gcd_strategy():
    import math
    import fractions
    return math.gcd if hasattr(math, "gcd") else fractions.gcd


gcd = gcd_strategy()


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


c_multiple_last = b // c
d_multiple_last = b // d
cd_multiple_last = b // lcm(c, d)

for i in range(a, b+1):
    if i % c == 0 and c_flag == 0:
        c_multiple_first = i // c
        c_flag = 1
    if i % d == 0 and d_flag == 0:
        d_multiple_first = i // d
        d_flag = 1
    if i % lcm(c, d) == 0 and cd_flag == 0:
        cd_multiple_first = i // lcm(c, d)
        cd_flag = 1
    if c_flag == 1 and d_flag == 1 and cd_flag == 1:
        break

c_count = c_multiple_last - c_multiple_first + 1
d_count = d_multiple_last - d_multiple_first + 1
cd_count = cd_multiple_last - cd_multiple_first + 1

a_to_b_count = b - a + 1
ans = a_to_b_count - (c_count + d_count - cd_count)
print(ans)

Buggy submission (verdict: Runtime Error):
import math

a, b, c, d = map(int, input().split())
c_flag = 0
d_flag = 0
cd_flag = 0


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


c_multiple_last = b // c
d_multiple_last = b // d
cd_multiple_last = b // lcm(c, d)

for i in range(a, b+1):
    if i % c == 0 and c_flag == 0:
        c_multiple_first = i // c
        c_flag = 1
    if i % d == 0 and d_flag == 0:
        d_multiple_first = i // d
        d_flag = 1
    if i % lcm(c, d) == 0 and cd_flag == 0:
        cd_multiple_first = i // lcm(c, d)
        cd_flag = 1
    if c_flag == 1 and d_flag == 1 and cd_flag == 1:
        break

c_count = c_multiple_last - c_multiple_first + 1
d_count = d_multiple_last - d_multiple_first + 1
cd_count = cd_multiple_last - cd_multiple_first + 1

a_to_b_count = b - a + 1
ans = a_to_b_count - (c_count + d_count - cd_count)
print(ans)

Buggy submission (verdict: Runtime Error):
import math

a, b, c, d = map(int, input().split())
c_flag = 0
d_flag = 0
cd_flag = 0


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


c_multiple_last = b // c
d_multiple_last = b // d
cd_multiple_last = b // lcm(c, d)

for i in range(a, b+1):
    if i % c == 0 and c_flag == 0:
        c_multiple_first = i // c
        c_flag = 1
    if i % d == 0 and d_flag == 0:
        d_multiple_first = i // d
        d_flag = 1
    if i % lcm(c, d) == 0 and cd_flag == 0:
        cd_multiple_first = i // lcm(c, d)
        cd_flag = 1
    if c_flag == 1 and d_flag == 1 and cd_flag == 1:
        break

c_count = c_multiple_last - c_multiple_first + 1
d_count = d_multiple_last - d_multiple_first + 1
cd_count = cd_multiple_last - cd_multiple_first + 1

a_to_b_count = b - a + 1
ans = a_to_b_count - (c_count + d_count - cd_count)
print(ans)