1. 程式人生 > >luoguP3175 [HAOI2015]按位或 min-max容斥 + 高維前綴和

luoguP3175 [HAOI2015]按位或 min-max容斥 + 高維前綴和

def names define 子集 math ble fmt limits tor

技術分享圖片


考慮min-max容斥

\(E[max(S)] = \sum \limits_{T \subset S} min(T)\)

\(min(T)\)是可以被表示出來

即所有與\(T\)有交集的數的概率的和的倒數

通過轉化一下,可以考慮求所有與\(T\)沒有交集的數的概率和

即求\(T\)的補集的子集的概率和

用FMT隨意做下吧...

註意:概率為1的時候需要特判

復雜度\(O(2^n * n)\)


#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define de double
#define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --)
    
const int sid = (1 << 20) + 25;

int n, show;
de Max, sub[sid];

int main() {
    scanf("%d", &n);
    rep(i, 0, (1 << n) - 1) {
        scanf("%lf", &sub[i]);
        show |= i * (sub[i] > 1e-8);
    }
    if(show != (1 << n) - 1) { puts("INF"); return 0; }
    
    rep(i, 1, n) rep(S, 0, (1 << n) - 1)
        if(!(S & (1 << i - 1))) 
            sub[S ^ (1 << i - 1)] += sub[S];
    
    int T = (1 << n) - 1;
    rep(S, 1, (1 << n) - 1) { // no 0
        if(__builtin_popcount(S) & 1) Max += 1.0 / (1.0 - sub[T ^ S]);
        else Max -= 1.0 / (1.0 - sub[T ^ S]);
    }
    printf("%.12lf\n", Max);
    return 0;
}

luoguP3175 [HAOI2015]按位或 min-max容斥 + 高維前綴和