【HDU】Card Collector

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

题意:n张卡片,每一次取一个盒子,盒子里装有卡片i的概率是p[i],求得到所有卡片所需要开的盒子的期望数(n<=20)

#include <cstdio>
#include <cstring>
using namespace std;

const int N=22;
int n;
double p[N], f[1<<N];
int main() {
	while(~scanf("%d", &n)) {
		int all=(1<<n)-1;
		for(int i=0; i<n; ++i) scanf("%lf", &p[i]);
		f[all]=0;
		for(int s=all-1; s>=0; --s) {
			double up=1, down=0;
			for(int i=0; i<n; ++i) {
				if(s&(1<<i)) continue;
				up+=f[s|(1<<i)]*p[i];
				down+=p[i];
			}
			f[s]=up/down;
		}
		printf("%f\n", f[0]);
	}
	return 0;
}

  

设$f[s]$表示当前得到状态为$s$的卡片还需要开的盒子的期望数:

考虑开一个箱子:

1、没有卡片,概率为:$1-\sum_{i} p[i]$;期望和为:$(1-\sum_{i} p[i])f[s]$

2、卡片$i$已经收集过了,概率为:$p[i]$;期望和为:$\sum_{i \in s} p[i]f[s]$

3、卡片$i$没有收集过,概率为:$p[i]$;期望和为:$\sum_{i \notin s} p[i]f[s \cup \{ i \}]$

所以

$$
\begin{align}
f[s]
& = (1-\sum_{i} p[i])f[s] + \sum_{i \in s} p[i]f[s] + \sum_{i \notin s} p[i]f[s \cup \{ i \}] \\
& = \frac{1 + \sum_{i \notin s} p[i]f[s \cup \{ i \}]}{\sum_{i \notin s} p[i]}
\end{align}
$$

然后还有注意,spj的话精度一定要注意啊,不要只输出了几位= =,最好多输出几位,你懂的...于是就wa了几发..

时间: 2024-08-05 19:27:43

【HDU】Card Collector的相关文章

【HDOJ】【4336】Card Collector

概率DP/数学期望/状压DP/容斥原理 kuangbin总结中的第14题 好神奇的做法……题解看kuangbin的代码好了…… 1 //HDOJ 4336 2 #include<cstdio> 3 #define rep(i,n) for(int i=0;i<n;++i) 4 #define F(i,j,n) for(int i=j;i<=n;++i) 5 #define D(i,j,n) for(int i=j;i>=n;--i) 6 const int N=22; 7 8

【HDU】2147 kiki&#39;s game

http://acm.hdu.edu.cn/showproblem.php?pid=2147 题意:n×m的棋盘,每次可以向左走.向下走.向左下走,初始在(1, m),n,m<=2000,问先手是否胜利. #include <cstdio> using namespace std; int main() { int n, m; while(scanf("%d%d", &n, &m), n|m) (n&1)&&(m&1)?

【HDU】4923 Room and Moor(2014多校第六场1003)

Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 263    Accepted Submission(s): 73 Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0

【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】

传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因是不愿意写暴力推断的).. 首先是简单的行列建边.源点向行建边.容量为该行元素和,汇点和列建边.容量为该列元素和.全部的行向全部的列建边,容量为K. 跑一次最大流.满流则有解,否则无解. 接下来是推断解是否唯一. 这个题解压根没看懂.还是暴力大法好. 最简单的思想就是枚举在一个矩形的四个端点.设A.D为主对角

【HDU】4918 Query on the subtree 点分治+树状数组

传送门:[HDU]4918 Query on the subtree 题目分析: 首先,简化问题. 1.求一次到点u的距离不超过d的点的个数.很容易,一次O(NlogN)的点分治便可以完成. 2.多次进行操作1.此时不能每次都O(NlogN)了,太慢了.我们考虑到对于点分治,树的重心一共有logN层,第一层为整棵树的重心,第二层为第一层重心的子树的重心,以此类推,每次至少分成两个大小差不多的子树,所以一共有logN层.而且,对于一个点,他最多只属于logN个子树,也就是最多只属于logN个重心.

【HDU】1754 I hate it ——线段树 单点更新 区间最值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 37448    Accepted Submission(s): 14816 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要

【HDU】 1160 FatMouse&#39;s Speed (DP)

一开始写的dfs进行记忆化结果不知道怎么进行路径的记录...改成循环就好了 dp[i] = max(dp[j]) + 1 , weight[j] < weight[j] && speed[j] > speed[i] 一开始进行一次排序使得重量递增,这样只需要考虑速度就好了 #include<cstdio> #include<algorithm> using namespace std; const int maxn = 10005; struct Mou

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】Mayor&#39;s posters(线段树区间问题)

需要离散化处理,线段树的区间修改问题. 需要注意的就是离散化的时候,由于给的数字是一段单位长度,所以需要特殊处理(因为线段的覆盖和点的覆盖是不一样的) 比如:(1,10)(1,4) (6,10) 离散化之后是 1 , 4 , 6 , 10 分别离散为 1 2 3 4 覆盖的时候先覆盖1 4 之后覆盖了1 2 之后覆盖了 2 3,结果为2 但是实际上应该是3 13450359 201301052100 2528 Accepted 1140K 985MS C++ 1960B 2014-09-17 1