Beautiful Quadruples

  • + 1 comment

    def beautifulQuadruples(a, b, c, d): MAX_VALUE = 5000 MAXN = 3001

    count1 = [[0] * MAXN for _ in range(MAX_VALUE)]  # (x, y) stored number of pair (i, j) which i^j == x && i >= x && j >= y
    count2 = [0] * MAXN  # value at x stored number of pair (i, j) which i >= x && j >= x
    
    abcd = [a, b, c, d]
    abcd.sort()
    
    for i in range(1, abcd[0] + 1):
        for j in range(i, abcd[1] + 1):
            count1[i ^ j][j] += 1
            count2[j] += 1
    
    for i in range(MAX_VALUE):
        for j in range(1, MAXN):
            count1[i][j] += count1[i][j - 1]
    
    for i in range(1, MAXN):
        count2[i] += count2[i - 1]
    
    totalCombination = 0
    count = 0
    
    for i in range(1, abcd[2] + 1):
        for j in range(i, abcd[3] + 1):
            totalCombination += count2[i]
            count += count1[i ^ j][i]
    
    return totalCombination - count
    

    Reading input and writing output

    if name == "main": import os fptr = open(os.environ['OUTPUT_PATH'], 'w')

    abcd = list(map(int, input().strip().split()))
    result = beautifulQuadruples(*abcd)
    
    fptr.write(str(result) + '\n')
    fptr.close()