N machines each apply AND, OR, or XOR with K at given probabilities, and the expected output after chaining them must be computed.
Medium5Bit manipulationProbabilityMathNo attempts yetTime limit10sMemory limit512 MBA "random number generator" (RNG) takes one nonnegative integer as input and returns one nonnegative integer as output. This machine is not very random. It uses a fixed number K, and every time it performs exactly one of these three operations:
The machine picks the operation according to A, B, and C, and that choice is truly random and independent every time.
You have N of these machines, wired in series so that the output of one machine is the input of the next. If you feed X to 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. In order, these are the number of machines, the initial input, the fixed number every machine uses for its bitwise operation, and 100 times the probabilities of choosing AND, OR, and XOR.
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, rounded to four decimal places. Always print exactly four digits after the decimal point, including trailing zeros.
In every test case the answer sits far from a rounding boundary, so double precision arithmetic gives the same output.
In the first test case of the sample, the final output is 5 if AND or OR happens and 0 if XOR happens. The probability of getting 5 is 0.1+0.5=0.6 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, the final output is 5 with probability 0.72 and 0 otherwise.