SGU 495 Kids and Prizes probability dp, sguprizes
Question:
There are N boxes with gifts, and M people take them one by one. If the box has a gift, take it away. Whether you get the present or not, put the box back. (So it is possible that the people behind the prize will not receive the boxes that the people in front of them have taken ). Q: What is the expectation of the number of gifts that can be taken away.
Two ways of thinking: Kneel down, and still did not come up ....
M individuals are independent. If the probability of not being selected for each gift is (n-1)/n) ^ m, the expected number of unselected gifts is n * (n-1)/n) ^ m so the answer is n-n * (n-1)/n) ^ m;
In this place, I always want to find 1/n, and the result is not transferred... Orz
Probability dp
This question is: Set dp [I] to indicate the expected number of gifts sent by the organizer after I personally took the gift.
Then, for the I-th person, he may or may not receive a 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 need to get one more. If you don't get the probability, it's still the same.
Description
ICPC (International Cardboard Producing Company) is 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 are
NPrizes for the winners, each one carefully packed in a cardboard box (made by the ICPC, of course). The awarding process will be as follows:
- All the boxes with prizes will be stored in a separate room.
- The winners will enter the room, 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 has already been selected by one or more previous winners ), the winner will instead get a certificate printed on a sheet of excellent cardboard (made by ICPC, of course ).
- Whether there is a prize or not, the box is re-sealed and returned to the room.
The management of the company wowould like to know how many prizes will be given by the above process. it is assumed that each winner picks a box at random and that all boxes are equally likely to be picked. compute the mathematical expectation of the number of prizes given (the certificates are not counted as prizes, of course ).
Input
The first and only line of the input file contains the values
NAnd
M().
Output
The first and only line of the output file shoshould 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 Answer ...... If you don't talk about it, it's all tears.