[HDU 4336] Card Collector (状态压缩概率dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336

题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡片。问你收集齐n种卡片,吃的期望零食数是多少?

状态压缩:f[mask],代表收集齐了mask,还需要吃的期望零食数。

打开包装,有3种情况,第一种:没有卡片,概率(1-sigma(p[i]))

第二种,在已知种类中:概率sigma(p[j])

第三种,在未知种类中:p[k]

因此 f[mask] = f[mask]*(1-sigma(p[i])) + f[mask] * sigma(p[j]) + sigma(f[mask|k]*p[k]) + 1

 1 ///#pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <bitset>
 9 #include <cmath>
10 #include <numeric>
11 #include <iterator>
12 #include <iostream>
13 #include <cstdlib>
14 #include <functional>
15 #include <queue>
16 #include <stack>
17 #include <string>
18 #include <cctype>
19 using namespace std;
20 #define PB push_back
21 #define MP make_pair
22 #define SZ size()
23 #define ST begin()
24 #define ED end()
25 #define CLR clear()
26 #define ZERO(x) memset((x),0,sizeof(x))
27 typedef long long LL;
28 typedef unsigned long long ULL;
29 typedef pair<int,int> PII;
30 const double EPS = 1e-8;
31
32 const int MAX_N = 20;
33 int N;
34 double p[MAX_N+1],f[1<<MAX_N];
35
36
37 int main(){
38
39     while( ~scanf("%d",&N) ) {
40         ZERO(f);
41         double sum = 0.0;
42         for(int i=1;i<=N;i++){
43             scanf("%lf",&p[i]);
44             sum += p[i];
45         }
46         f[(1<<N)-1] = 0;
47         for( int mask=(1<<N)-2;mask>=0;mask-- ){
48             double p1 = 0;
49             for(int i=0;i<N;i++){
50                 if( (mask>>i)&1 ) {
51                     p1 += p[i+1];
52                 }
53             }
54 //            printf("p1 = %f\n",p1);
55             double p2 = 0;
56             for(int i=0;i<N;i++){
57                 if( !((mask>>i)&1) ) {
58                     p2 += f[mask|(1<<i)]*p[i+1];
59 //                    printf("f[mask|(1<<i)]=%f\n",f[mask|(1<<i)]);
60                 }
61             }
62 //            printf("p2 = %f\n",p2);
63             f[mask] = (p2+1.0)/(sum-p1);
64         }
65         printf("%.10f\n",f[0]);
66     }
67
68     return 0;
69 }
时间: 2024-08-03 10:16:30

[HDU 4336] Card Collector (状态压缩概率dp)的相关文章

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

题目链接:点击打开链接 题意: 有n种卡片,每吃一包方便面都有一定概率获得其中一种卡片(也可能不获得卡片) 问集齐n张召唤神龙需要吃的方便面包数的期望. 思路: dp[i] 表示已经拥有卡片的状态为i, 还需要吃多少包才能拥有所有卡片, 显然 dp[(1<<n)-1] = 0; (已经拥有卡片就不用吃了嘛) 而答案就是dp[0]; 用样例二举例,下面dp方程内直接用二进制表示,为了方便观察,我们用最高位表示第一张卡片(P1=0.1),最低位表示第n张卡片(P2=0.4) dp[01]  = (

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

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

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|容斥原理)

详细的题目大意与解析大家参考一下kuangbin的文章. kuangbin链接 这边说一下自己对于kuangbin代码以及容斥原理位元素枚举的理解与解释,希望对大家有所帮助. 状态DP AC代码:状态压缩的思想我就不赘述了,我也只是略懂,这边仅仅分析一下状态方程 由于量比较多,我这边有的便用文字代替,有利于描述. dp[i]表示i状态达到满状态(即收集满n个物品,以下称满状态)所需要的期望. 那么i状态当中收集了x的物品,剩余n-x个物品没有收集 那么dp[i]=p*dp[i]+p2*dp[i2

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)

 题意:有n种卡片,吃零食的时候会吃到一些卡片,告诉你在一袋零食中吃到每种卡片的概率,求搜集齐每种卡片所需要买零食的袋数的期望. 思路:先状态压缩,然后概率DP 用d[i]表示由状态i到目标需要再买多少包,则状态转移方程为d[i] = p'*(d[i]+1) + sigma(d[ i | (1 << j) * p[i] ),然后相同项移到左边,最后就可以得到答案. #include<cstdio> #include<cstring> #include<cmat

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