SGU 495 Kids and prizes probability DP

Source: Internet
Author: User

Test instructions

There are n boxes with gifts, and m individuals take them in turn. If there is a gift in the box, take the gift. Whether or not to get a gift, the box is put back in the original. (so it's possible that the people behind them get the boxes from the people in front of them, and then they don't get the prizes). Ask, and finally can take away the number of gifts expected.

Two kinds of ideas, to kneel,,, or did not think out ....

M-Individuals are independent. The probability of not being selected for each gift is ((n-1)/n) ^m then the expectation of the number of gifts not selected is N ((n-1)/n) ^m so the answer is  n-n* ((n-1)/n) ^m;

This place is always thinking about begging for 1/n, the result didn't turn around ... Orz


Probability DP

So the question is: set dp[i] to show that I have been taken after the organizers to send out the expected number of gifts.

Then, for the first person, may or may not have received the gift, the transfer equation is:

Dp[i] = (n-dp[i-1])/n * (Dp[i-1] + 1) + (dp[i-1])/n * dp[i-1];

If you get the probability, you're going to have to send an extra probability of not getting it.


Description



ICPC (International Cardboard producing company) are in the business of producing Cardboard boxes. Recently the company organized a contest for kids for the best design of a cardboard box and selected MWinners. There is NPrizes for the winners, each one carefully packed in a cardboard box (made by the ICPC, of course). The awarding process would be as follows:
    • All the boxes with prizes would be stored in a separate.
    • The winners would enter the same, one at a time.
    • Each winner selects one of the boxes.
    • The selected box is opened by a representative of the organizing Committee.
    • If the box contains a prize, the winner takes it.
    • If the box is empty (because the same box have already been selected by one or more previous winners), the winner would inst EAD get a certificate printed on a sheet of excellent cardboard (made by ICPC, of course).
    • Whether there is a prize or not, the box was re-sealed and returned to the class.
The management of the company would such as know how many prizes would be given by the above process. It is assumed this each winner picks a box at the random and that all boxes was equally likely to be picked. Compute the mathematical expectation of the number of prizes given (the certificates is not counted as prizes, of course) .

Input

The first and only line of the input file contains the values of Nand M( ).

Output

The first and only line of the output file should contain a single real number:the expected number of prizes given out. The answer is accepted as correct if either the absolute or the relative error is less than or equal to 10-9.

Sample Input

Sample input
Sample output
5 7
3.951424

Sample input
Sample output
4 3
2.3125

#include <stdio.h> #include <iostream> #include <algorithm> #include <math.h>using namespace Std;int Main () {    int n,m;    while (scanf ("%d%d", &n,&m)!=eof)    {        printf ("%.9lf\n", N-n*1.0*pow (((1.0*n-1)/(n*1.0)), M));    }    return 0;}

Probability DP

#include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> #include < Cstring>using namespace Std;int Main () {    int n,m;    Double dp[100005];    while (scanf ("%d%d", &n,&m)!=eof)    {        memset (dp,0,sizeof (DP));        dp[0]=0;        Dp[1]=1;        for (int i=2;i<=m;i++)            dp[i]=dp[i-1]* (1-dp[i-1]) +dp[i-1]* (dp[i-1]-1.0/n);        Double s=0;        for (int i=1;i<=m;i++)            s+=dp[i];            printf ("%.9lf\n", s);    }    return 0;}

True plexiglass problem ... No, it's all tears.

SGU 495 Kids and prizes probability DP

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.