HDU4336 - Card Collector

考虑 Min-Max 容斥。用 $\min(S)$ 表示 $S$ 中出现至少一个元素的期望时间,用 $\max(S)$ 表示 $S$ 中每一个元素都出现的期望时间。

则:

$$
\begin{aligned}
\min(S) &= \frac{1}{\sum\limits_{i \in S} p_i} \\
\max(S) &= \sum\limits_{S’ \subseteq S} \min(S’) (-1)^{|S’| - 1}
\end{aligned}
$$

答案显然是让我们求 $\max(\texttt{全集})$ ,故状压一下即可。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// =================================
// author: memset0
// date: 2019.01.14 07:43:43
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 25;
int n;
double ans, sum, p[N];

void main() {
while (~scanf("%d", &n)) {
ans = 0;
for (int i = 1; i <= n; i++) scanf("%lf", &p[i]);
for (int x = 1; x < (1 << n); x++) {
sum = 0;
for (int i = 1; i <= n; i++)
if (x & (1 << (i - 1)))
sum += p[i];
ans += (__builtin_popcount(x) & 1 ? 1 : -1) / sum;
}
printf("%.6lf\n", ans);
}
}

} signed main() { return ringo::main(), 0; }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×