Expectation
Time limit2sMemory limit64 MB
Compute the exact expected value (as a reduced fraction) of the XOR of two independent uniform random integers in [0, n-1), for up to 1000 values of n up to 1e9.
- Level
Medium6 of 10
- Topics
- Bit manipulation, Math, Combinatorics
- Solved
- No attempts yet
Problem
Eric built a simple scheme for generating random integers. Given an integer , it outputs a uniformly random integer between and inclusive. For example, when it returns , , or , each with probability .
Eric now wants a more elaborate scheme. He takes two independent copies of this generator and feeds their outputs into a bitwise XOR gate, which returns the bitwise exclusive or of its two inputs. His friend Nick is curious about the expectation of the result, and they would like you to compute it.
Recall that the expectation of a random variable is its average value. For a variable taking non-negative integer values it is
where is the probability that equals .
The exact expectation is always a rational number, so you must report it exactly rather than as a rounded decimal.
Input
The first line contains the number of test cases (). Each of the next lines contains a single integer ().
Output
For each test case, output the exact expected value of the XOR of the two generators' outputs as an irreducible fraction , where and . If the value is an integer, still write it with denominator (for example, ). Print the answer for each test case on its own line.