zoukankan      html  css  js  c++  java
  • HDU-4336 Card Collector

    题目链接

    Translate:

    (n) 种卡片,一包零食里会有一张卡片,其中是卡片 (i) 的概率是 (p_i)

    求集齐全部卡片的期望购买零食数。

    (1leq Nleq 20,sum pleq 1)

    Solution:

    (f_{S}) 为从已有的状态为 (S) 时开始,集齐全部全部卡片的期望购买零食数,显然有:

    前半部分是抽中原先没有的,后半部分是抽中已经有的或者没抽中,最后 (+1) 是因为购买了一包零食。

    [f_S=sum_{i otin S}f_{Scup{i}}p_i+(1-sum_{i otin S}p_i)f_S+1 \ (sum_{i otin S}p_i)f_S = (sum_{i otin S}f_{Scup{i}}p_i)+1 ]

    状压dp即可。

    参考代码

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    namespace do_while_true {
    	#define ld double
    	#define ll long long
    	#define re register
    	#define pb push_back
    	#define fir first
    	#define sec second
    	#define pp std::pair
    	#define mp std::make_pair
    	const ll mod = 998244353;
    	template <typename T>
    	inline T Max(T x, T y) { return x > y ? x : y; }
    	template <typename T>
    	inline T Min(T x, T y) { return x < y ? x : y; }
    	template <typename T>
    	inline T Abs(T x) {	return x < 0 ? -x : x; }
    	template <typename T>
    	inline T& read(T& r) {
    		r = 0; bool w = 0; char ch = getchar();
    		while(ch < '0' || ch > '9') w = ch == '-' ? 1 : 0, ch = getchar();
    		while(ch >= '0' && ch <= '9') r = r * 10 + (ch ^ 48), ch = getchar();
    		return r = w ? -r : r;
    	}
    	template <typename T>
    	inline T qpow(T x, T y) {
    		re T sumq = 1; x %= mod;
    		while(y) {
    			if(y&1) sumq = sumq * x % mod;
    			x = x * x % mod;
    			y >>= 1;
    		}
    		return sumq;
    	}
    }
    using namespace do_while_true;
    
    const int N = 21;
    
    int n;
    
    ld p[N], f[2100000];
    
    void solve() {
    	for(int i = 1; i <= n; ++i) scanf("%lf", &p[i]);
    	f[(1 << n)-1] = 0;
    	for(int i = (1 << n)-2; ~i; --i) {
    		ld s1 = 0, s2 = 0;
    		for(int j = 1; j <= n; ++j)
    			if(((1 << (j-1)) & i) == 0)
    				s1 += p[j], s2 += f[i | (1 << (j-1))] * p[j];
    		f[i] = (s2 + 1) / s1;
    	}
    	printf("%.4lf
    ", f[0]);
    }
    
    signed main() {
    	#ifndef ONLINE_JUDGE
    	freopen("in.txt", "r", stdin);
    	#endif
    //	int T = 1;
    //	read(T);
    	while(~scanf("%d", &n)) solve();
    	fclose(stdin);
    	return 0;
    }
    
  • 相关阅读:
    移动端rem切图
    在Vue中如何使用axios跨域访问数据
    如何使地图和柱状图联动
    dedecms 5.7 采集目标文章的发布时间 采集后变成当前本地时间
    PHP的内存限制 Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
    Web前端性能优化——如何提高页面加载速度
    jquery中链式调用原理
    jdk1.6以后 对synchronized锁做了哪些优化
    Synchronized的锁优化
    分布式缓存
  • 原文地址:https://www.cnblogs.com/do-while-true/p/14359416.html
Copyright © 2011-2022 走看看