HDU 4586 Play the Dice (probability + expectation)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4586


Problem Descriptionthere is a dice with n sides, which was numbered from,..., N and has the equal possibility to show Up when one rolls a dice. Each side have an integer AI on it. Now here's a game that's can roll this dice once, if the i-th side are up and you'll get Ai yuan. What's more, some SIDs of this dice is colored with a special different color. If you turn this side up, you'll get once more chance to roll the dice. When the dice is the second time, you still has the opportunity to win money and rolling chance. Now, need to calculate the expectations of money, we get after playing the game once.
Inputinput consists of multiple cases. Each case includes the lines.
The first line is a integer n (2<=n<=200), following with n integers ai (0<=ai<200)
The second line is a integer m (0<=m<=n), following with M integers bi (1<=bi<=n), and which is the numbers of th e Special sides to get another more chance.
Outputjust a real number which is the expectations of the one can get, rounded to exact, and digits. If you can get the unlimited money, print INF.

Sample Input
6 1 2 3 4 5 604 0 0 0 01 3

Sample Output
3.500.00

SOURCE2013 ACM-ICPC Nanjing Division National Invitational--re-title


Reproduced:

Test instructions: A dice have n-sided, each side has a different number a[i], and give a m, give M face, these faces if the dice to, you can continue to dice once, ask you sic once get the number of expectations is how much?
problem-solving ideas: I started to push the formula, but due to the wrong way of thinking, calculate a bit of trouble, in fact, the transformation of a train of thought can be. We want to calculate the probability of dice, I began to think is Sum1 is (n-m) non-special surface and, sum2 is a special surface and, then the probability of a dice issum1*[(N-M)/n], the probability of two dice is (sum2* (m/n) +sum1* (m/n) *[(n-m)/n], the third time is sum2* (m/n) * (m/n) +sum1* (m/n) * (m/n) *[(n-m)/n] So recursion can find out the final solution.  The solution that can be obtained is (SUM1+SUM2) + (sum1+sum2) * (m/n) + (sum1+sum2) * (m/n) ^2+....+ ... Remember sum1+sum2=sum this is infinite geometric series, you can use the formula to get p=sum*[(n)/(N-M)], then this is the total expectation, but also divided by the number of polygons N, the answer is sum/(n-m).
In fact, we can also change a line of thought, because it is infinite, we can be seen asonly for the first time, the average value of the coins obtained is the sum of the gold values for the faces of all the dice sum/n.sum.
For good luck, shake it up and you can do it again, the wheel will get m/n* (sum/n)
Good luck, again, the wheel can get (m/n) ^2* (sum/n)
Endless shake down, altogether can get sum/n* (1+q + q^2+ "" "+q^k +") is actually a geometric series, where the male ratio is q = m/n
By simplifying the formula, you can get E = sum/(n-m) or E = a1/(1-q), where a1 = sum/n. So when sum = 0 o'clock is 0,n = m, it is INF. The rest is sum/(n-m).
However, this topic requires a special sentence, if sum = 0 First output 0.00, then n = = m, output INF. Because the topic will be card you sum==0 and n==m this group of answers.

The code is as follows:

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include < Iostream>using namespace Std;int Main () {    int n, m;    int a[247], b[247];    while (~SCANF ("%d", &n))    {        int sum = 0;        for (int i = 0; i < n; i++)        {            scanf ("%d", &a[i]);            Sum+=a[i];        }        scanf ("%d", &m);        for (int i = 0; i < m; i++)        {            scanf ("%d", &b[i]);        }        Double A1, Q;        a1 = sum*1.0/n;        Q = m*1.0/n;        if (a1 = = 0)        {            printf ("0.00\n");        }        else if (q = = 1)        {            printf ("inf\n");        }        else        {            printf ("%.2lf\n", a1/(1-q));        }    }    return 0;}


HDU 4586 Play the Dice (probability + expectation)

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.