Card Collector

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1708 Accepted Submission(s): 780
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
当时看到,这一题,一点思路都没有啊,后来,发现是用容斥原理,这个表示没用过,补上这个知识点!
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
double p[25];
int main()
{
int n,i,flag,j,k,all,cnt;
double sum,s,temp;
while(scanf("%d",&n)!=EOF)
{
flag=1;
for(i=0;i<n;i++)
{
scanf("%lf",&p[i]);
}
sum=0;
flag=1;
all=1<<n;
for(i=1;i<all;i++)
{
cnt=0;
temp=0;
for(j=0;j<n;j++)
{
if(i&(1<<j))//相交
{
temp+=p[j];cnt++;
}
}
if(cnt&1)//奇加偶减
sum+=1.0/temp;
else
sum-=1.0/temp;
}
printf("%.4f\n",sum);
}
return 0;
}
再来一个状态压缩dp,这里我们可以推出dp[n]=1+all dp[n](空的)+all dp[n](重复的)+all dp[k](没有但是加一个就有了);这样就好做了,好像这一题还非要写成输出4位,输出3位还是错的,样例应该是有问题的!很坑啊,有木有!
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
double p[25],dp[1<<20];
int main ()
{
int n,i,j,all;
double allp,tempallp,sum;
while(scanf("%d",&n)!=EOF)
{
allp=0;
for(i=0;i<n;i++)
{
scanf("%lf",&p[i]);
allp+=p[i];
}
all=1<<n;
dp[0]=0; for(i=1;i<all;i++)
{
tempallp=allp;
sum=1;//买了一袋
for(j=0;j<n;j++)
{
if(i&(1<<j))
{
sum+=p[j]*dp[i^(1<<j)]; }
else
{
tempallp-=p[j]; }
}
dp[i]=sum/tempallp; }
printf("%.4f\n",dp[all-1]);
} return 0;
}

hdu4336 Card Collector 状态压缩dp的更多相关文章

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

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

  2. [HDU4336]:Card Collector(概率DP)

    题目传送门 题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生日礼物.商店里一共有种礼物.夏川每得到一种礼物,就会获得相应喜悦值$W_i$(每种礼物的喜悦值不能重复获得).每 ...

  3. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  4. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  5. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  6. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  7. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  8. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  9. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

随机推荐

  1. SQL Server 查看数据表占用空间大小的SQL语句

    ) ) if object_id('tempdb..#space') is not null drop table #space ),rows ),data ),index_size ),unused ...

  2. jQ的自定义插件

    此文运用的是优雅的Markdown而书 项目中写js时,有很多时候有需要重复利用的东西,我们可以给它们写成一个插件的形式,这样阅读性和适用性都会大大提高.最近抽个时间,好一番的研究了下 Jcrop 的 ...

  3. 常用Java片段

    1. 字符串与整型的相互转换 String a = String.valueOf(2);   //integer to numeric string int i = Integer.parseInt( ...

  4. yii2.0 从控制器到视图的输出

    在controllers/SiteController.php文件中,添加 public function actionSay($message = 'Hello') { return $this-& ...

  5. ZOJ 3209 Treasure Map 精确覆盖

    题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...

  6. ECSHOP 开发总结

    今天算是仔细学习ecshop 的第一天,实话说,如果不是任务紧,肯定不用这个东西.2013年之后都不再维护了.使用起来万一出什么BUG 就不好了.而且不是纯粹的MVC ,看代码也是怪怪的呢.但是都已经 ...

  7. python request模块学习

    安装: pip install requests 使用: import requests HTTP请求:GET.POST.PUT.DELETE.HEAD.OPTIONS 1) get res = re ...

  8. 自定义TWebBrowser浏览器控制遇到的一些问题

    最近工作需要,要将TWebBrowser样式改头换面,包括菜单,滚动条等,都要换掉. 由于滚动条已经屏蔽掉,所以关于页面滚动等,全部都需要自己写代码来模拟了.模拟的过程中发现获得页面的客户区大小Cli ...

  9. css 自适应布局阮一峰

    转载一篇文章: 自适应网页设计(Responsive Web Design) 作者: 阮一峰 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小 ...

  10. 个人信用卡管理 - iOS UI原型