codeforces 148 D Bag of mice(概率dp)
題目大意:原來袋子裡有w只白鼠和b只黑鼠龍和王妃輪流從袋子裡抓老鼠。誰先抓到白色老師誰就贏。王妃每次抓一隻老鼠,龍每次抓完一隻老鼠之後會有一隻老鼠跑出來。每次抓老鼠和跑出來的老鼠都是隨機的。如果兩個人都沒有抓到白色老鼠則龍贏。王妃先抓。問王妃贏的概率。
解題思路:dp[i][j]表示現在輪到王妃抓時有i只白鼠,j只黑鼠,王妃贏的概率
王妃要想贏必須:第i次的時候王妃抓到白鼠,否則這個龍要抓到黑鼠王妃才有可能這輪不輸,而將會跑出去的老鼠有可能是白的,也有可能是黑色的。所以就產生了不同的狀態。記憶化搜一下。
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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
//#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 1010;
using namespace std;
double dp[maxn][maxn];
double dfs(int x, int y)
{
if(dp[x][y] > 0)
return dp[x][y];
if(x < 0 || y < 0)
return 0;
if(x == 0)
return 0;
if(y == 0)
return 1;
double a = x*1.0;
double b = y*1.0;
double c = a+b;
dp[x][y] = a/c;
if(x+y >= 3)
{
dp[x][y] += (dfs(x-1, y-2)*(b/(c))*((b-1)/(c-1))*(a/(c-2)) + dfs(x, y-3)*(b/(c))*((b-1)/(c-1))*((b-2)/(c-2)));
}
return dp[x][y];
}
int main()
{
int w, b;
memset(dp, 0, sizeof(dp));
while(cin >>w>>b)
{
printf("%.10lf\n",dfs(w,b));
}
return 0;
}
相關文章
- Codeforces 148D Bag of mice (概率dp)
- Codeforces 148D Bag of mice:概率dp 記憶化搜尋
- codeforces 148 D 概率dp
- CodeForces - 628D (數位dp)
- Codeforces 432D Prefixes and Suffixes:KMP + dpKMP
- 【dp】codeforces 837D Round Subset
- CodeForces 401D 數位DP
- 【數位dp】Beautiful numbers CodeForces - 55D
- Codeforces Round #358 (Div. 2) D dp
- Codeforces 351B Jeff and Furik:概率 + 逆序對【結論題 or dp】
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces 478D Red-Green Towers:dp
- 概率DP入門題
- Codeforces 626D Jerry's Protest(暴力列舉+概率)
- Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】
- Codeforces Round #336 (Div. 2) D 區間dp
- 概率DP總結 by kuangbin
- HDU 3853 LOOPS(概率dp)OOP
- Codeforces 219D Choosing Capital for Treeland:Tree dpAPI
- Codeforces 11D A Simple Task 題解 [ 藍 ] [ 狀壓 dp ]
- Codeforces 235B Let's Play Osu! (概率dp求期望+公式變形)公式
- SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式公式
- Codeforces 463D Gargari and Permutations:隱式圖dp【多串LCS】3D
- Educational Codeforces Round 167 (Rated for Div. 2) D(dp,貪心)
- POJ 3744 概率dp+矩陣矩陣
- Codeforces 358D Dima and Hares:dp【只考慮相鄰元素】
- codeforces455A Boredom (裸DP)
- Codeforces 148E Porcelain (預處理+多重揹包)AI
- Bag FilterFilter
- Codeforces 372B Counting Rectangles is Fun:dp套dp
- Codeforces 180C Letter:dp
- 【演算法學習筆記】概率與期望DP演算法筆記
- Codeforces 158E Phone Talks:dp
- Codeforces 294B Shaass and Bookshelf:dp
- 【dp】CodeForces - 623B Array GCDGC
- 【dp】codeforces 830-A Office Keys
- HDU 4326Game(比較難理解的概率dp)GAM
- lightoj 1030 Discovering Gold (基礎概率dp)Go