Luogu3175 HAOI2015 按位或 min-max容斥、高维前缀和、期望

传送门


套路题

看到\(n \leq 20\),又看到我们求的是最后出现的位置出现的时间的期望,也就是集合中最大值的期望,考虑min-max容斥。

\(E(max(S)) = \sum\limits_{T \subset S} (-1)^{|T| + 1} E(min(T))\),我们要求的就是一个集合至少有一个数字出现的期望时间。那么\(E(min(T)) = \frac{1}{\sum\limits_{S' \cap T \neq \emptyset} p_{S'}}\)

\(\sum\limits_{S' \cap T \neq \emptyset} p_{S'}\)不是很好求,考虑反过来求。它等于\(1 - \sum\limits_{S' \cap T = \emptyset} p_{S'} = 1 - \sum\limits_{S' \subset (2^N - 1 - T)}p_{S'}\),而$ \sum\limits_{S' \subset (2^N - 1 - T)}p_{S'}$就是子集和,高维前缀和求解即可。

#include<bits/stdc++.h>
#define ld long double
//This code is written by Itst
using namespace std;

inline int read(){
    int a = 0;
    char c = getchar();
    bool f = 0;
    while(!isdigit(c) && c != EOF){
        if(c == '-')
            f = 1;
        c = getchar();
    }
    if(c == EOF)
        exit(0);
    while(isdigit(c)){
        a = a * 10 + c - 48;
        c = getchar();
    }
    return f ? -a : a;
}

long double p[1 << 20];
int N , cnt1[1 << 20];

int main(){
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
    freopen("out","w",stdout);
#endif
    cin >> N;
    int all = 0;
    for(int i = 0 ; i < 1 << N ; ++i){
        cin >> p[i];
        cnt1[i] = cnt1[i >> 1] + (i & 1);
        if(p[i] > 1e-6)
            all |= i;
    }
    if(all != (1 << N) - 1){
        puts("INF");
        return 0;
    }
    for(int i = 0 ; i < N ; ++i)
        for(int j = 0 ; j < 1 << N ; ++j)
            if(!(j & (1 << i)))
                p[j | (1 << i)] += p[j];
    ld sum = 0;
    for(int i = 0 ; i < 1 << N ; ++i)
        if(1 - p[((1 << N) - 1) ^ i] > 1e-7)
            sum = sum + (cnt1[i] & 1 ? 1 : -1) / (1 - p[((1 << N) - 1) ^ i]);
    cout << fixed << setprecision(6) << sum;
    return 0;
}

转载于:https://www.cnblogs.com/Itst/p/10273809.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值