[HAOI2015]按位或
作者:互联网
[HAOI2015]按位或
min-max 容斥
记 \(\max\{S\}\) 表示 \(S\) 中每一数都被选到过的期望时间,\(\min\{S\}\) 表示 \(S\) 中出现至少一个数被选到的期望时间
答案显然为 \(\max\{U\}\) ,\(U= \{n \cdot 1\}\)
直接求 \(\max\{U\}\) 不好求,利用 min-max 容斥转化为求 \(\min\{S\}\)
\(\max\{S\} = \sum_{T \subseteq S} (-1)^{|T| + 1} \cdot \min\{T\}\)
记 \(f(S)\) 表示每一秒选择 \(S\) 子集的概率
则第一次选到 \(S\) 子集的期望时间,也就是 \(\min\{S\}\) 为 :
\(\min\{S\} = \sum_{i = 1} ^ {+\infty} i \cdot (1 - f(\complement_US)) \cdot f ^ {i - 1}(\complement_US)\)
注意 :\(1 - f(\complement_US) \not = f(S)\)
左边是选到的有 \(S\) 内的数的概率,而右边是选到的全是 \(S\) 中数(也就是选到 \(S\) 子集)的概率,显然左边概率比右边大
发现 \(\min\{S\}\) 满足几何分布,于是 \(\min\{S\} = \frac{1}{1 - f(\complement_US)}\)
几何分布可以理解为:
抽一个物品,每次中奖率为 1% ,那么期望抽100次就能抽中(然而现实显然不是这样,但我们只管期望)
考虑求 \(f(S)\)
记 \(g(S)\) 表示选择 \(S\) 的概率,则
\(f(S) = \sum_{T\subseteq S} g(T)\)
发现是个 FWTOR,得以求解
#include <bits/stdc++.h>
#define re register
// #define int long long
// #define pair pair<int, int>
// #define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout);
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
using namespace std;
inline int read()
{
re int x = 0, f = 0;
re char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = 1; ch = getchar();}
while (isdigit(ch)) {x = (x << 3) + (x << 1) + ch - 48; ch = getchar();}
return f ? -x : x;
}
inline string getstr()
{
string res = "";
re char ch = getchar();
while (isspace(ch)) ch = getchar();
while (!isspace(ch)) res.push_back(ch), ch = getchar();
return res;
}
const int N = 1.1e6 + 6;
const double eps = 1e-6;
namespace FastWT
{
#define OR 0
#define AND 1
#define XOR 2
template<short op> inline void FWT(double *A, const int &siz, const bool &flag)
{
for (re int len = 1; len < siz; len <<= 1)
{
const int rlen = len << 1;
for (re int i = 0; i < siz; i += rlen)
for (re int j = 0; j < len; ++j)
{
double x = A[i + j], y = A[i + j + len];
if (op == OR)
A[i + j] = x,
A[i + j + len] = y + (flag ? - 1. : 1.) * x;
else if (op == AND)
A[i + j] = x + (flag ? - 1. : 1.) * y,
A[i + j + len] = y;
else
A[i + j] = (x + y) / (flag ? 2. : 1.),
A[i + j + len] = (x - y) / (flag ? 2. : 1.);
}
}
}
}
using namespace FastWT;
double f[N];
int cnt[N];
signed main()
{
int siz;
scanf("%d", &siz);
siz = 1 << siz;
for (re int i = 0; i < siz; ++i) scanf("%lf", &f[i]);
for (re int i = 1; i < siz; ++i) cnt[i] = cnt[i >> 1] + (i & 1);
FWT<OR>(f, siz, 0);
for (re int i = 0; i < siz - 1; ++i) if (1. - f[i] < eps) return puts("INF"), 0;
double ans = 0;
const int X = siz - 1;
for (re int i = 1; i < siz; ++i) ans = ans + (cnt[i] + 1 & 1 ? -1.0 : 1.0) / (1.0 - f[i ^ X]);
printf("%.10lf", ans);
return 0;
}
标签:max,min,int,选到,HAOI2015,按位,siz,define 来源: https://www.cnblogs.com/After-glow/p/15836744.html