hdu 4336 Card Collector(期望)

http://acm.hdu.edu.cn/showproblem.php?pid=4336

有N种卡片,每一袋零食里面最多有一张卡片,给出一袋零食里面每种卡片的概率,问平均要买多少袋零食能收集到所有的卡片。

状态压缩一下,共有1<<n-1个状态,设dp[sta]表示当前状态到目标状态平均买的零食数目,已知终态dp[1<<n-1] = 0,dp[sta]可由一下状态得到:

这一袋零食里没有卡片,概率为p(没有一张卡片的概率),状态转移到sta;

这一袋零食里面有卡片j,但是他已经拥有这种卡片,概率是a[j],状态转移到sta;

这一袋零食里面有卡片j,且是以前没有过的,概率是a[j],状态转移到sta | ( 1 << j )

逆推即可。

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
//#define LL __int64
#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 4010;

double dp[1100000];
double a[22];

int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		double t = 0;
		for(int i = 0; i < n; i++)
		{
			scanf("%lf",&a[i]);
			t += a[i];
		}
		t = 1-t;
		memset(dp,0,sizeof(dp));
		int m = (1 << n) - 1;
		dp[m] = 0;

		for(int sta = m-1; sta >= 0; sta--)
		{
			double s1 = 0,s2 = 0;
			for(int j = 0; j < n; j++)
			{
				if(!(sta & (1<<j)))
				{
					s1 += a[j] * dp[sta|(1<<j)];
				}
				else
					s2 += a[j];
			}
			s1 += 1;
			dp[sta] = s1/(1-s2-t);
		}
		printf("%.4lf\n",dp[0]);
	}
	return 0;
}

时间: 2024-10-22 22:20:07

hdu 4336 Card Collector(期望)的相关文章

hdu 4336 Card Collector(期望 dp 状态压缩)

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

HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由于得到每张卡片的状态不知道,所以用状态压缩,dp[i] 表示这个状态时,要全部收齐卡片的期望. 由于有可能是什么也没有,所以我们要特殊判断一下.然后就和剩下的就简单了. 另一个方法就是状态压缩+容斥,同样每个状态表示收集的状态,由于每张卡都是独立,所以,每个卡片的期望就是1.0/p,然后要做的就是要去重,既然

HDU 4336 Card Collector(动态规划-概率DP)

Card Collector 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

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

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——————【概率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): 3014    Accepted Submission(s): 1445 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

HDU 4336 Card Collector(容斥原理 or 状压求期望dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 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,

【期望DP】 HDU 4336 Card Collector

通道 题意:有n种卡片,吃零食的时候会吃到一些卡片,告诉你在一袋零食中吃到每种卡片的概率,求搜集齐每种卡片所需要买零食的袋数的期望 思路: 假设S状态中为1的数位表示还没有拿到的卡片,那么每次可能会拿到这其中的某一张卡片, 也可能拿到原来已经拿到的卡片, 还可能一张卡片也拿不到 后两种情况的状态不变.dp[0]=0;(表示每一种卡片都取完了,期望当然是0喽)dp[S]=sum*dp[S]+p[x1]dp[S^(1<<x1)]+p[x2]dp[S^(1<<x2)].....+1;su