Present (Codeforces 1322B)
Tags: Math, bitmasks

Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one — xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute$$$$$$ (a_1 + a_2) \oplus (a_1 + a_3) \oplus \ldots \oplus (a_1 + a_n) \\ \oplus (a_2 + a_3) \oplus \ldots \oplus (a_2 + a_n) \\ \ldots \\ \oplus (a_{n-1} + a_n) \\ $$$$$$Here $$$x \oplus y$$$ is a bitwise XOR operation (i.e. $$$x$$$ ^ $$$y$$$ in many modern programming languages). You can read about it in Wikipedia: https://en.wikipedia.org/wiki/Exclusive_or#Bitwise_operation.

Input: The first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 400\,000$$$) — the number of integers in the array.The second line contains integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^7$$$).

Output: Print a single integer — xor of all pairwise sums of integers in the given array.

Note: In the first sample case there is only one sum $$$1 + 2 = 3$$$.In the second sample case there are three sums: $$$1 + 2 = 3$$$, $$$1 + 3 = 4$$$, $$$2 + 3 = 5$$$. In binary they are represented as $$$011_2 \oplus 100_2 \oplus 101_2 = 010_2$$$, thus the answer is 2.$$$\oplus$$$ is the bitwise xor operation. To define $$$x \oplus y$$$, consider binary representations of integers $$$x$$$ and $$$y$$$. We put the $$$i$$$-th bit of the result to be 1 when exactly one of the $$$i$$$-th bits of $$$x$$$ and $$$y$$$ is 1. Otherwise, the $$$i$$$-th bit of the result is put to be 0. For example, $$$0101_2 \, \oplus \, 0011_2 = 0110_2$$$.

Sample Inputs:
2
1 2

3
1 2 3

Sample Outputs:
3

2

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
pow2 = [1]
l = 24
for _ in range(l):
    pow2.append(2 * pow2[-1])
v = 0
ans = 0
for p in pow2:
    v ^= p
    ok = 0
    x = [0] * (v + 3)
    for i in a:
        j = v & i
        if j << 1 & p:
            ok ^= x[j]
        x[j] ^= 1
    for i in range(p << 1):
        x[i] ^= x[i - 1]
    for i in range(p):
        ok ^= (x[i] ^ x[i - 1]) & (x[i ^ v] ^ x[max((i ^ v) - p, i)])
    u = 3 * p
    for i in range(p + 1, p << 1):
        ok ^= (x[i] ^ x[i - 1]) & (x[v] ^ x[max(u - i - 1, i)])
    ans ^= ok * p
print(ans)

Solution (Python):
from sys import stdin, stdout
import math

def main():
    n = int(stdin.readline())
    arr = list(map(int, stdin.readline().split()))
    ans = 0
    for bit in range(25):
        mask = (1 << bit + 1) - 1
        bit0 = []
        bit1 = []
        for x in arr:
            if x & 1 << bit:
                bit1.append(x)
            else:
                bit0.append(x)
        arr = bit0 + bit1
        temp_arr = [mask & x for x in arr]
        above = 1 << bit + 1
        current = 1 << bit
        abc = above + current
        j1 = j2 = j3 = 0
        res = 0
        for (i, x) in enumerate(reversed(temp_arr)):
            while j1 < n and temp_arr[j1] + x < current:
                j1 += 1
            while j2 < n and temp_arr[j2] + x < above:
                j2 += 1
            while j3 < n and temp_arr[j3] + x < abc:
                j3 += 1
            r1 = n - i - j3 - 1
            if r1 < 0:
                r1 = 0
            r2 = min(n - i - 1, j2) - j1
            if r2 < 0:
                r2 = 0
            res += (r1 + r2) % 2
        if res % 2 == 1:
            ans += current
    stdout.write(str(ans))
main()

Solution (Python):
from sys import stdin, stdout

def main():
    n = int(stdin.readline())
    arr = list(map(int, stdin.readline().split()))
    ans = 0
    for bit in range(29):
        mask = (1 << bit + 1) - 1
        bit0 = []
        bit1 = []
        for x in arr:
            if x & 1 << bit:
                bit1.append(x)
            else:
                bit0.append(x)
        arr = bit0 + bit1
        temp_arr = [mask & x for x in arr]
        above = 1 << bit + 1
        current = 1 << bit
        abc = above + current
        j1 = j2 = j3 = 0
        res = 0
        for (i, x) in enumerate(reversed(temp_arr)):
            while j1 < n and temp_arr[j1] + x < current:
                j1 += 1
            while j2 < n and temp_arr[j2] + x < above:
                j2 += 1
            while j3 < n and temp_arr[j3] + x < abc:
                j3 += 1
            r1 = n - i - j3 - 1
            if r1 < 0:
                r1 = 0
            r2 = min(n - i - 1, j2) - j1
            if r2 < 0:
                r2 = 0
            res += (r1 + r2) % 2
        if res % 2 == 1:
            ans += current
    stdout.write(str(ans))
main()