题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片。求需要买多少包才能拿到所以的N张卡片,求次数的期望。

析:期望DP,是很容易看出来的,然后由于得到每张卡片的状态不知道,所以用状态压缩,dp[i] 表示这个状态时,要全部收齐卡片的期望。

由于有可能是什么也没有,所以我们要特殊判断一下。然后就和剩下的就简单了。

另一个方法就是状态压缩+容斥,同样每个状态表示收集的状态,由于每张卡都是独立,所以,每个卡片的期望就是1.0/p,然后要做的就是要去重,既然要去重,

那么就是得用容斥原理了。

代码如下:

期望DP+状态压缩

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #define lson l,m,rt<<1
  17. #define rson m+1,r,rt<<1|1
  18. //#include <tr1/unordered_map>
  19. #define freopenr freopen("in.txt", "r", stdin)
  20. #define freopenw freopen("out.txt", "w", stdout)
  21. using namespace std;
  22. //using namespace std :: tr1;
  23.  
  24. typedef long long LL;
  25. typedef pair<int, int> P;
  26. const int INF = 0x3f3f3f3f;
  27. const double inf = 0x3f3f3f3f3f3f;
  28. const LL LNF = 0x3f3f3f3f3f3f;
  29. const double PI = acos(-1.0);
  30. const double eps = 1e-8;
  31. const int maxn = (1<<20) + 5;
  32. const LL mod = 10000000000007;
  33. const int N = 1e6 + 5;
  34. const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
  35. const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
  36. const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  37. inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
  38. int n, m;
  39. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. inline int Min(int a, int b){ return a < b ? a : b; }
  42. inline int Max(int a, int b){ return a > b ? a : b; }
  43. inline LL Min(LL a, LL b){ return a < b ? a : b; }
  44. inline LL Max(LL a, LL b){ return a > b ? a : b; }
  45. inline bool is_in(int r, int c){
  46. return r >= 0 && r < n && c >= 0 && c < m;
  47. }
  48. double dp[maxn];
  49. double p[25];
  50.  
  51. int main(){
  52. while(scanf("%d", &n) == 1){
  53. double pp = 1.0;
  54. for(int i = 0; i < n; ++i){
  55. scanf("%lf", p+i);
  56. pp -= p[i];
  57. }
  58. dp[(1<<n)-1] = 0.0;
  59. for(int i = (1<<n)-2; i >= 0; --i){
  60. double have = 0.0, sum = 1.0;
  61. for(int j = 0; j < n; ++j)
  62. if(i&(1<<j)) have += p[j];
  63. else sum += p[j] * dp[i|(1<<j)];
  64. dp[i] = sum / (1.0 - pp - have);
  65. }
  66. printf("%.4f\n", dp[0]);
  67. }
  68. return 0;
  69. }

状态压缩+容斥

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #define lson l,m,rt<<1
  17. #define rson m+1,r,rt<<1|1
  18. //#include <tr1/unordered_map>
  19. #define freopenr freopen("in.txt", "r", stdin)
  20. #define freopenw freopen("out.txt", "w", stdout)
  21. using namespace std;
  22. //using namespace std :: tr1;
  23.  
  24. typedef long long LL;
  25. typedef pair<int, int> P;
  26. const int INF = 0x3f3f3f3f;
  27. const double inf = 0x3f3f3f3f3f3f;
  28. const LL LNF = 0x3f3f3f3f3f3f;
  29. const double PI = acos(-1.0);
  30. const double eps = 1e-8;
  31. const int maxn = (1<<20) + 5;
  32. const LL mod = 10000000000007;
  33. const int N = 1e6 + 5;
  34. const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
  35. const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
  36. const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  37. inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
  38. int n, m;
  39. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. inline int Min(int a, int b){ return a < b ? a : b; }
  42. inline int Max(int a, int b){ return a > b ? a : b; }
  43. inline LL Min(LL a, LL b){ return a < b ? a : b; }
  44. inline LL Max(LL a, LL b){ return a > b ? a : b; }
  45. inline bool is_in(int r, int c){
  46. return r >= 0 && r < n && c >= 0 && c < m;
  47. }
  48. double p[25];
  49.  
  50. int main(){
  51. while(scanf("%d", &n) == 1){
  52. for(int i = 0; i < n; ++i) scanf("%lf", p+i);
  53. double ans = 0.0;
  54. for(int i = 1; i < (1<<n); ++i){
  55. int cnt = 0;
  56. double sum = 0.0;
  57. for(int j = 0; j < n; ++j) if(i&(1<<j)){
  58. sum += p[j];
  59. ++cnt;
  60. }
  61. ans += (cnt & 1) ? 1.0/sum : -1.0/sum;
  62. }
  63. printf("%f\n", ans);
  64.  
  65. }
  66. return 0;
  67. }

HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. hdu 4336 Card Collector —— Min-Max 容斥

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...

  9. HDU 4336 Card Collector(容斥)

    题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...

随机推荐

  1. 查看已安装的CentOS版本信息:

    如何查看已安装的CentOS版本信息: 1)[root@localhost ~]# cat /proc/version Linux version 2.6.18-194.el5 (mockbuild@ ...

  2. transfromjs动画效果

    记得以前facebook做过一款HTML5游戏.开场动画是一块软体类似豆腐的东西一起摇摆.类似的效果如下面的gif所示: facebook当时使用的是createjs下的子项目easeljs和twee ...

  3. 关于NIO

    操作系统的IO控制 在整个IO控制方式的发展过程中,始终贯穿着这样一条宗旨:即尽量减少主机对IO控制的干预,把主机从繁杂的IO控制事务中解脱出来,以便更多地去完成数据处理任务.为了缓和高速CPU和IO ...

  4. c++的默认构造函数 VS 深拷贝(值拷贝) 与 浅拷贝(位拷贝)

    C++默认为类生成了四个缺省函数: A(void); // 缺省的无参数构造函数 A(const A &a); // 缺省的拷贝构造函数 ~A(void); // 缺省的析构函数 A & ...

  5. Matrix Calculator

    表达式分析+矩阵+计算器+寄存器=矩阵计算器 怎么想起来搞这个呢.. //刚看龙书兴致勃勃要搞表达式分析 这个寄存器比较简陋,26字母+4缓存,//字母不分大小写 当然,不只能算矩阵,还能算数= = ...

  6. Unity使用Windows弹窗保存图片

    此功能都在类EditorUtility中(using UnityEditor;) 包括 OpenFilePanel打开文件窗口Displays the "open file" di ...

  7. javascript全局变量和局部变量

    局部变量和全局变量可以同名.不过在函数体内部,局部变量的优先级高于全局变量.需要格外注意:专用于函数体内部的变量一定要用var关键字声明,否则该变量会变成全局变量.因为js是弱类型语言,所以它可以存放 ...

  8. KVO

    •基本概念 Key Value Observing, 键值观察者.它提供一种机制,当指定的对象的属性被修改后,则对象就会接收到通知. 与NSNotification不同,键值观察中并没有中心对象来为所 ...

  9. 一个div,包含三个小的div,平均分布的样式

    从11月份开始,自学前端开发,写静态页面中,经常用到一个大的div下包含三个小的div,平均分布div大小样式,写过多次,也多次忘记,每次都要现找资料,不想之后,在这么麻烦,索性今天自己记录一下,方便 ...

  10. 【随笔】Photoshop简单切图

    一.首先准备一张需要切割的图: 二.打开Photoshop,在ps里打开这张图片: 三.在界面的左边选择切片工具: 四.根据需要,鼠标点住上方和左边的标尺拖动,给图片加上分割辅助线: 五.用切片工具点 ...