CF 148D. Bag of mice (概率DP)
来源:互联网 发布:云计算就是虚拟化吗 编辑:程序博客网 时间:2024/06/11 10:43
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance.
They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning?
If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one.
The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000).
Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed10 - 9.
1 3
0.500000000
5 5
0.658730159
Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
采用了搜索记忆化的思想,每次的问题向下归结为较小的子问题求解。
注意一下精度问题,如果精度已经达到要求,就不用再向下进行求解了。
dp[w][b]=p1+p2*tmp;
p1=w/(w+b) //公主直接赢的概率
p2=b/(w+b), b--; p*=(b/(w+b)); b--; 经过一轮,两个选手均未摸到白老鼠
tmp=dfs(w-1,b)*(w/(w+b))+dfs(w,b-1)*(b/(w+b)); //向下深搜乘以相应的概率,某种颜色老鼠吓跑的概率。。
#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>using namespace std;#define N 1005#define LL __int64const int inf=0x1f1f1f1f;const double eps=1e-10;double f[N][N];double dfs(int w,int b) //Princess赢的概率{ if(w<=0) return 0; if(b<=0) return 1; if(fabs(f[w][b]+1.0)>eps) return f[w][b]; double p1,p2,tmp=1; p1=w*1.0/(w+b); //直接赢 p2=b*1.0/(w+b); //还没输 b--; p2=p2*(b*1.0/(w+b)); //dragon 也没赢 b--; if(p2>eps) //精度不够则继续向下深搜!! tmp=(w*1.0/(w+b))*dfs(w-1,b)+(b*1.0/(w+b))*dfs(w,b-1); //printf("%.9f %.9f %.9f\n",p1,p2,tmp); return f[w][b+2]=p1+p2*tmp;}int main(){ int i,w,b; memset(f,-1,sizeof(f)); while(scanf("%d%d",&w,&b)!=-1) { printf("%.9f\n",dfs(w,b)); } return 0;}
非递归形式的:
#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>using namespace std;#define N 1005#define LL __int64const int inf=0x1f1f1f1f;const double eps=1e-10;double dp[N][N]; //dp[w][b] 代表当前情况下Princess赢的概率void inti(){ memset(dp,-1,sizeof(dp)); dp[0][0]=0; for(int i=1;i<N;i++) { dp[i][0]=1; dp[0][i]=0; }}int main(){ int i,j,w,b; double tmp; inti(); while(scanf("%d%d",&w,&b)!=-1) { if(fabs(dp[w][b]+1.0)>eps) { printf("%.9f\n",dp[w][b]); continue; } for(i=1;i<=w;i++) { for(j=1;j<=b;j++) { dp[i][j]=i*1.0/(i+j); tmp=j*1.0/(i+j)*(j-1)/(i+j-1); if(j>=2&&i+j>3) dp[i][j]+=tmp*dp[i-1][j-2]*i/(i+j-2); if(j>=3&&i+j>3) dp[i][j]+=tmp*dp[i][j-3]*(j-2)/(i+j-2); } } printf("%.9f\n",dp[w][b]); } return 0;}
- CF D. Bag of mice(概率dp)
- CF 148D Bag of mice (概率DP)
- [CF 148D]Bag of mice[概率DP]
- CF 148D Bag of mice 概率DP
- CF 148D. Bag of mice (概率DP)
- CF 148D. Bag of mice[概率dp]
- 【概率DP】 CF 148D Bag of mice
- CF 148D Bag of mice 概率DP
- CF 148D Bag of mice 概率dp 简单题
- [概率dp] cf 148D Bag of mice
- (CF 148D Bag of mice)<简单概率DP>
- Codeforces 148D D Bag of mice(概率dp)
- codeforces 148D D. Bag of mice(概率dp)
- 【CF148D】【D. Bag of mice】【概率dp】
- codeforces 148D Bag of mice DP 概率
- 148D Bag of mice - 简单概率dp
- Codeforces 148D - Bag of mice 概率dp
- CodeForces 148D Bag of mice —— 概率DP
- 交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别
- 利用Android源代码编译一个自己写的Hello World模块
- linearLayout 和 relativeLayout的区别
- 安卓 有关动态加载ImageView,Radiobutton
- 信息安全意识宣贯免费啦!
- CF 148D. Bag of mice (概率DP)
- postfix之安全转发认证服务
- 随笔心事
- macports 安装
- Request的getParameter和getAttribute的区别
- 业内首发CISM国际注册信息安全经理中文书
- Java 实现选择排序
- cocos2dx wuhan
- 黑马程序员---类的本质