Problem

There is a certain "random number generator" (RNG) which takes one nonnegative integer as input and generates another Nonn Egative integer as output. But you know the RNG was really not very random at all! It uses a fixed number **K**, and always performs one of the following three operations:

- With probability
**A**/100:return The bitwise AND of the input and **K**
- With probability
**B**/100:return The bitwise OR of the input and **K**
- With probability
**C**/100:return The bitwise XOR of the input and **K**

Assume the RNG is truly random in the the-the-the-same it chooses the operation each time and based on the V Alues of **A**, **B**, and **C**.)

You have an **N** copies of this RNG, and you have a arranged them in series such that output from one machine would be the Input for the next machine in the series. If you provide **X** as a input to the first machine, what would be being the expected value of the the final Mac Hine in the series?

Input

The first line of the input gives the number of test cases, **T**. **T** test Cases follow; Each consists of one line with six integers **N**, **X**, **K**, **A**, **B**, and **C**. Respectively, these denote the number of machines, the initial input, the fixed number with which all the bitwise operatio NS'll is performed (on every machine), and the times the probabilities of the bitwise and, or, and XOR operations.

Output

For each test case, output one line containing ' case #x: Y ', where x is the ' Test Case Number ' (starting from 1) and y-is th E expected value of the final output. Y'll be considered correct if it's within an absolute or relative error of 10-9 of the correct answer. See the FAQ for a explanation of what is means, and what formats of real numbers we accept.

Limits

1 ≤ **T** ≤50.

0≤ **A** ≤100.

0≤ **B** ≤100.

0≤ **C** ≤100.

**a+b+c** = 100.

Sample

input |
output |

31 5 5 402 5 5 (4010) |
< Pre class= "io-content" style= "font-family: ' Bogus font here ', monospace; border:0px; padding:0px; Overflow:auto; margin-top:0px; margin-bottom:0px; width:172px; Background-color:rgb (239,239,239) ">case #1:3.0000000000Case #2:3.6000000000Case #3: 15.6850579098 |

The main idea: for a "random number generator", given, the output may have three kinds of results, ①②③. The expected value of the results obtained by the repetition of the operation times.

idea: First understand what to ask for--the expectation, namely:. So to solve the problem, we need to know what are the possible values and calculate their probabilities. But the possible value is too much, the calculation probability is also very troublesome, so to find the breakthrough--bit operation. because the operations provided are bitwise operations: bitwise AND, bitwise OR, bitwise XOR, and each bit is independent of each other. So the first reaction should be to decompose the problem into a bit-wise expectation. Then the problem turns to:. So we just need to ask each bit to contribute the expectation and then add it to the result.

With this in mind, the next step is how to solve a bit's expectations. Without losing its generality, the following is an analysis of the position. In the entire operation, is invariant, to an input variable, you can quickly find the result is 0, the probability of 1. As follows:

With the State migration of the previous round of operations, we can write a recursive relationship of state:

This allows for a state transition matrix:

For a given input, the initial probability can be calculated according to bit 0 or 1, the rotation of the state transition matrix to the power, the use of fast power complexity. The AC code is as follows:

#include <iostream> #include <cstdio>using namespace std;void multiply (double m1[], double m2[]) {double ans[ ] = {0, 0, 0, 0};ans[0] = m1[0] * m2[0] + m1[1] * m2[2];ans[1] = m1[0] * m2[1] + m1[1] * m2[3];ans[2] = m1[2] * M2[0] + m1 [3] * m2[2];ans[3] = m1[2] * m2[1] + m1[3] * m2[3];for (int i = 0; i < 4; ++i) m1[i] = ans[i];} int main () {int TC, CA = 0;cin >> tc;while (tc--) {int n, x, k;double A, B, c;cin >> n >> x >> k > > a >> b >> c;a/=, b/=, c/= 100;/*ans0,ans1 save final status */double ans0[] = {1, 0, 0, 1}, ans1[] = {1, 0 , 0, 1};d ouble cur0[] = {1, a, 0, B + C}, cur1[] = {A, c, B + C, a + b};while (n) {if (n & 1) Multiply (ANS0, cur0), Multip Ly (ANS1, Cur1), multiply (cur0, cur0), multiply (cur1, cur1); n >>= 1;} /* Bitwise expectation */INT base = 1;double Res = 0;while (x | | k) {/*P0,P1 Indicates the initial state is 0,1 probability, C0,C1 represents the corresponding matrix coefficients */double p1 = x & 1? 1:0, P0 = 1-p1;double C0, c1;if (K & 1) C0 = ans1[2], C1 = ANS1[3];ELSEC0 = Ans0[2], C1 = ans0[3];res + = (C0 * p0 + C1 * p1) * BASE; The desired base of the current bit is <<= 1, x >>= 1, K >>= 1;} printf ("Case #%d:%.10lf\n", ++ca, res);}}

Summary: First of all to understand test instructions, will solve the original problem to think clearly, and then from the perspective of the algorithm to consider the problem. For a finite state transition, you can use a state transition matrix to represent it, and then convert the repeated operations to matrix multiplication, and you can use the fast power to optimize.

Google APAC Test not so Random Matrix Express power (Logn complexity)