hdu4336 Card Collector MinMax 容斥

题目传送门

https://vjudge.net/problem/HDU-4336

http://acm.hdu.edu.cn/showproblem.php?pid=4336

题解

minmax 容斥模板题。

一个集合 \(S\) 的至少有一个邮票出现的最早时间是 \(\frac 1{\sum\limits_{i\in S} p_i}\)


时间复杂度 \(O(2^n)\)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

#define lowbit(x) ((x) & -(x))

const int N = 20 + 7;
const int NP = (1 << 20) + 7;

int n, S;
double p[N], f[NP];
int pcnt[NP];

inline void work() {
	S = (1 << n) - 1;
	double ans = 0;
	for (int s = 1; s <= S; ++s) f[s] = f[s ^ lowbit(s)] + p[std::__lg(lowbit(s)) + 1], pcnt[s] = pcnt[s ^ lowbit(s)] + 1;
	for (int s = 1; s <= S; ++s)
		if (pcnt[s] & 1) ans += 1 / f[s];
		else ans -= 1 / f[s];
	printf("%.10lf\n", ans);
}

inline void init() {
	for (int i = 1; i <= n; ++i) scanf("%lf", &p[i]);
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	while (~scanf("%d", &n) && n) {
		init();
		work();
	}
	fclose(stdin), fclose(stdout);
	return 0;
}
posted @ 2019-09-27 19:46  hankeke303  阅读(236)  评论(0编辑  收藏  举报