4036: [HAOI2015]按位或
Time Limit: 10 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 285 Solved: 166
[ Submit][ Status][ Discuss]
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.250.250.250.25
0.250.250.250.25
Sample Output
2.6666666667
HINT
对于100%的数据,n<=20
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn = (1 << 20);
typedef double DB;
const DB EPS = 5E-7;
DB f[maxn];
int n,N;
void fwt()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < N; j++)
{
if (j & (1 << i)) continue;
DB u = f[j],v = f[j | (1 << i)];
f[j] = u; f[j | (1 << i)] = v + u;
}
}
void ifwt()
{
for (int i = 0; i < n; i++)
for (int j = 0; j < N; j++)
{
if (j & (1 << i)) continue;
DB u = f[j],v = f[j | (1 << i)];
f[j] = u; f[j | (1 << i)] = v - u;
}
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n; N = (1 << n);
for (int i = 0; i < N; i++) scanf("%lf",&f[i]);
fwt();
for (int i = 0; i < N; i++)
f[i] = fabs(1.00 - f[i]) >= EPS ? -1.00 / (1.00 - f[i]) : 0;
ifwt();
if (f[N - 1] >= EPS) printf("%.10lf\n",f[N - 1]); else puts("INF");
return 0;
}