Not So Random
Time limit5sMemory limit512 MB
Feed X through N stages that each apply bitwise AND, OR, or XOR with K at given probabilities and report the expected final value.
- Level
Medium5 of 10
- Topics
- Probability, Bit manipulation, Dynamic programming
- Solved
- No attempts yet
Problem
A random number generator takes one nonnegative integer as input and returns one nonnegative integer as output. This machine is not very random. It fixes a number and always performs one of these three operations.
- with probability , return the bitwise AND of the input and
- with probability , return the bitwise OR of the input and
- with probability , return the bitwise XOR of the input and
The machine picks the operation independently each time according to , and , and that choice really is random.
You put of these machines in series, so the output of one machine is the input of the next one. If you feed into the first machine, what is the expected value of the output of the last machine?
Input
The first line contains the number of test cases . Each of the next lines contains six integers , , , , and , separated by spaces. They are the number of machines, the value fed into the first machine, the fixed number every machine uses, and 100 times the probabilities of the bitwise AND, OR and XOR operations.
Limits
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the expected value of the output of the last machine.
Round the exact expected value at the eleventh digit after the decimal point and print exactly ten digits after the decimal point, padding with zeros. Print the decimal point and the ten zeros even when the value is an integer.
Explanation
In the first test case of the example, the final output is 5 when AND or OR happens and 0 when XOR happens. The probability of getting 5 is and the probability of getting 0 is , so the expected value is .
In the second test case of the example, the final output is 5 with probability and 0 otherwise.