HDOJ 4336 —— 概率DP

Card Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1948    Accepted Submission(s): 909
Special Judge


Problem Description
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin, you will win an amazing award. 

As a smart boy, you notice that to win the award, you must buy much more snacks than it seems to be. To convince your friends not to waste money any more, you should find the expected number of snacks one should buy to collect a full suit of cards.
 

Input
The first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, ..., pN, (p1 + p2 + ... + pN <= 1), indicating the possibility of each card to appear in a bag of snacks. 

Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.
 

Output
Output one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.

You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.
 

Sample Input
   
   
   
   
1 0.1 2 0.1 0.4
 

Sample Output
   
   
   
   
10.000 10.500
 

Source
2012 Multi-University Training Contest 4
 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:   4337  4331  4332  4333  4334 
 
题意是给你n张卡出现的概率,问你把它们都得到要取的次数的期望。
思路:因为n只有20,我们用状态压缩表示状态,每次抽取有三种结果:抽到未抽过的卡 抽中已抽取的卡 ,没抽到卡。后两种情况对状态没有改变,我们记他们的概率为sum。
dp[S] 表示在S状态下取遍所有卡的次数的期望。
dp[S] = dp[S] * sum + dp[S ^ (1 << x1)] * p(x1) + dp[S ^ (1 << x2)] * p(x2) ... 
逆推一边,dp[0]就是结果。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 40000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-10
const long long MOD = 1000000000 + 7;
const int mod = 10007;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
typedef vector<int> vec;
typedef vector<vec> mat;


#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
double a[25] , dp[1 << 25] , sum , none , same;
int n;
int main()
{
    //ios::sync_with_stdio(false);
    #ifdef Online_Judge
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif // Online_Judge
    while(~scanf("%d" , &n))
    {
        none = 0;
        for(int i = 0 ; i < n ; i ++)
        {
            scanf("%lf" , &a[i]);
            none += a[i];
        }
        none = 1 - none;///没有卡的概率
        dp[(1 << n) - 1] = 0;///全部得到
        for(int i  = (1 << n) - 2 ; i >= 0 ; i--)
        {
            sum = 1;
            same = 0;
            for(int j = 0 ; j < n ; j++)
            {
                if(i & (1 << j))same += a[j];
                else sum += dp[i | (1 << j)] * a[j];
            }
            dp[i] = sum / (1 - same - none);
//            cout << dp[i] << endl;
//            cout << '*' << sum << ' ' << same << endl;
        }
        printf("%.4lf\n" , dp[0]);
    }
    return 0;
}


你可能感兴趣的:(ACM,概率DP)