bzoj4036 / P3175 [HAOI2015] bitwise OR

bzoj4036 / P3175 [HAOI2015] bitwise OR

Is a min-max容斥board problem.

min-max容斥 formula:

$ \displaystyle max(S) = \sum_{T\sube S} (-1)^{|T|+1} min(T) $

And very good, it was established in the undesirable situation!

This has to do with it. .

If each considered separately, if the first $ i $ 1 bits become the desired time is $ T (i) $

Then the seeking is $ E (max (T_ {1 \ dots n})) $

this is okay min-max容斥

Seeking $ min $ is a subset of which allow one to become 1a desired number of times.

Consider a choice that allows one to become this subset of 1probability, is 1-- probability of this subset of all the bits are 0 and numbers, can be considered in addition to make $ S $ is a subset of the bit is zero all other is a number (set) of 1, the probability is $ 1 - \ sum_ {a [ i] \ sube S} p_i $ is equivalent to each selection, it is desirable that $ \ frac {1} {p } $

Then minmax inclusion-exclusion formula kinds $ | T | $ $ S $ is actually a number of zeroes, isn - popcount

This calculation is actually half or convolution

Complexity $ (n2 ^ n) O $

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
#define MAXN ( 1 << 21 ) + 6
int n;
double p[MAXN];

inline void FWT(double a[], int len) {
    for (int mid = 2; mid <= len; mid <<= 1) 
        for (int i = 0; i < len; i += mid)
            for (int j = i; j < i + (mid >> 1); j++) 
                a[j + (mid >> 1)] += a[j];
}

int main() {
    cin >> n;
    for( int i = 0 ; i < ( 1 << n ) ; ++ i ) scanf("%lf",&p[i]);
    FWT( p , ( 1 << n ) );
    double ans = 0.0;
    for( int i = 0 ; i < ( 1 << n ) - 1; ++ i ) {
        ans += ( ( n - __builtin_popcount( i ) & 1 ) ? 1.0 : -1.0 ) / ( 1.0 - p[i] );
    }
    if( ans > 1e50 ) puts("INF");
    else printf("%.7lf",ans);
}

Guess you like

Origin www.cnblogs.com/yijan/p/bzoj4036.html