One Person Game
--------------------------------------------------------------------------------
Time Limit: 1 Second Memory Limit: 32768 KB Special Judge
--------------------------------------------------------------------------------
There is a very simple and interesting one-person game. you have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. die2 has K2 faces. die3 has K3 faces. all the dice are fair dice, so the probability of rolling each value, 1 to K1, K2, K3 is exactly 1/K1, 1/K2 and 1/K3. You have a counter, and the game is played as follow:
Set the counter to 0 at first.
Roll the 3 dice simultaneously. if the up-facing number of Die1 is a, the up-facing number of Die2 is B and the up-facing number of Die3 is c, set the counter to 0. otherwise, add the counter by the total value of the 3 up-facing numbers.
If the counter's number is still not greater than n, go to step 2. Otherwise the game is ended.
Calculate the expectation of the number of times that you cast dice before the end of the game.
Input
There are multiple test cases. the first line of input is an integer T (0 <T <= 300) indicating the number of test cases. then T test cases follow. each test case is a line contains 7 non-negative integers n, K1, K2, K3, a, B, c (0 <= n <= 500, 1 <K1, K2, k3 <= 6, 1 <= a <= K1, 1 <= B <= K2, 1 <= c <= K3 ).
Output
For each test case, output the answer in a single line. A relative error of 1e-8 will be accepted.
Sample Input
2
0 2 2 2 1 1 1
0 6 6 6 1 1 1
Sample Output
1.142857142857143
1.004651162790698
We can introduce the formula dp [I] = sum {pk * dp [I + k]} + p0 * dp [0] + 1, dp [I] indicates that the current score is "I" is the expected end of the game, so dp [0] is the requirement. In fact, we can find a rule, the expectation is always pushed from the back to the front. For example, here, it is pushed from I + k to I, and the probability is the opposite, but we find that this formula is obtained, it is a ring, so deformation is required. Because every one is related to dp [0], we can set, dp [I] = a [I] * dp [0] + B [I], then dp [0] is not B [I]/(1-a [I]) now, we can use this sub-statement to get it. dp [I] = sum {pk * a [I + k]} + p0, B [I] = sum {pk * B [I + k]} + 1, so that the result can be obtained immediately!
# Include <iostream> # include <stdio. h> # include <string. h> using namespace std; # define MAXN 550 double pa [MAXN], pb [MAXN], dp [MAXN], p [MAXN]; int main () {int tcase, i, j, k, n, a, B, c, k1, k2, k3; double p0; scanf ("% d", & tcase); while (tcase --) {scanf ("% d", & n, & k1, & k2, & k3, & a, & B, & c); p0 = 1.0/k1/k2/k3; memset (pa, 0, sizeof (pa); memset (pb, 0, sizeof (pb )); memset (dp, 0, sizeof (dp); memset (p, 0, sizeof (p); fo R (I = 1; I <= k1; I ++) for (j = 1; j <= k2; j ++) for (k = 1; k <= k3; k ++) if (I! = A | j! = B | k! = C) // not all equal p [I + j + k] + = p0; int temp = k1 + k2 + k3; for (I = n; I> = 0; I --) {pa [I] = p0; pb [I] = 1; for (k = 1; k <= temp; k ++) {pa [I] + = pa [I + k] * p [k]; pb [I] + = pb [I + k] * p [k];} printf ("%. 15f \ n ", pb [0]/(1.0-pa [0]);} return 0 ;}# include <iostream >#include <stdio. h> # include <string. h> using namespace std; # define MAXN 550 double pa [MAXN], pb [MAXN], dp [MAXN], p [MAXN]; int main () {int tcase, i, j, k, n, a, B, c, k1, k2, k3; double p0; scanf (" % D ", & tcase); while (tcase --) {scanf (" % d ", & n, & k1, & k2, & k3, & a, & B, & c); p0 = 1.0/k1/k2/k3; memset (pa, 0, sizeof (pa )); memset (pb, 0, sizeof (pb); memset (dp, 0, sizeof (dp); memset (p, 0, sizeof (p )); for (I = 1; I <= k1; I ++) for (j = 1; j <= k2; j ++) for (k = 1; k <= k3; k ++) if (I! = A | j! = B | k! = C) // not all equal p [I + j + k] + = p0; int temp = k1 + k2 + k3; for (I = n; I> = 0; I --) {pa [I] = p0; pb [I] = 1; for (k = 1; k <= temp; k ++) {pa [I] + = pa [I + k] * p [k]; pb [I] + = pb [I + k] * p [k];} printf ("%. 15f \ n ", pb [0]/(1.0-pa [0]);} return 0 ;}