Card Collector(HDU 4336)
Card Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3001 Accepted Submission(s): 1435
Special Judge
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.
Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.
You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.
0.1
2
0.1 0.4
10.500
求期望
方法一:状压
逆序枚举所有状态 d[i] 表示状态为i时收集完所有卡片的期望步数。
d[i] = 1 + ∑(d[i | (1 << j)] * p[j])(ps: 累加所有走一步会增加新一张卡片的期望步数) + (1 - t) * d[i](ps: t为增加一张新卡片的概率);
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN (1 << 20) double d[MAXN + ];
double p[];
int main()
{
int n;
while(~scanf("%d", &n))
{
memset(d, , sizeof(d));
repu(i, , n) scanf("%lf", &p[i]);
if(( << n) - ) d[( << n) - ] = 0.0;
double t;
for(int i = ( << n) - ; i >= ; i--)
{
d[i] += 1.0;
t = 0.0;
for(int j = ; j < n; j++)
if(!(i & ( << j))) {
d[i] += p[j] * d[i | ( << j)];
t += p[j];
}
d[i] /= t;
}
printf("%.4lf\n", d[]);
} return ;
}
方法二:容斥
设Ai表示取到第i张卡片的期望,Ai = 1 / pi;
由容斥原理得:


#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN (1<<20) double p[];
double d[MAXN + ];
int main()
{
int n;
while(~scanf("%d", &n))
{
double re = 0.0;
repu(i, , n) scanf("%lf", &p[i]);
int m = ;
double t = 0.0;
repu(i, , ( << n)) {
m = , t = 0.0;
repu(j, , n) if(i & ( << j)) t += p[j], m++;
if(m & ) re += 1.0 / t;
else re -= 1.0 / t;
}
printf("%.4lf\n", re);
}
return ;
}
Card Collector(HDU 4336)的更多相关文章
- HDU 4336:Card Collector(容斥原理)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Special Judge Problem Descriptio ...
- HDU 4336 Card Collector 期望dp+状压
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...
- 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】
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 4336 Card Collector (概率dp+位运算 求期望)
题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDOJ 4336 Card Collector
容斥原理+状压 Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu4336 Card Collector 状态压缩dp
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- min-max容斥 hdu 4336 && [BZOJ4036] 按位或
题解: 之前听说过这个东西但没有学 令$max(S)$表示S中编号最大的元素,$min(S)$表示编号中最小的元素 $$max(S)=\sum{T \in S} {(-1)}^{|T|+1} min( ...
- hdu 4336 概率dp + 状压
hdu 4336 小吃包装袋里面有随机赠送一些有趣的卡片,如今你想收集齐 N 张卡片.每张卡片在食品包装袋里出现的概率是p[i] ( Σp[i] <= 1 ), 问你收集全部卡片所需购买的食品数 ...
随机推荐
- fzu 1015 土地划分(判断线段相交+求出交点+找规律)
链接:http://acm.fzu.edu.cn/problem.php?pid=1015 Problem 1015 土地划分 Accept: 714 Submit: 1675Time Lim ...
- C# 线程(五):线程池
From : http://kb.cnblogs.com/page/42531/ 在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然 ...
- CSS笔记(五)字体
CSS 字体属性定义文本的字体系列.大小.加粗.风格(如斜体)和变形(如小型大写字母). 参考:http://www.w3school.com.cn/css/css_font.asp CSS字体系列 ...
- MVC服务器前台提示
[HttpPost] public ActionResult AddMsg(MsgModel model) { string strSql = "insert into tbl_msg(ti ...
- Android获取窗体信息的Util方法
package com.wangyi.tools; import android.app.Activity; import android.util.DisplayMetrics; public cl ...
- RTSP协议、RTMP协议、HTTP协议的区别
理论上RTSP RTMPHTTP都可以做直播和点播,但一般做直播用RTSP RTMP,做点播用HTTP.做视频会议的时候原来用SIP协议,现在基本上被RTMP协议取代了. RTSP. RTMP.HTT ...
- MyEclipse JCO tomcat 提示查找不到sapjco3.dll
java.lang.UnsatisfiedLinkError: no sapjco3 in java.library.path 1.system32添加sapjco3.dll 2.tomcat bin ...
- 关于inline-block 元素之间为何会产生间隔
关于inline-block 元素之间为何会产生间隔 现象: <body> <input type="text"> <input type=" ...
- For Exam (Java常用设计模式) 介绍
一 创建型模式 工厂模式(Factory): 定义一个用以创建对象的接口 抽象工厂模式(Abstract Factory): 提供一个创建一系列相关或相互依赖对象的接口 单例模式(Singleton) ...
- 手机CPU知识扫盲:谈谈手机CPU架构与原理 (全
CPU是手机上面最复杂,最贵的Soc(芯片),担任的也是手机中大脑的位 置,是手机跑分性能的决定性硬件.智能手机发展到今天,各大手机CPU厂商也从春秋战国逐渐到了现在四国鼎立的时代(高通,MTK,三星 ...