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
dp;
n个糖果放入m个箱子中 问 存在空箱子的概率
状态转移方程: dp[i][j] = dp[i - 1][j] * (j * 1.0/ m) + dp[i - 1][j - 1] * (m - j + 1)*1.0/m;
i是糖果数量 j是箱子数量 分析可推出状态转移方程
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
double dp[110][110];
int main(){
int n, m;
int ca = 1;
while(cin >> n && n != -1){
cin >> m;
memset(dp, 0, sizeof(dp));
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;
}
}
double sum = 0;
for(int i = 1; i < m; i++){
sum += dp[n][i];
}
printf("Case %d: %.7lf\n",ca++, sum);
}
return 0;
}