HDU 4336 容斥原理

/**
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336
题意:期望容斥; 改写容斥公式即可;
*/
#include
#define ll long long
using namespace std;

/**********************************************Head-----Template****************************************/
bool Finish_read;
templateinline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
templateinline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
templateinline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
templateinline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){ll gg=gcd(a,b);a/=gg;if(a<=LLONG_MAX/b) return a*b;return LLONG_MAX;}
/********************************Head----Temlate**********************************************/

ll l,r;
int n;
double ans;

vectorvec;

void dfs(int step,int flag,double tmp){
	if(step==vec.size()){
		if(tmp!=0) ans+=flag*1.0/tmp;
		return ;
	}
	dfs(step+1,flag,tmp);
	dfs(step+1,-flag,tmp+vec[step]);
}

int main (){
	while(~scanf("%d",&n)){
		vec.clear();
		for(int i=1;i<=n;i++){
			double x;scanf("%lf",&x);
			vec.push_back(x);
		}
		ans=0;
		dfs(0,-1,0);
		printf("%.6f\n",ans);
	}
	return 0;
}

 

你可能感兴趣的:(ACM--组合数学)