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

  题意:买食品收集n个卡片,每个卡片的概率分别是pi,且Σp[i]<=1,求收集n个卡片需要买的食品数的期望。

  压缩DP:把每个食品用二进制表示,0和1分别表示没有卡片和已经收集到此卡片的期望,则

     f[s]=(1-Σp[i])*f[s]+Σp[j]*f[s]+Σp[k]*f[s|(1<<k)]

      s表示状态,i表示所有卡片编号,j表示s状态中已经有的卡片编号,k表示s状态中没有的卡片编号

  ->  Σp[i]*f[s]=Σp[i]*f[s|(1<<i)]

  或者容斥原理做:

  压缩DP:

 //STATUS:C++_AC_281MS_7128KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=(<<)+;
const int INF=0x3f3f3f3f;
const int MOD= ,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End double p[],f[N];
int n; int main(){
// freopen("in.txt","r",stdin);
int i,j,up;
double s;
while(~scanf("%d",&n))
{
for(i=;i<n;i++){
scanf("%lf",&p[i]);
}
up=(<<n)-;
f[up]=;
for(i=up-;i>=;i--){
f[i]=;s=;
for(j=;j<n;j++){
if(i&(<<j))continue;
f[i]+=p[j]*f[i|(<<j)];
s+=p[j];
}
f[i]/=s;
} printf("%lf\n",f[]);
}
return ;
}

  容斥原理:

 //STATUS:C++_AC_203MS_244KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=(<<)+;
const int INF=0x3f3f3f3f;
const int MOD= ,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End double p[];
int n; int main(){
// freopen("in.txt","r",stdin);
int i,j,up,cnt;
double ans,s;
while(~scanf("%d",&n))
{
for(i=;i<n;i++){
scanf("%lf",&p[i]);
}
up=(<<n)-;ans=;
for(i=;i<=up;i++){
s=;
for(j=cnt=;j<n;j++){
if(i&(<<j)){
cnt++;
s+=p[j];
}
}
if(cnt&)ans+=/s;
else ans-=/s;
} printf("%lf\n",ans);
}
return ;
}

HDU-4336 Card Collector 概率DP的更多相关文章

  1. $HDU$ 4336 $Card\ Collector$ 概率$dp$/$Min-Max$容斥

    正解:期望 解题报告: 传送门! 先放下题意,,,已知有总共有$n$张卡片,每次有$p_i$的概率抽到第$i$张卡,求买所有卡的期望次数 $umm$看到期望自然而然想$dp$? 再一看,哇,$n\le ...

  2. HDU 4336 Card Collector 期望dp+状压

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...

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

    Problem Description In your childhood, people in the famous novel Water Margin, you will win an amaz ...

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

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

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

    Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...

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

    Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

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

    题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

  9. HDU 4336 Card Collector(状压 + 概率DP 期望)题解

    题意:每包干脆面可能开出卡或者什么都没有,一共n种卡,每种卡每包爆率pi,问收齐n种卡的期望 思路:期望求解公式为:$E(x) = \sum_{i=1}^{k}pi * xi + (1 - \sum_ ...

  10. HDU 4336 Card Collector:期望dp + 状压

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 一共有n种卡片.每买一袋零食,有可能赠送一张卡片,也可能没有. 每一种卡片赠送的概率为p ...

随机推荐

  1. ie6 iframe src="javascript:" 报安全警报问题

    <iframe id="shuaka_iframe" class="embed-page-iframe" data-src="https://w ...

  2. grep正则表达式后面的单引号和双引号的区别

    单引号''是全引用,被单引号括起的内容不管是常量还是变量者不会发生替换:双引号""是部分引用,被双引号括起的内容常量还是常量,变量则会发生替换,替换成变量内容! 一般常量用单引号' ...

  3. static函数与普通函数

    转自http://blog.163.com/sunshine_linting/blog/static/44893323201191294825184/ 全局变量(外部变量)的说明之前再冠以static ...

  4. 找出程序cpu使用率高的原因

    确定是CPU过高 使用top观察是否存在CPU使用率过高现象 找出线程 对CPU使用率过高的进程的所有线程进行排序 ps H -e -o pid,tid,pcpu,cmd --sort=pcpu |g ...

  5. ECSHOP报错误Deprecated: preg_replace(): The /e modifier is depr

    http://www.ecshoptemplate.com/article-1850.html

  6. c缺陷与陷阱笔记-第六章 预处理器

    1.这一章貌似有个小错误,开始时定义 #define f (x) ((x)-1),然后f(x)代表什么,书上说是(x) ((x)-1),应该是 (x) ((x)-1)(x) 2.关于宏定义中参数的2次 ...

  7. 全选与反选(dom与jquery比较)

    <html> <head> <title>全选或反选(dom)</title> <meta http-equiv="Content-Ty ...

  8. Android:自定义适配器

    无论是ArrayAdapter还是SimpleAdapter都继承了BaseAdapter,自定义适配器同样继承BaseAdapter 实例:Gallery实现图片浏览器 <?xml versi ...

  9. NFC(11)MifareUltralight格式规范及读写示例

    注意 MifareUltralight 不支三种过滤方式之一,只支持第四种(用代码,activity singleTop ) 见  NFC(4)响应NFC设备时启动activity的四重过滤机制 Mi ...

  10. git源码中的Makefile

    https://github.com/chucklu/GitStudy   这链接里面的第一次提交 [chucklu@localhost GitStudy]$ cat Makefile CFLAGS= ...