Tanya, Balls, and <<Exclusive Or>>
Time limit1sMemory limit512 MB
Compute the sum of bitwise XOR over all unordered pairs of the integers 1 to n, modulo 10^9+7, for n up to 10^9.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Math, Combinatorics, Divide and conquer
- Solved
- No attempts yet
Problem
Tanya had balls, numbered from to . Unfortunately, Tanya dropped all the balls into a river and became very upset.
To comfort her, her older brother Seryozha suggested a fun mathematical pastime to Tanya: compute the sum of the pairwise exclusive or of her ball numbers.
The exclusive or of two numbers is denoted and corresponds to the <<xor>> operation in Pascal or <<\char 94>> in other languages. To compute for two integers, do the following: write each number in binary, and set the -th bit of the result to one if that bit is one in exactly one of and . For example, , .
Help Tanya! Compute the sum over all pairs of her balls of the exclusive or of their numbers. Tanya does not like large numbers, so the answer must be printed modulo .
For example, if Tanya had balls, the desired value is .
Input
The first line contains the number , the number of balls Tanya has ().
Output
Print the sum over all pairs of her balls of the exclusive or of their numbers, taken modulo .