HDU 4336 Card Collector [概率DP]
2015-11-30 21:14
381 查看
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,是因为移项的结果
代码:
实现使用了记忆化搜索
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; }
相关文章推荐
- ActivityManager 获取系统进程信息
- IOS开发之UIview
- SHELL
- Android获取手机存储空间大小
- junit单元测试
- 黑马程序员--模拟登录程序
- 欢迎使用CSDN-markdown编辑器
- IP数据报、TCP报文、UDP报文格式
- A 类,B 类 ,C类网络地址的最大网络数,以及网段内最大主机数
- Android中通过代码获取arrays.xml文件中的数据
- A 类,B 类 ,C类网络地址的最大网络数,以及网段内最大主机数
- ThinkPHP框架四 逻辑运算和链接数据库
- python Argparse模块的使用
- spring事务回滚
- HBase性能调优(转)
- Python 更改默认版本
- PopupWindow和AlertDialog的区别
- http://stackexchange.com/
- 链表的头插入法建立和尾插入法建立
- java环境配置