hdu4336 Card Collector

本文探讨了一个经典的概率问题:通过购买零食来收集特定卡片,求集齐所有卡片所需的零食包数的期望值。采用状态压缩的方法进行求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin, you will win an amazing award. 

As a smart boy, you notice that to win the award, you must buy much more snacks than it seems to be. To convince your friends not to waste money any more, you should find the expected number of snacks one should buy to collect a full suit of cards.

题目大意

给出每张卡片出现在零食袋中的概率,每袋零食中最多只有一张卡片,可以没有,求集齐所有卡片的买零食包数的期望值
卡片种类数n<=20

题解

由于n<=20,状压
我们设d(i)为已收集到状态为i,集齐所有卡片还需买零食包数的期望值
初状态d((1<<n)1)=0
目标状态d(0)
对于每个状态i,我们枚举每一种卡片j
记录a,b;
如果该卡片还未获得,则a+=p[j],b+=p[j]d[i|(1<<j)]
所以,如果抽到了未获得的卡片,再抽a包就能集齐,由于要加上买的这一包,a++
所以d[i]=a/b

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
double d[3000000];
double p[30];
int main()
{
    int n;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++)
            scanf("%lf",&p[i]);
        int aim=(1<<n)-1;
        d[aim]=0;
        for(int i=aim-1;i>=0;i--){
            double dont_have_w=1,dont_have_p=0;
            for(int j=0;j<n;j++){
                if(i&(1<<j));
                else{
                    dont_have_p+=p[j];
                    dont_have_w+=p[j]*d[i|(1<<j)];
                }
            }
            d[i]=dont_have_w/dont_have_p;
        }
        printf("%.4lf\n",d[0]);
    }
}

听说还能容斥原理做

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值