HDU 4336 Card Collector [概率DP]

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.

题意:

每天你吃一包零食,零食中可能会出现一张收集卡片,卡片共N张,第I张出现的概率为Pi,ΣPi<=1,保证一包零食中最多只有一张卡片(可能没有),问收集完所有卡片的期望天数。

范围:

N<=20

解法:

N为20,很容易发现需要状压,然后进行概率DP求解。

设DP[state]表示当前已收集状态为state的卡片,收集剩下的卡片的期望天数为DP[state],显然,边界为DP[(1<<n)-1]=0,目标为DP[0]

枚举当天打开的卡片为K,如果K是已经得到过的,记这部分的概率和为P0

转移方程: DP[state] = ( Σ(DP[ state | (1<<x) ] * Px) +1 )/ (1-P0) 这里的x表示当前状态没有得到过的卡片标号,之所以除以1-P0,是因为移项的结果

代码:

实现使用了记忆化搜索

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;


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

int n,tot;
double p[111];
double dp[1080000];
bool vis[1080000];
double dfs(int state){
    if(state==tot)return 0;
    if(vis[state])return dp[state];
    vis[state]=1;
    double rp=1.0;
    dp[state]=1.0;
    rep(i,0,n-1){
        if((state&(1<<i))==0){
            rp-=p[i];
            dp[state]+=p[i]*dfs(state|(1<<i));
        }
    }
    dp[state]/=(1.0-rp);
    return dp[state];
}
int main(){
    while(scanf("%d",&n)!=EOF){
        rep(i,0,n-1)scanff(p[i]);
        tot=(1<<n)-1;
        rep(i,0,tot)vis[i]=0;
        double ans=dfs(0);
        printf("%.8lf\n",ans);
    }
    return 0;
}









  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值