P3175 [HAOI2015] 按位或(min-max 容斥)
题意
有一个初始为
引入:min-max 容斥
以两个式子入手:
两个式子的证明都大差不差,不妨证明第一个。
令
不妨钦定
这个东西在一般的求最小值的题目下可能没啥用,但是这个东西在期望的意义下也是成立的(期望的线性性),即:
当
分析
将
考虑 min-max 容斥,将式子变为
我们有结论:设
证明:
将期望按照定义展开,
,考虑错位相减, ,相减得 ,也即 ,套用等比数列求和公式得 。
当然也有另外一种证法,考虑 DP,将
视为一个 DP 状态,有转移 ,意义就是,我花费了一次操作,如果随到了与 有交的数(概率为 ),就结束了,否则要继续操作,此时局面和操作前一样,所以从 转移来。解方程得 。
问题转化成了求
还有一个小细节,当
时间复杂度
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<array>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
int qwqx=0,qwqf=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
if(qwqx<0){qwqx=-qwqx;putchar('-');}
int qwqy=0;char qwqz[40];
while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=21,maxm=(1<<20),inf=0x3f3f3f3f;
const double eps=1e-10;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,m;
double a[maxm],f[maxm];
inline void solve_the_problem(){
n=rd(),m=(1<<n);
rep(i,0,m-1)scanf("%lf",&a[i]);
rep(i,0,m-1)f[i]=a[i];
rep(j,0,n-1)rep(i,0,m-1)if((i>>j)&1)f[i]+=f[i^(1<<j)];
double ans=0;
int cnt=0;
rep(S,1,m-1){
double p=1-f[(m-1)^S];
int siz=__builtin_popcount(S);
if(p<eps){
if(siz&1)++cnt;
else --cnt;
}else{
if(siz&1)ans+=1.0/p;
else ans-=1.0/p;
}
}
if(cnt)puts("INF");
else printf("%.9lf",ans);
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;
while(_--)solve_the_problem();
}
/*
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现