Feed X through N stages that each apply bitwise AND, OR, or XOR with K at given probabilities and report the expected final value.
Medium5ProbabilityBit manipulationDynamic programmingNo attempts yetTime limit5sMemory limit512 MBA 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 K and always performs one of these three operations.
The machine picks the operation independently each time according to A, B and C, and that choice really is random.
You put N of these machines in series, so the output of one machine is the input of the next one. If you feed X into the first machine, what is the expected value of the output of the last machine?
The first line contains the number of test cases T. Each of the next T lines contains six integers N, X, K, A, B and C, 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.
For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y 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.
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 0.1+0.5 and the probability of getting 0 is 0.4, so the expected value is 5×0.6+0×0.4=3.
In the second test case of the example, the final output is 5 with probability 0.72 and 0 otherwise.