#P12177. Index Difference and XOR Sum

    ID: 14280 Type: Default 1000ms 256MiB

Index Difference and XOR Sum

Index Difference and XOR Sum

You are given a sequence of n integers, \(a_1, a_2, \ldots, a_n\). Your task is to compute the following sum:

\[ \sum_{i=1}^{n} \sum_{j=i+1}^{n} \Bigl( (a_i \oplus a_j) \times (j-i)\Bigr), \]

Here, \(\oplus\) denotes the bitwise XOR operation. In other words, for every pair \((i, j)\) with \(1 \le i < j \le n\), you need to calculate \((a_i \oplus a_j) \times (j-i)\) and then output the sum of all these values.

inputFormat

The first line contains an integer \(n\) representing the number of elements.

The second line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\).

outputFormat

Output a single integer which is the computed sum:

\[ \sum_{i=1}^{n} \sum_{j=i+1}^{n} \Bigl( (a_i \oplus a_j) \times (j-i)\Bigr) \]

sample

2
1 2
3