跳到正文

个人记录

Card Collector(Min-Max容斥)

zzr8178541919约 2 分钟
原文
文章信息与版本

文章信息

发布
2021年2月28日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
#define x1 x11
#define x2 x22
#define y1 y11
#define y2 y22
#define z1 z11
#define z2 z22
const int mod=998244353;
const int INF=2e18;
const int maxn=1e6+5;
const double Pi=acos(-1.0);
double Exp=1e-6;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9')
	{
	    if(ch=='-')
	        f=-1;
	    ch=getchar();
	}
	while(ch>='0' && ch<='9')
	{
	    x=x*10+ch-'0';
	    ch=getchar();
	}
	return x*f;
}
int n;
double p[maxn];
double val[(1<<20)+5];
signed main()
{
	while(cin>>n)
	{
		if(n==0)
		break;
		for(int i=1;i<=n;i++)
		{
			scanf("%lf",&p[i]);
		}
		memset(val,0,sizeof(val));
		for(int s=1;s<=(1<<n)-1;s++)
		{
			double wns=0;
			for(int i=1;i<=n;i++)
			{
				if(s & (1<<(i-1)))
				wns=wns+p[i];
			}
			val[s]=1.0/wns;
		}
		double ans=0;
		for(int s=1;s<=(1<<n)-1;s++)
		{
			int cnt=0;
			for(int i=1;i<=n;i++)
			{
				if(s & (1<<(i-1)))
				cnt++;
			}
			if(cnt & 1)
			ans=ans+val[s];
			else
			ans=ans-val[s];
		}
		printf("%.3lf\n",ans);
	}
	return 0;
}	
/*
1
0.1
2
0.1 0.4
0

*/
原文