hdu 4336 Card Collector

概率+状态dp

#include <CSTDIO>
#include <STRING.H>
#include <MATH.H>
// hdu 4336
/*
*/
const int MAXN = 21;
int n;
double dp[1<<MAXN], p[MAXN];
int main()   
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
	int i, j;
	double pz;	// 一张都没有的概率
	while (scanf("%d", &n) == 1)
	{
		for (i = 0, pz = 0.0; i < n; ++i)
			scanf("%lf", &p[i]), pz += p[i];
		pz = 1 - pz;
		dp[(1<<n)-1] = 0.0; // 卡片全满
		int t = 1<<n;
		double a, b;
		for (i = t - 2 ; i >= 0; --i)
		{
			a = 1.0;
			b = pz;
			for (j = 0; j<n; ++j)
			{
				if ( i & (1<<j))
					b += p[j];
				else
					a += p[j] * dp[i | (1<<j)];
			}
			b = 1 - b;
			dp[i] = a/b;
		}
		printf("%.6lf\n", dp[0]);
	}
	return 0;
}


你可能感兴趣的:(hdu 4336 Card Collector)