zoukankan      html  css  js  c++  java
  • hdu4336 Card Collector MinMax 容斥

    题目传送门

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

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

    题解

    minmax 容斥模板题。

    一个集合 (S) 的至少有一个邮票出现的最早时间是 (frac 1{sumlimits_{iin 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
    ", 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;
    }
    
  • 相关阅读:
    window.open全屏显示
    js关闭当前页面
    sublime text 3 3126注册码
    关闭tomcat8080端口
    给json数组添加新字段并赋值
    怎么把json数据alert
    js里url里有特殊字符(如&)情况,后台request.getParameter("url")里&变成&
    request.getParameter乱码
    javaMail 详解
    JavaMail使用SMTP协议发送电子邮件(详解)
  • 原文地址:https://www.cnblogs.com/hankeke/p/hdu4336.html
Copyright © 2011-2022 走看看