题目
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]);
}
}
听说还能容斥原理做