Card Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5272    Accepted Submission(s): 2688
Special Judge

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 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.

 
Input
The 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.

 
Output
Output 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
 
Source
 
    容斥暂时搞不懂,f[S]表示当前收集状态为S距离目标的期望购买次数,S能推出的状态有n+1种,把推完之后状态还是S的移到方程左边,其他的在右边最后算一下答案就好了。(显然推完之后还是S的情况只可能是下一次买的卡是空的或者S中已经有了)。
    

 #include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stack>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<time.h>
#include<algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
#define debug puts("debug")
#define LL long long
#define pii pair<int,int>
#define eps 1e-10
#define inf 0x3f3f3f3f double f[(<<)+];
double p[];
int main()
{
int t,i,j,k,n,m,u,v;
while(scanf("%d",&n)==){double none=;
for(i=;i<n;++i) scanf("%lf",p+i),none+=p[i];
int all=(<<n)-;
memset(f,,sizeof(f));
for(i=all-;i>=;--i){
double Pj=(double)1.00-none,s=;
for(j=;j<n;++j){
if(i&(<<j)){
Pj+=p[j];
}
else{
s+=p[j]*f[i|(<<j)];
}
}
s+=1.00;
Pj=1.00-Pj;
f[i]=s/Pj;
}
printf("%.5f\n",f[]);
}
return ;
}

  

      容斥做法的话不是很懂说一下简单思路。

    E(至少得到i号卡)=1/pi,把他记作E(i),这个的含义就是一直买卡直至第一次出现i卡时停止的期望购买次数,这个式子是可以推出来的,数学不好真tm烦= =E(至少得到A卡或者B卡的)=E(A|B).

    我们要求的就是E(至少得到1&2&...&N号卡),不妨记作E(1&2&...&N)=E(1)+E(2)+...+E(n)-{E(1|2)+E(2|3)+......}+{E(1|2|3)+...}.....

就这样一直利用容斥定理奇加偶减算出最终的答案。

    以后会了可能会补。

HDU-4336-期望dp-bit的更多相关文章

  1. hdu 4336 概率dp + 状压

    hdu 4336 小吃包装袋里面有随机赠送一些有趣的卡片,如今你想收集齐 N 张卡片.每张卡片在食品包装袋里出现的概率是p[i] ( Σp[i] <= 1 ), 问你收集全部卡片所需购买的食品数 ...

  2. poj 2096 , zoj 3329 , hdu 4035 —— 期望DP

    题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...

  3. HDU 4405 期望DP

    期望DP算是第一题吧...虽然巨水但把思路理理清楚总是好的.. 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落到a点时直接飞向b点.求走到n ...

  4. HDU 3853(期望DP)

    题意: 在一个r*c的网格中行走,在每个点分别有概率向右.向下或停止不动.每一步需要的时间为2,问从左上角走到右下角的期望时间. SOL: 非常水一个DP...(先贴个代码挖个坑 code: /*== ...

  5. hdu 4336 概率dp

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

  6. HDU 4035 期望dp

    这道题站在每个位置上都会有三种状态 死亡回到起点:k[i] 找到出口结束 e[i] 原地不动 p[i] k[i]+e[i]+p[i] =1; 因为只给了n-1条路把所有都连接在一起,那么我们可以自然的 ...

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

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

  8. HDU 4405 Aeroplane chess 期望dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/ ...

  9. HDU 3853 LOOPS:期望dp【网格型】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意: 有一个n*m的网格. 给出在每个格子时:留在原地.向右走一格,向下走一格的概率. 每走一 ...

  10. 升级降级(期望DP)2019 Multi-University Training Contest 7 hdu杭电多校第7场(Kejin Player)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意: 有 1~n 个等级,你现在是1级,求升到n级的花费期望.会给你n个条件(i~i+1级升级 ...

随机推荐

  1. nginx 下使用 bootstrap 字体的问题

    使用boostrap时,出现 glyphicons-halflings-regular.ttf glyphicons-halflings-regular.woff glyphicons-halflin ...

  2. 信号(Django信号、Flask信号、Scrapy信号)

    简介 Django.Flask.scrapy都包含了一个“信号分配器”,使得当一些动作在框架的其他地方发生的时候,解耦的应用可以得到提醒. 通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒 ...

  3. 基于 Spark 的文本情感分析

    转载自:https://www.ibm.com/developerworks/cn/cognitive/library/cc-1606-spark-seniment-analysis/index.ht ...

  4. 跟我学Makefile(四)

    使用函数:函数调用,很像变量的使用,也是以“$”来标识的,其语法如下: $(<function> <arguments>) 或是 ${<function> < ...

  5. Selenium之firefox浏览器的启动

    1.编写如下代码 import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; /** ...

  6. js cookie的读写

    cookie是一小段信息,以键/值对的信息保存在计算机硬盘上的字符串, cookie存储容量大概在4kb,不同的浏览器厂家对cookie大小的限制有微微的差异:cookie主要的本质是“识别”,通过识 ...

  7. Java实现最基本的集中排序

    排序是一个很重要的概念,现实生活中,我们需要为很多的东西排序.下面我们就介绍几种简单的排序的方法和最基本的思想. 1.冒泡排序:假设一个数组中有10个数字,从左边开始

  8. Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, ...

    使用oracle数据库一个多月依赖这问题一直都得不到解决,最近任务不是很忙了,所以决定把这问题解决掉.写一篇文章做记录. 以上错误主要是net程序oracle数据库使用了Microsoft Enter ...

  9. Linux_Chrome出现Adobe Flash Player is out of date解决方法

    在安装Google的Chrome后都有出现Adobe Flash Player is out of date的问题. Chrome浏览器用的播放器插件是PepperFlashPlayer.而且是内置的 ...

  10. selenium-python读取XML文件

    首先这是我们要读取的XML文件 <?xml version="1.0" encoding="utf-8" ?><info> <ba ...