C - Chocolate Box UVA - 10648

           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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值