Zoj 3329 DP expectation

Source: Internet
Author: User
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:

    1. Set the counter to 0 at first.
    2. Roll the 3 dice simultaneously. If the up-facing NumberDie1IsA, The up-facing NumberDie2IsBAnd the up-facing NumberDie3IsC, Set the counter to 0. Otherwise, add the counter by the total value of the 3 up-facing numbers.
    3. If the counter's number is still not greaterN, 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 integerT(0 <T<= 300) indicating the number of test cases. ThenTTest Cases Follow. Each test case is a line contains 7 non-negative integersN,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

 
20 2 2 2 1 1 10 6 6 6 1 1

Sample output

1.1428571428571431.00000000002790698

Double P [510]; // coefficient of DP [0] In DP [I]
Double CON [510]; // constant of DP [I]
// DP [I] = P [I] * DP [0] + con [I];
// DP [I] indicates the number of times to throw a sieve for I.
// List n equations
/*
DP [N] = DP [0] * x + sigma (DP [n + k] * sum [k]);
DP [n-1] = DP [0] * x + sigma (DP [n + k] * sum [k]);
.....
DP [0] = DP [0] * x + sigma (DP [0 + k] * sum [k]);
Where k is the sum of the possible number of sieve, sum [k] is the probability of K

View code

# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using Namespace STD;
Double Sum [ 100 ];
Double P [ 510 ]; // Coefficient of DP [0] In DP [I]
Double CON [ 510 ]; // Constants of DP [I]
Int Main ()
{
Int T, n, K1, K2, K3, I, J, K, A, B, C;
Scanf ( " % D " , & T );
While (T --)
{
Scanf ( " % D " , & N, & K1, & K2, & K3, & A, & B, & C );
Double X = ( 1.0 /K1 * 1.0 /K2 * 1.0 /K3 );
Memset (p, 0 ,Sizeof (P ));
Memset (con, 0 , Sizeof (CON ));
Memset (sum, 0 , Sizeof (SUM ));
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) Continue ;
Sum [I + J + k] + = X;
}
}
}
For (I = N; I> = 0 ; I --)
{
For (J = I + 3 ; J <= k1 + k2 + K3 + I; j ++)
{
P [I] + = P [J] * sum [J-I];
CON [I] + = CON [J] * sum [J-I];
}
P [I] + = X;
CON [I] + = 1.0 ;
}
Printf ( " %. 15lf \ n " , CON [ 0 ]/( 1.0 -P [ 0 ]);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.