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 ), 问你收集全部卡片所需购买的食品数 ...
随机推荐
- [SAP ABAP开发技术总结]将文件存储到数据库表中,并可发送邮件
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- mysql优化技巧《转》
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- Spring AOP执行方法
execution(* springinaction.springidol.Instrument.play(..)) * 代表返回为任意类型 springinaction.springidol.I ...
- Codeforces708C Centroids 【树形dp】
题目链接 题意:给定一棵n个结点的树,问:对于每个结点,能否通过删除一条边并添加一条边使得仍是树,并且删除该结点后得到的各个连通分量结点数 <= n/2? 题解:树形dp,两遍dfs,第一遍df ...
- 08 高效的SQL
编写高效 SQL 需要以下知识 有关所查询内容的物理组织的知识 数据库能做什么的知识, 例如: 如果你不知道跳跃扫描索引及其用途, 那么你可能会看着模式说”索引丢了” SQL 所有错综复杂的知识 对目 ...
- Mysql delete,truncate,drop
1.delete 是DML(Data Manipulation Language),每次删除一行,作为事务记录在日志,可以回滚.delete from xxx 2.truncate是DDL(Data ...
- unique-substrings-in-wraparound-string(好)
https://leetcode.com/problems/unique-substrings-in-wraparound-string/ 好,我自己做出来的.多总结规律,多思考. package c ...
- golang时间
//获取本地location toBeCharge := "2015-01-01 00:00:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板 ...
- jQuery的单选,复选,下拉
单选 获取 ($('input:first').attr('vlaue')); 选中 ($('input:checked').val()); 属性值的设置$().val(属性值)$('input:fi ...
- SQL 比较时间大小
比较字符串类型的时间大小 数据库中的时间是varchar类型的,MySql使用CURDATE()来获取当前日期,SqlServer通过GETDATE()来获取当前日期 1. 直接使用字符串来比较 注意 ...