Ultraviolet A 10417 gift exchanging

Source: Internet
Author: User

Uva_000017

If we set event AI to bring class I gift to 1st people, event B shows the number of classes in the question for all people. P (ai | B) indicates that in the current state, 1st people bring the probability of Class I gift. Then the remaining problem is to calculate the probability, then, the number divided by the class I gift is the probability that the class I gift is successful.

The formula based on the conditional probability is P (ai | B) = P (AIB)/P (B), P (AIB), indicating the probability of simultaneous occurrence of event AI and B. Because the number of substates in the original gift status is limited, we can specify the order in which the gifts are given (or the order in which the gifts are given) and then perform a rule to calculate the probability of each State.

So we may wish to stipulate that from the first person, each person will take his present and take it empty. If f [I] [J] is set, it indicates the probability that I can meet the situation where I am facing the gift status J. J is not difficult to indicate that it can be in the 13 hexadecimal format, the rest is how to transfer the state, there should be f [I] [J] = sum {G [I] [ai] * f [I-1] [J-ai]}, among them, G [I] [ai] brings the probability of the gift AI to the I-th individual. J-AI indicates the status of the remaining gift after the I-th person takes the AI, instead of directly performing subtraction.

Now we have the probability of reaching all States. How does P (AIB) represent it? It should be g [1] [ai] * f [2] [K], where G [1] [ai] brings the probability of gift AI to 1st people, K is the status of the remaining gift after the AI is taken by 1st people. How does P (B) represent it? It should be f [1] [K], where k is the original gift status.

# Include <stdio. h>
# Include < String . H>
# Define Maxn 13
# Define Maxb 6
# Define Maxd 350000
Int Vis [maxn] [maxd], a [maxb], B [maxb], n, m, gift [maxb];
Double F [maxn] [maxd], G [maxn] [maxb];
Double DFS ( Int Cur, Int St)
{
Int I, J, K;
Double Ans = 0 ;
If (Vis [cur] [st])
Return F [cur] [st];
K = sT;
For (I = 5 ; I> 0 ; I --)
{
A [I] = K % 13 ;
K/= 13 ;
}
For (I = 1 ; I <= 5 ; I ++)
If (A [I])
{
-- A [I];
For (J = 1 , K = 0 ; J <= 5 ; J ++)
K = K * 13 + A [J];
Ans + = DFS (cur + 1 , K) * g [cur] [I];
++ A [I];
}
Vis [cur] [st] = 1 ;
Return F [cur] [st] = ans;
}
Void Solve ()
{
Int I, J, K, box;
Double Max, ANS, Res;
Scanf ( " % D " , & N );
For (I = 1 ; I <= 5 ; I ++)
Scanf ( " % D " , & Gift [I]);
For (I = 1 ; I <= N; I ++)
For (J = 1 ; J <= 5 ; J ++)
Scanf ( " % Lf " , & G [I] [J]);
For (I = 1 , K = 0 ; I <= 5 ; I ++)
K = K * 13 + Gift [I];
Memset (VIS,0 , Sizeof (VIS ));
Vis [n + 1 ] [ 0 ] = 1 ;
F [n + 1 ] [ 0 ] = 1 ;
Res = DFS ( 1 , K );
Max =- 1 ;
For (I =1 ; I <= 5 ; I ++)
If (Gift [I])
{
-- Gift [I];
For (J = 1 , K = 0 ; J <= 5 ; J ++)
K = K * 13 + Gift [J];
++ Gift [I];
Ans = f [ 2 ] [K] * g [1 ] [I]/gift [I];
If (ANS/RES> MAX + 1E- 9 )
{
Max = ANS/RES;
Box = I;
}
}
Printf ( " % D %. 3lf \ n " , Box, max );
}
Int Main ()
{
Int T;
Scanf ( " % D " , & T );
While (T --)
{
Solve ();
}
Return 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.