Problem Description
Description
985 has n positive integers, and he wants to quickly know the return value of the following function.
| |
Note: ^ denotes the XOR operation.
Input
The first line contains an integer t, representing t test cases.
For each test case, the first line contains an integer representing the number of elements, and the next line contains n positive integers a[].
Note: 1 <= t <= 30, 1 <= n, a[] <= 100000.
Output
An integer representing the final return value ans.
Sample Input
| |
Sample Output
| |
Problem Analysis
Obviously, the function cannot be computed directly.
This type of problem involves binary operations.
There is a theorem:
x^y+x&y==x|y
Consider the effect of each bit of the binary representation on the result.
For example, apply the above operations to 1101, 1001, and 1010.
Then we can find that
the number of 1s in each bit from right to left is recorded as
cnt[0]~cnt[3] are respectively
2, 1, 1, 3
Because for the OR operation, except 0|0==0
all others are 1.
That is, 0|1+1|1
So for the rightmost bit, the number of effective 1s after the operation
is cnt[0](n-cnt[0])+(cnt[0](cnt[0]-1))/2
And the number represented by these 1s must be converted to decimal
so it must be multiplied by its corresponding 1«0
So the core code is
| |
where lx represents the number of bits in the binary representation of the largest number.
As for computing a[i]+a[j]
we only need to sum a[1~n] and then multiply by (n-1).
That is, each number is used n-1 times.
| |
Problem URL: ZZULI 1893 985’s Math Problem