题目大意:n个盒子里装有礼物,m个人随机选择礼物,选完之后空盒子放回问选中的礼物数的期望。
解题思路1:m个人是独立的。对于每个礼物不被人选中的概率为((n-1)/n)^m,那么不被选中的礼物数的期望就是 n*((n-1)/n)^m。所以答案就是 n-n*((n-1)/n)^m;
思路2:每个人选中的概率有两种情况决定:上一个人选中或者没选中,直接进行dp。
495. Kids and Prizes
Time limit per test: 0.25 second(s)
Memory limit: 262144 kilobytes
Memory limit: 262144 kilobytes
input: standard
output: standard
output: standard
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 M winners. There are N prizes 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.
Input
The first and only line of the input file contains the values of
N
and
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
.
Example(s)
sample input | sample output |
5 7 | 3.951424 |
sample input | sample output |
4 3 | 2.3125 |
//第一种
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 100010;
using namespace std;
double p[maxn];
int main()
{
double n, m;
while(cin >>n>>m)
{
printf("%.10lf\n",n-(n*pow((n-1)/n, m)));
}
return 0;
}
//第二种
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 100010;
using namespace std;
double p[maxn];
int main()
{
int n, m;
while(cin >>n>>m)
{
p[1] = 1;
double P = 1.0/n;
for(int i = 2; i <= m; i++)
p[i] = (1-p[i-1])*p[i-1]+p[i-1]*(p[i-1]-P);
double ans = 0;
for(int i = 1; i <= m; i++)
ans += p[i];
printf("%.10lf\n",ans);
}
return 0;
}