[BZOJ4036] [HAOI2015]按位或

传送门https://lydsy.com/JudgeOnline/problem.php?id=4036

Description

刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal

的or)操作。选择数字i的概率是p[i]。保证0<=p[i]<=1,Σp[i]=1问期望多少秒后,你手上的数字变成2^n-1。

Input

第一行输入n表示n个元素,第二行输入2^n个数,第i个数表示选到i-1的概率

Output

仅输出一个数表示答案,绝对误差或相对误差不超过1e-6即可算通过。如果无解则要输出INF

Sample Input

2
0.25 0.25 0.25 0.25

Sample Output

2.6666666667

Solution

\(\min-\max\)容斥套路题。

\(\min\{S\}\)表示\(S\)最早出现的元素出现时间的期望,\(\max\{S\}\)同理。

那么有:

\[\max\{S\}=\sum_{T\subseteq S}(-1)^{|T|+1}\min\{T\} \]

考虑怎么算\(\min\),根据定义,有:

\[\min\{S\}=\frac{1}{\sum_{S\subseteq T}p(T)} \]

但是这个玩意不是很好算,有一个很巧妙的想法就是正难则反,设\(x=S\oplus(2^n-1)\),也就是\(S\)的补集,那么我们可以枚举\(x\)的子集,剩下没枚举到的就是分母要枚举的东西。

那么快速处理子集和可以用\(fwt\)来实现,具体的代码就很短了。

#include<bits/stdc++.h>
using namespace std;
 
void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double

const int maxn = 2e6+10;
const lf eps = 1e-8;

int m,n;
lf p[maxn];

void fwt(lf *r) {
	for(int i=1;i<n;i<<=1)
		for(int j=0;j<n;j+=i<<1)
			for(int k=0;k<i;k++)
				r[i+j+k]+=r[j+k];
}

int main() {
	read(m),n=1<<m;
	for(int i=0;i<n;i++) scanf("%lf",&p[i]);
	fwt(p);lf ans=0;
	for(int i=1;i<n;i++) if(1.0-p[i^(n-1)]>eps) ans+=1.0/(1.0-p[i^(n-1)])*(lf)(__builtin_popcount(i)&1?1:-1);
	for(int i=0;i<m;i++) if(1.0-p[(1<<i)^(n-1)]<eps) return puts("INF"),0;
	printf("%lf\n",ans);
	return 0;
}
posted @ 2019-03-12 11:03  Hyscere  阅读(103)  评论(0编辑  收藏  举报