在做概率dp突然發現一道容斥的水體:
簡答考慮兩個集合的時候,E(1) = 1/p1 , E(2) = 1/p2 ,直接相加會出現一個問題,在沒抽到1的時候抽到了2,只要減一個E(1,2) = 1/(p1+p2); 這成立了就可以地推n個集合也是如此的.
#include<cstdio>
#include<iostream>
const double eps = 1e-10;
const int maxn = 3e2;
using namespace std;
double p[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
bool flag = 0;
for(int i = 0; i < n; i++){
scanf("%lf",&p[i]);
}
double ans = 0;
double temp;int cc;
for(int i = 1; i < (1<<n); i++){
temp = 0;
cc = 0;
for(int j = 0; j < n; j++){
if(i&(1<<j)){
temp+=p[j];
cc++;
}
}
if(cc&1)ans+=1.0/temp;
else ans -= 1.0/temp;
}
printf("%.6lf\n",ans);
}
return 0;
}