Recently one of my friend Tarik became a member of the food committee of an ACM regional competition.
He has been given m distinguishable boxes, he has to put n types of chocolates in the boxes.
The probability that one chocolate is placed in a certain box is 1/m. What is the probability that one
or more boxes are empty?
At first he thought it as an easy task. But soon he found that it was much harder. So, he falls into
a great trouble and asked you to help him in this task.
Input
Each line of the input contains two integers n indicating total number of distinguishable types of
chocolate and m indicating total number of distinguishable boxes (m ≤ n < 100). A single line
containing ‘-1’ denotes the end.
Output
For each of the cases you should calculate the probability corrected to seven decimal places. The output
format is shown below.
Sample Input
50 12
50 12
-1
Sample Output
Case 1: 0.1476651
Case 2: 0.1476651
Chocolate Box
此题是一个类型题目,n个球,m个盒子的排列组合。
题目要用到dp
dp[n][m] = dp[n-1][m-1] * 概率 + dp[n-1][m] * 概率
Chocolate Box
https://vjudge.net/contest/207906#status/16120581058/D/0/
12394452
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int n, m;
int Case = 0;
while(~scanf("%d", &n)&&n!=-1)
{
Case++;
scanf("%d", &m);
double dp[234][134] = {0};
dp[1][1] = 1;
for(int i=2;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
dp[i][j] = dp[i-1][j]*(j*1.0)/m + dp[i-1][j-1] * (m-j+1)*1.0/m;
}
}
printf("Case %d: %.7lf\n", Case, 1.0-dp[n][m]);
}
return 0;
}