Captain Jack Sparrow#12293

Captain Jack Sparrow (Editorial)

Problem Author:

Akbar Akbar

Prepared by:

Hasan Saleh (MathModel)

Edited by:

Hasan Saleh (MathModel)

Solution

We need to compute, for each test case, the value

\sum_{l=a}^{b}\sum_{r=l}^{b}\big((l\oplus r)+(l\&r)\big)\cdot(r-l)\pmod{10^9+7}.

The terms with r=l contribute zero because of the factor (r-l), so we can rewrite the sum over all pairs with a\le l<r\le b. The key step is to remove the XOR using the standard identity

l + r = (l \oplus r) + 2(l \& r) \quad\Rightarrow\quad (l\oplus r) = l + r - 2(l\&r),

therefore

(l\oplus r)+(l\&r) = l + r - (l\&r).

So the answer becomes

\mathsf{Ans} = \sum_{a\le l<r\le b} (l + r - (l\&r))(r-l) = S_1 - S_2,

where

S_1 = \sum_{a\le l<r\le b}(l+r)(r-l), \qquad S_2 = \sum_{a\le l<r\le b}(l\&r)(r-l).

To make the notation cleaner, we reindex. Let n = b-a+1 and define x_i = a+i for i=0,\dots,n-1. Any pair (l,r) with a\le l<r\le b corresponds to (x_i,x_j) with 0\le i<j<n, and

r - l = x_j - x_i = (a+j)-(a+i) = j-i.

Then

S_1 = \sum_{0\le i<j<n}(x_i+x_j)(j-i), \qquad S_2 = \sum_{0\le i<j<n}(x_i\&x_j)(j-i).

For S_1, instead of summing over all pairs, we look at the contribution of each index separately. The element x_k appears in pairs as a right endpoint with i=0,\dots,k-1, and the total distance from those is

\sum_{i=0}^{k-1}(k-i) = 1+2+\dots+k = \frac{k(k+1)}{2}.

It also appears as a left endpoint (k,j) with j=k+1,\dots,n-1, giving

\sum_{j=k+1}^{n-1}(j-k) = 1+2+\dots+(n-k-1) = \frac{(n-k-1)(n-k)}{2}.

So the total distance weight for index is

W_k = \frac{k(k+1)}{2} + \frac{(n-k-1)(n-k)}{2},

and

S_1 = \sum_{k=0}^{n-1} x_k W_k = \sum_{k=0}^{n-1}(a+k)W_k.

This is just one loop over , doing everything modulo 10^9+7.

For S_2, we handle bits. We can write

x_i\&x_j = \sum_{b=0}^{30} \left( (1\ll b)\ \text{if bit }b\text{ is set in both }x_i,x_j \right),

so

S_2 = \sum_{b=0}^{30} (1\ll b)\cdot T_b,

where

T_b = \sum_{\substack{0\le i<j<n \\ \text{bit }b\text{ set in both }x_i,x_j}} (j-i).

Fix some bit . Consider only indices where this bit is set. Suppose their indices in increasing order are p_0,p_1,\dots,p_{m-1}. Then

T_b = \sum_{0\le u<v<m}(p_v-p_u).

We can compute this in one pass without explicitly storing pairs. When we process p_v, its contribution with all previous indices is

\sum_{u=0}^{v-1}(p_v-p_u) = v\cdot p_v - \sum_{u=0}^{v-1}p_u.

So we maintain for each bit :

  • \text{cnt}_b = count of indices seen with that bit set (this is ),

  • \text{sumIdx}_b = sum of those indices,

  • T_b = accumulated value for this bit.

When we are at index , let x = a + i. For each bit such that the -th bit of is , we update

T_j \mathrel{+}= \text{cnt}_j\cdot i - \text{sumIdx}_j, \qquad \text{cnt}_j \mathrel{+}= 1, \qquad \text{sumIdx}_j \mathrel{+}= i.

At the end, T_b is known for each bit, and

S_2 = \sum_{b=0}^{30} (T_b \bmod \text{MOD})\cdot((1\ll b)\bmod \text{MOD})\bmod \text{MOD},

with \text{MOD}=10^9+7. The final answer is

\mathsf{Ans} = (S_1 - S_2)\bmod \text{MOD}.

Complexity \mathcal{O}(n + n\cdot\log n)

MOD=10**9+7
for _ in range(int(input())):
    a,b=map(int, input().split())
    n=b-a+1
    s1=0
    for i in range(n):
        l=i*(i+1)//2
        r=(n-i-1)*(n-i)//2
        w=l+r
        s1=(s1+(a+i)%MOD*(w%MOD))%MOD

    c=[0]*31
    si=[0]*31
    t=[0]*31
    for i in range(n):
        x=a+i
        for j in range(31):
            if(x>>j&1):
                t[j]+=c[j]*i-si[j]
                c[j]+=1
                si[j]+=i

    s2=0
    for j in range(31):
        s2=(s2+(t[j]%MOD)*((1<<j)%MOD))%MOD
    print((s1-s2)%MOD)