Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P3175 [HAOI2015]按位或

P3175 [HAOI2015]按位或

Min-Max反演:

$$
\max(S) = \sum _ {T \subseteq S} (-1) ^ {|T| + 1} \min (T)
$$

$$
\min(S) = \sum _ {T \subseteq S} (-1) ^ {|T| + 1} \max (T)
$$

其中 $\min, \max$ 表示集合中的最值。

拓展:

$$
kth\max (S) = \sum _ {T \subseteq S} (-1) ^ {|T| - k} \binom {|T| - 1} {k - 1} \min (T)
$$

或运算中,每一位是独立的,考虑每一位的时间,我们需要求全集的时间的最大值。那么我们考虑求所有子集的时间的最小值。考虑求一个集合 $S$ 的时间的最小值,这意味着前面选择的 $T$ 都满足 $S \cap T = \varnothing$ 。即 $\min (S) = 1 + \sum _ {S \cap T = \varnothing} p _ T \min(S)$ 。考虑操作一步,代价是 $1$;如果选到与 $S$ 无交集的 $T$,则仍然要花费 $\min(S)$ 的代价;选到有交集的,则结束,无贡献。即 $\min (S) = \frac 1 {1 - \sum _ {S \cap T = \varnothing} p _ T}$ 。所有的 $T$ 即 $S$ 的补集的子集。记 $f_ x$ 表示选择了 $x$ 的子集的概率,可以用一次高维前缀和解决。

查看代码
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
42
43
44
45
#include <cstdio>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
flag |= c == '-';
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x); read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
const double eps = 1e-8;
const int N = 1 << 20;
int n;
bool cnt[N];
double f[N];
int main ()
{
read(n);
for (int i = 0; i < 1 << n; ++i) scanf("%lf", f + i);
for (int i = 0; i < n; ++i)
for (int j = 0; j < 1 << n; ++j)
if (j >> i & 1) f[j] += f[j ^ 1 << i];
double res = 0;
for (int i = 1; i < 1 << n; ++i)
{
double t = 1 - f[(1 << n) - 1 ^ i];
if (t < eps) return puts("INF"), 0;
res += ((cnt[i] = cnt[i >> 1] ^ (i & 1)) ? 1 : -1) / t;
}
printf("%.10lf", res);
return 0;
}