D - Chocolate Box (概率+dp)

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值