hdu4336-----Card Collector

Card Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2785    Accepted Submission(s): 1321

Special Judge

Problem Description

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.

Input

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.

Output

Output one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.

You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.

Sample Input

1
0.1
2
0.1 0.4

Sample Output

10.000
10.500

Source

2012 Multi-University Training Contest 4

Recommend

zhoujiaqi2010   |   We have carefully selected several similar problems for you:  4337 4331 4332 4333 4334

简单的概率dp,发现n最多只有20,所以状态压缩来搞定

dp[i] 表示 买到卡片的状态为i时,买齐卡片所需要的期望值

ans = dp[0]

dp[(1 << n) - 1] = 0;

转移方程里分为2块,第一块是此状态里有的卡片, 第二块里是状态里没有的和没买到卡片的

/*************************************************************************
    > File Name: hdu4336.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2014年12月23日 星期二 14时48分32秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = (1 << 20) + 10;
int n;
double dp[N];
double p[30], vp;

double dfs(int s)
{
	if (dp[s] != -1)
	{
		return dp[s];
	}
	double x = 0, y = 0;
	for (int i = 0; i < n; ++i)
	{
		if (!(s & (1 << i))) //这种状态下i没有抽到过
		{
			x += p[i] * (dfs(s | (1 << i)) + 1);
		}
		else
		{
			y += p[i];
		}
	}
	y += vp;
	return 	dp[s] = (x + y) / (1 - y);
}

int main()
{
	while (~scanf("%d", &n))
	{
		vp = 0;
		for (int i = 0; i < n; ++i)
		{
			scanf("%lf", &p[i]);
			vp -= p[i];
		}
		vp += 1;
		for (int i = 0; i <= (1 << n); ++i)
		{
			dp[i] = -1.0;
		}
		dp[(1 << n) - 1] = 0;
		printf("%f\n", dfs(0));
	}
	return 0;
}
时间: 2024-08-01 00:44:11

hdu4336-----Card Collector的相关文章

HDU-4336 Card Collector(状压概率DP||容斥原理)

Card Collector http://acm.hdu.edu.cn/showproblem.php?pid=4336 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful cards in

HDU4336 Card Collector【容斥原理】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意: 每包零食里有一张卡牌,总共有N种不同的卡牌,得到这N种卡牌的概率分别为P[i](1 <= i <= N). 求收集到所有卡牌的期望是多少. 思路: Pi表示得到第i张卡牌的概率,Ei表示得到第i张卡的期望. 假设现在有两张卡牌,由题意可知: E1 = 1/P1,E2 = 1/P2,E12(表示肯定买到1或2其中一包的期望) = 1/(P1+P2). 当我们计算E1和E2的时候,

HDU4336 Card Collector 概率DP求期望+状压

题目大意:要集齐N张卡片,每包干脆面出现每种卡片的概率已知,问你集齐N张卡片所需要的方便面包数的数学期望(N<=20). solution: 由于N<=20,我们可以考虑状压,设dp[S]表示牌的状态为S时的需要的方便面包数的数学期望. 那么,对于每一个状态,考虑枚举每一张牌i(摸到了i),此时: ① 如果S中不含i,dp[S]+=(dp[S|(1<<i-1)]+1)*p[i]. ② 如果S中已经包含i,那么算到下面的情况中去. 但是注意到,上述情况是已经保证了摸到牌,但是其实可以

hdu 4336 Card Collector

Card Collector http://acm.hdu.edu.cn/showproblem.php?pid=4336 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful cards in

HDU 4336:Card Collector 期望+状压

Card Collector 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 去商店里买零食,每包零食里最多有1张卡片,也有可能没有,问要集齐所有n(n≤20)种卡片所需要购买零食个数的期望. 题解: 设dp[i](二进制,对应位为1表示已经有该卡片)为以当前状态为起点还需要购买零食个数的期望,则dp[0]即答案 对DP求解期望有问题的可以看下这里 代码 #include<stdio.h>#include<string.h

HDU 4336——Card Collector——————【概率dp】

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3407    Accepted Submission(s): 1665Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful

hdu 4336 Card Collector (概率dp+位运算 求期望)

题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2711    Accepted Submission(s): 1277Special Judge Problem Description In your childhood, do you crazy for collecting the beaut

HDU 4336 Card Collector(概率dp+状压)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3014    Accepted Submission(s): 1445 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

Card Collector(HDU 4336)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3001    Accepted Submission(s): 1435Special Judge Problem Description In your childhood, do you crazy for collecting the beautiful

HDU 4336:Card Collector(容斥原理)

http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Description 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