Card Collector
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 notice that to win the award, you must buy much more snacks than it seems to be. To convince your friends not to waste money any more, you should find the expected number of snacks one should buy to collect a full suit of cards.
InputThe first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, ..., pN, (p1 + p2 + ... + pN <= 1), indicating the possibility of each card to appear in a bag of snacks.
Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.OutputOutput one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.
You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.
Sample Input
1
0.1
2
0.1 0.4
Sample Output
10.000
10.500
题意:收集卡片的故事.有 N 种卡片,买一包零食最多有一张,可能没有,然后给出每种卡片的出现概率。想要每种都收集至少一张,问需要买的零食包数期望
题解:用dp做,用数字的二进制来表示状态,例如,4 种卡片的话 1110 表示 2-4 卡片至少有一种,第 1 种没有的情况
那么: dp[i] = SUM( pk * dp[i+k] ) + ( 1 - SUM(pk) ) * dp[i] + 1 (设 k 指的是缺少的卡片,pk 是获得这种卡片的概率)
即 dp[i] = ( SUM( pk * dp[i+k] ) + 1 ) / SUM(pk)
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; const int MAXN = (<<)+;
int n;
double p[];
double dp[MAXN]; int main()
{
while(scanf("%d",&n)!=EOF)
{
for (int i=;i<n;i++)
scanf("%lf",&p[i]); int mmm = (<<n)-; //二进制为 n 个 1
dp[mmm]=;
for (int i=mmm-;i>=;i--) //所有情况都遍历到
{
double all_p=;
dp[i]=;
for (int j=;j<n;j++)
{
if ( (i&(<<j))== ) //第 j 种卡片没有
{
dp[i]+=p[j]*dp[i+(<<j)]; //dp [i+(1<<j)] 肯定赋过值了
all_p+=p[j];
}
}
dp[i]/=all_p;
}
printf("%lf\n",dp[]);
}
}
Card Collector的更多相关文章
- HDOJ 4336 Card Collector
容斥原理+状压 Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 4336:Card Collector(容斥原理)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Descriptio ...
- Card Collector(HDU 4336)
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu4336 Card Collector 状态压缩dp
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4336 Card Collector(动态规划-概率DP)
Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...
- HDU 4336 Card Collector 期望dp+状压
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...
- 【HDU4336】Card Collector(Min-Max容斥)
[HDU4336]Card Collector(Min-Max容斥) 题面 Vjudge 题解 原来似乎写过一种状压的做法,然后空间复杂度很不优秀. 今天来补一种神奇的方法. 给定集合\(S\),设\ ...
- 【HDU4336】Card Collector (动态规划,数学期望)
[HDU4336]Card Collector (动态规划,数学期望) 题面 Vjudge 题解 设\(f[i]\)表示状态\(i\)到达目标状态的期望 \(f[i]=(\sum f[j]*p[j]+ ...
- HDU 4336——Card Collector——————【概率dp】
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [HDU4336]Card Collector(min-max容斥,最值反演)
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- go语言编程小tips
go语言一个比较方便的特性是你不需要显示的定义一个变量.例如,在c语言中,你想要使用一个int型变量,那么代码如下 int i; i =0; i++; 而在go语言中, i := 0; i++ 这样你 ...
- netty handle处理流程
server handlerAdded server channelRegistered server channelActive server read server channelInactive ...
- C#设计模式---观察者模式简单例子
在开发过程中经常遇到一个模块中的 一个方法调用了其他模块中相关的方法 比如说在一个系统中,如果出现了错误,就调用专门进行错误处理的模块中的方法进行错误处理 而因为错误处理的操作有很多,所以将这些具体的 ...
- zabbix通过snmp监控linux主机
1.安装net-snmp [root@db01 ~]# yum install -y net-snmp 2.修改配置文件 [root@db01 ~]# vim /etc/snmp/snmpd.conf ...
- phpMyAdmin搭建及管理多台数据库服务器
phpMyAdmin搭建及管理多台数据库服务器 环境说明: 系统版本 CentOS 6.9 x86_64 软件版本 nginx-1.12.2 php-5.5. ...
- sersync+rsync实时数据同步
sersync+rsync实时数据同步 1.相关背景介绍 前面有关文章配置实现了rsync增量同步以及配置为定时同步,但是在实际生产环境中需要实时的监控数据从而进行同步(不间断同步),可以采取inot ...
- 2017.7.18 windows下ELK环境搭建
参考来自:Windows环境下ELK平台的搭建 另一篇博文:2017.7.18 linux下ELK环境搭建 0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1.7, ...
- Angular 学习笔记——$interpolateProvide
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- 编程算法 - 食物链 并查集 代码(C)
食物链 并查集 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有N仅仅动物, 分别编号为1,2,...,N. 全部动物都属于A,B,C中的一种 ...
- kali渗透综合靶机(一)--Lazysysadmin靶机
kali渗透综合靶机(一)--Lazysysadmin靶机 Lazysysadmin靶机百度云下载链接:https://pan.baidu.com/s/1pTg38wf3oWQlKNUaT-s7qQ提 ...