Program to find kpr sum for all queries for a given list of numbers in Python

We need to find the kpr sum for each query in a list. Given a list of numbers and queries containing [k, p, r], we calculate the sum using XOR operations on pairs of elements within a specified range.

The formula for kpr_sum is:

$$\mathrm{{???}\_{???} =\sum_{\substack{?=?}}^{??1}\sum_{\substack{?=?+1}}^{?}(? ?(?[?]??[?]))}$$

If the sum exceeds the limit, we return the result modulo 10^9+7.

Understanding the Problem

For each query [k, p, r], we:

  • Take all pairs (i, j) where p ? i < j ? r
  • Calculate k XOR (nums[i] XOR nums[j]) for each pair
  • Sum all these values

Algorithm Steps

The solution uses bit manipulation to optimize the calculation:

  • Create cumulative bit counts for each bit position (0-19)
  • For each query, calculate pairs based on bit patterns
  • Use the fact that XOR pairs can be counted efficiently

Implementation

def solve(nums, queries):
    m = 10**9 + 7
    N = len(nums)
    q_cnt = len(queries)
    C = []
    res = []
    
    # Build cumulative bit counts for each bit position
    for i in range(20):
        R = [0]
        t = 0
        for x in nums:
            t += (x >> i) & 1
            R.append(t)
        C.append(R)
    
    # Process each query
    for j in range(q_cnt):
        K, P, R = queries[j]
        d = R - P + 1
        t = 0
        
        # Calculate for each bit position
        for i in range(20):
            n1 = C[i][R] - C[i][P-1]  # Count of 1s in range
            n0 = d - n1               # Count of 0s in range
            
            if (K >> i) & 1:
                # When K's i-th bit is 1, count pairs with same bits
                x = (n1 * (n1 - 1) + n0 * (n0 - 1)) >> 1
            else:
                # When K's i-th bit is 0, count pairs with different bits
                x = n1 * n0
            
            t = (t + (x << i)) % m
        
        res.append(t)
    
    return res

# Test the function
nums = [1, 2, 3]
queries = [[1, 1, 3], [2, 1, 3]]
result = solve(nums, queries)
print("Result:", result)
Result: [5, 4]

How It Works

The algorithm works by processing each bit position independently:

  • Cumulative counts: C[i] stores cumulative count of 1s at bit position i
  • Range queries: n1 = count of 1s, n0 = count of 0s in range [P, R]
  • Pair counting: XOR of same bits = 0, XOR of different bits = 1
  • K's influence: If K's bit is 1, we want pairs that XOR to 0; if 0, pairs that XOR to 1

Example Walkthrough

For nums = [1, 2, 3] and query [1, 1, 3]:

  • Pairs: (1,2), (1,3), (2,3)
  • XOR values: 1?2=3, 1?3=2, 2?3=1
  • Final: 1?3 + 1?2 + 1?1 = 2 + 3 + 0 = 5

Conclusion

This solution efficiently calculates kpr sums using bit manipulation and cumulative counting. The time complexity is O(20 × N + 20 × Q) where N is the array size and Q is the number of queries.

Updated on: 2026-03-26T14:25:25+05:30

388 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements