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;
}





  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值