Problem: 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.

Submission attempts (in order):
# coding: utf-8
import sys

# import math
# import numpy as np
# from fractions import gcd

"""Template"""


class IP:
    
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        
        return int(self.input())

    def S(self):
        
        return self.input()

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

    def SL(self):
        
        return list(map(str, self.input().split()))

    def ILS(self, n):
        
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        
        
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        
        return -(-x//y)

    def ave(self, a):
        
        return sum(a) / len(a)

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

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





def main():
    
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    
    a, b, c, d = ip.IL()
    memo = b - a + 1
    memo2 = id.lcm(c, d)
    a_c = (a-1) // c
    a_d = (a-1) // d
    b_c = b // c
    b_d = b // d
    b_cd = b // memo2
    a_cd = (a-1) // memo2

    acd = a_c + a_d
    bcd = b_c + b_d
    kaburi = b_cd - a_cd
    print(memo)
    print(a_c, a_d, b_c, b_d, b_cd, a_cd)
    print(bcd - acd - kaburi)
    ans = bcd - acd - kaburi
    print(memo - ans)


main()


# coding: utf-8
import sys

# import math
# import numpy as np
# from fractions import gcd

"""Template"""


class IP:
    
    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        
        return int(self.input())

    def S(self):
        
        return self.input()

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

    def SL(self):
        
        return list(map(str, self.input().split()))

    def ILS(self, n):
        
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        
        
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        
        return -(-x//y)

    def ave(self, a):
        
        return sum(a) / len(a)

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

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





def main():
    
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    
    a, b, c, d = ip.IL()
    memo = b - a + 1
    memo2 = id.lcm(c, d)
    a_c = (a-1) // c
    a_d = (a-1) // d
    b_c = b // c
    b_d = b // d
    b_cd = b // memo2
    a_cd = a // memo2
    if a % memo2 == 0:
        aaa = 1
    else:
        aaa = 0

    acd = a_c + a_d
    bcd = b_c + b_d
    kaburi = b_cd - a_cd
    # print(memo)
    # print(a_c, a_d, b_c, b_d, b_cd, a_cd)
    
    ans = bcd - acd - kaburi - aaa
    print(memo - ans)


main()


Verdicts: Wrong Answer

Accepted