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

题意:

  有n种卡片(n <= 20)。

  对于每一包方便面,里面有卡片i的概率为p[i],可以没有卡片。

  问你集齐n种卡片所买方便面数量的期望。

题解:

  状态压缩。

  第i位表示手上有没有卡片i。

  表示状态:

    dp[state] = expectation

    (卡片状态为state时,要集齐卡片还要买的方便面数的期望)

  找出答案:

    ans = dp[0]

    刚开始一张卡片都没有。

  如何转移:

    now: dp[state]

    对于卡片i,如果手上已经有了i,则方便面里有i等价于面里什么都没有。

    所以子期望共两种:

      (1)拿到一张还没有的卡片i。

      (2)拿到垃圾2333。

    dp[state] = sigma( dp[state|(1<<i)] * p[i] ) + dp[state] * P(useless) + 1

    P(useless)为拿到垃圾的概率。

    设tot = sigma(p[i])

    P(useless) = 1 - tot

    原式移项后:

      dp[state] = ( sigma( dp[state|(1<<i)] * p[i] ) + 1 ) / tot

  边界条件:

    dp[(1<<n)-1] = 0

    已经集齐,不用再买。

AC Code:

 // state expression:
// dp[state] = expectation
// state: state of present cards
//
// find the answer:
// ans = dp[0]
//
// transferring:
// now: dp[state]
// dp[state] = sigma( dp[state|(1<<i)] * p[i] ) + dp[state] * P(useless) + 1
// i: not been collected
// dp[state] = ( sigma( dp[state|(1<<i)] * p[i] ) + 1 ) / (1 - P(useless))
// dp[state] = ( sigma( dp[state|(1<<i)] * p[i] ) + 1 ) / tot
//
// boundary:
// dp[(1<<n)-1] = 0
#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 25
#define MAX_S ((1<<20)+5) using namespace std; int n;
double p[MAX_N];
double dp[MAX_S]; void read()
{
for(int i=;i<n;i++)
{
cin>>p[i];
}
} void solve()
{
memset(dp,,sizeof(dp));
for(int state=(<<n)-;state>=;state--)
{
double tot=;
for(int i=;i<n;i++)
{
if(!((state>>i)&))
{
dp[state]+=dp[state|(<<i)]*p[i];
tot+=p[i];
}
}
dp[state]=(dp[state]+1.0)/tot;
}
} void print()
{
printf("%.9f\n",dp[]);
} int main()
{
while(cin>>n)
{
read();
solve();
print();
}
}

最新文章

  1. ajax——CORS跨域调用REST API 的常见问题以及前后端的设置
  2. 动态加载JS 和 CSS
  3. Linux下gcc与gdb简介
  4. Leap Motion 开发笔记
  5. Metasploit介绍
  6. Thinkjs学习2—数据库的配置
  7. C#之DataTable转List与List转Datatable
  8. springboot~Compiler时开启插件的注解功能
  9. Linux内核中的printf实现
  10. [转帖]web安全:QQ号快速登录漏洞及被盗原理
  11. 错误 1 “Entities.PlanPrjEntity.PlanPrjs”不可访问,因为它受保护级别限制
  12. 《objective-c基础教程》学习笔记(四)—— OC面向对象编程初探
  13. day 59 Bootstrap学习
  14. 帝国cms栏目别名怎样调用?栏目名称太短了
  15. Node.js中Process.nextTick()和setImmediate()的区别
  16. 谈谈django里的Contex和RequestContext---向模板里添加全局变量
  17. iOS7中的多任务I
  18. VisualStudio2017 远程 调试 IIS 服务器 web网站
  19. 【转载】python-协程
  20. elastic search6.2.2 实现用户搜索记录查询(去重、排序)

热门文章

  1. POJ 题目3264 Balanced Lineup(RMQ)
  2. 【Python】matplotlib绘制折线图
  3. jenkins构建一个python项目
  4. js向后台传递对象
  5. Flume-1-7-0用户手册
  6. Canvas中图片翻转的应用
  7. 【C/C++】高亮C++中函数的重写——函数名相同?参数列表相同?返回值相同?
  8. erlang的timer定时器浅析
  9. IT痴汉的工作现状10-Sprint Planning
  10. C#使用for循环移除HTML标记