题意

题目链接

\(N\)个物品,每次得到第\(i\)个物品的概率为\(p_i\),而且有可能什么也得不到,问期望多少次能收集到全部\(N\)个物品

Sol

最直观的做法是直接状压,设\(f[sta]\)表示已经获得了\(sta\)这个集合里的所有元素,距离全拿满的期望,推一推式子直接转移就好了

主程序代码:

int N;
double a[MAXN], f[MAXN];
signed main() {
// freopen("a.in", "r", stdin);
while(scanf("%d", &N) != EOF) {
memset(f, 0, sizeof(f)); double res = 1.0;
for(int i = 0; i < N; i++) scanf("%lf", &a[i]), res -= a[i];
int Lim = (1 << N) - 1;
for(int sta = Lim - 1; sta >= 0; sta--) {
double now = 1 - res, sum = 0;
for(int i = 0; i < N; i++)
if(sta & (1 << i)) now -= a[i];
else sum += f[sta | (1 << i)] * a[i];
sum += 1.0;
f[sta] = sum / now;
}
printf("%.4lf\n", f[0]);
}
return 0;
}

另一种MinMax容斥的做法:

设\(max(s)\)为\(s\)集合中的最大元素,\(min(T)\)为集合\(T\)中的最小元素

那么有\(E(max(s)) =\sum_{T \subseteq S} (-1)^{|T| + 1} E(min \{ T \})\)

这里的\(E(max(S))\)显然就是我们要求的答案

\(E(min \{ T\}) = \frac{1}{\sum_{i \in T} p_i}\)

直接dfs一波

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 2e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-7;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N;
double a[MAXN], ans;
void dfs(int x, double p, int opt) {
if(x == N) {
if(p > eps) ans += opt / p;
return ;
}
dfs(x + 1, p, opt);
dfs(x + 1, p + a[x], -opt);
}
signed main() {
// freopen("a.in", "r", stdin);
while(scanf("%d", &N) != EOF) {
for(int i = 0; i < N; i++) scanf("%lf", &a[i]);
ans = 0;
dfs(0, 0, -1);
printf("%.4lf\n", ans);
}
return 0;
}

HDU4336 Card Collector(期望 状压 MinMax容斥)的更多相关文章

  1. HDU 4336 Card Collector(状压 + 概率DP 期望)题解

    题意:每包干脆面可能开出卡或者什么都没有,一共n种卡,每种卡每包爆率pi,问收齐n种卡的期望 思路:期望求解公式为:$E(x) = \sum_{i=1}^{k}pi * xi + (1 - \sum_ ...

  2. codeforces 342D Xenia and Dominoes(状压dp+容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...

  3. bzoj2669 [cqoi2012]局部极小值 状压DP+容斥

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2669 题解 可以发现一个 \(4\times 7\) 的矩阵中,有局部最小值的点最多有 \(2 ...

  4. 一本通 1783 矩阵填数 状压dp 容斥 计数

    LINK:矩阵填数 刚看到题目的时候感觉是无从下手的. 可以看到有n<=2的点 两个矩形. 如果只有一个矩形 矩形外的方案数容易计算考虑 矩形内的 必须要存在x这个最大值 且所有值<=x. ...

  5. P3160 [CQOI2012]局部极小值 题解(状压DP+容斥)

    题目链接 P3160 [CQOI2012]局部极小值 双倍经验,双倍快乐 解题思路 存下来每个坑(极小值点)的位置,以这个序号进行状态压缩. 显然,\(4*7\)的数据范围让极小值点在8个以内(以下示 ...

  6. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  7. [清华集训2015 Day1]主旋律-[状压dp+容斥]

    Description Solution f[i]表示状态i所代表的点构成的强连通图方案数. g[i]表示状态i所代表的的点形成奇数个强连通图的方案数-偶数个强连通图的方案数. g是用来容斥的. 先用 ...

  8. NOIp模拟赛 巨神兵(状压DP 容斥)

    \(Description\) 给定\(n\)个点\(m\)条边的有向图,求有多少个边集的子集,构成的图没有环. \(n\leq17\). \(Solution\) 问题也等价于,用不同的边集构造DA ...

  9. uoj#37. 【清华集训2014】主旋律(状压dp+容斥)

    传送门 第一眼容斥,然后我就死活容不出来了-- 记\(f_i\)为点集\(i\)中的点强联通的方案数,那么就是总的方案数减去使\(i\)不连通的方案数 如果\(i\)不连通的话,我们可以枚举缩点之后拓 ...

随机推荐

  1. Elasticsearch Java API简介

    加入依赖 我本地的Elasticsearch的版本是2.1.0,因此加入相应的maven依赖 <dependency> <groupId>org.elasticsearch&l ...

  2. jdbc连接2(不可以注入)

    public void login1(String username, String password) throws ClassNotFoundException, SQLException { / ...

  3. 【NOIP2018】保卫王国 动态dp

    此题场上打了一个正确的$44pts$,接着看错题疯狂$rush$“正确”的$44pts$,后来没$rush$完没将之前的代码$copy$回去,直接变零分了..... 这一题我们显然有一种$O(nm)$ ...

  4. POJ 1067

    #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> usin ...

  5. java基本语法一

    1 关键字和保留字 1.1 关键字 关键字的定义:被java语言赋予了特殊含义,用做专门用途的字符串(单词). 关键字的特点:关键字中的所有字母都是小写. 1.2 保留字 java保留字:现有Java ...

  6. PHP基础记录

    1. require和require_once的区别 require_once()包涵是绝对路径 include() 和require() :语句包括并运行指定文件. include() 产生一个警告 ...

  7. Backing Up and Restoring HBase Data

    There are two strategies for backing up HBase:1> Backing it up with a full cluster shutdown2> ...

  8. Java之IO(零)总结

    转载请注明原出处:http://www.cnblogs.com/lighten/p/7274378.html 1.前言 本章是对之前所讲述的整个Java的IO包的一个总结,抽出个人认为比较重要的知识点 ...

  9. C#设计模式系列目录

    http://www.cnblogs.com/libingql/archive/2012/04/16/2451608.html 抽空,学习,加强!

  10. SourceTree跳过Atlassian账号,免登陆,跳过初始设置

    SourceTree 安装之后需要使用账号登陆以授权,并且是强制登陆. 登录过程非常漫长,并未在不FQ的情况下是不能成功的,下面记录一下跳过登录的方法. 装之后,转到用户本地文件夹下的 SourceT ...