【模板】Min-Max容斥-bzoj4036按位或

传送门:bzoj4036


Min-Max容斥

max ⁡ ( S ) \max(S) max(S)为集合 S S S中的最大值, min ⁡ ( S ) \min(S) min(S)为集合 S S S中的最小值, ∣ S ∣ |S| S为集合 S S S的大小,则存在:
max ⁡ ( S ) = ∑ T ⊆ S ( − 1 ) ∣ T ∣ + 1 min ⁡ ( T ) min ⁡ ( S ) = ∑ T ⊆ S ( − 1 ) ∣ T ∣ + 1 max ⁡ ( T ) \max(S)=\sum_{T\subseteq S}(-1)^{|T|+1}\min(T) \\ \min(S)=\sum_{T\subseteq S}(-1)^{|T|+1}\max(T) max(S)=TS(1)T+1min(T)min(S)=TS(1)T+1max(T)

同样地, M i n − M a x Min-Max MinMax容斥在期望下成立,即:
E ( max ⁡ ( S ) ) = ∑ T ⊆ S ( − 1 ) ∣ T ∣ + 1 E ( min ⁡ ( T ) ) E ( min ⁡ ( S ) ) = ∑ T ⊆ S ( − 1 ) ∣ T ∣ + 1 E ( max ⁡ ( T ) ) E(\max(S))=\sum_{T\subseteq S}(-1)^{|T|+1}E(\min(T)) \\ E(\min(S))=\sum_{T\subseteq S}(-1)^{|T|+1}E(\max(T)) E(max(S))=TS(1)T+1E(min(T))E(min(S))=TS(1)T+1E(max(T))

(证明略)


题解

套用 E ( max ⁡ ( S ) ) = ∑ T ⊆ S ( − 1 ) ∣ T ∣ + 1 E ( min ⁡ ( T ) ) E(\max(S))=\sum_{T\subseteq S}(-1)^{|T|+1}E(\min(T)) E(max(S))=TS(1)T+1E(min(T))

其中 E ( min ⁡ ( T ) ) = 1 ∑ G ∩ T ≠ ϕ p [ G ] E(\min(T))=\dfrac{1}{\sum\limits_{G\cap T\neq \phi}p[G]} E(min(T))=GT̸=ϕp[G]1

求出 P [ S ] = ∑ T ⊆ S p [ T ] P[S]=\sum\limits_{T\subseteq S}p[T] P[S]=TSp[T],则 E ( min ⁡ ( T ) ) = 1 1 − P [ ( 2 n − 1 ) ⊕ T ] E(\min(T))=\dfrac{1}{1-P[(2^n-1)\oplus T]} E(min(T))=1P[(2n1)T]1

P P P做个 F W T FWT FWT即可。


代码

#include
using namespace std;
typedef double db;
const int N=(1<<20)+5;
const db eps=1e-10;

int n,s,sz[N];
db p[N],ans;

int main(){
    int i,j,k;
    scanf("%d",&n);s=1<<n;
    for(i=0;i<s;++i) scanf("%lf",&p[i]),sz[i]=sz[i>>1]+(i&1);
    for(i=1;i<s;i<<=1)
        for(j=0;j<s;j+=(i<<1))
            for(k=0;k<i;++k)
              p[i+j+k]+=p[j+k];
    for(i=0,s--;i<=s;++i) if((1-p[s^i])>eps) ans+=((sz[i]&1)?1:(-1))/(1-p[s^i]);
    if(ans<eps) printf("INF");else printf("%.10lf",ans);
    return 0;
}

你可能感兴趣的:(容斥原理,FMT,子集卷积)