首页 > ACM题库 > HDU-杭电 > HDU 4336-Card Collector[解题报告]HOJ
2015
05-23

HDU 4336-Card Collector[解题报告]HOJ

Card Collector

问题描述 :

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.

输入:

The first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, …, pN, (p1 + p2 + … + pN <= 1), indicating the possibility of each card to appear in a bag of snacks.

Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.

输出:

The first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, …, pN, (p1 + p2 + … + pN <= 1), indicating the possibility of each card to appear in a bag of snacks.

Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.

样例输入:

1
0.1
2
0.1 0.4

样例输出:

10.000
10.500

#include<cstdio>
const int maxn=1<<20;

double E[maxn],p[21],pr;
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		pr=1;
		for(int i=1;i<=n;i++)
		{
			scanf("%lf",&p[i]);
			pr-=p[i];
		}
		int state=(1<<n)-1;
		E[state]=0;
		for(int i=state-1;i>=0;i--)
		{
			double a=pr,b=1.0;
			for(int j=1;j<=n;j++)
			{
				int t=1<<(j-1);
				if((i&t)>0)a+=p[j];
				else b+=p[j]*E[i+t];
			}
			E[i]=b/(1-a);
		}
		printf("%lf\n",E[0]);
	}

	return 0;
}