codeforces 148D Bag of mice(概率dp)

在一个游戏中,公主和龙通过轮流从袋中抽取白鼠和黑鼠来决定胜负,公主先抽。若一方首先抽到白鼠,则该方获胜。若所有鼠被抽完仍未有人抽到白鼠,则龙获胜。求公主获胜的概率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Bag of mice
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000).

Output

Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed10 - 9.

Examples
input
1 3
output
0.500000000
input
5 5
output
0.658730159
Note

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.



题意:袋子里有w只白鼠,b只黑鼠。公主和龙谁先抓到第一只白鼠谁就获胜,都抓不到白鼠,算龙获胜。 两人轮流抓,每次只能抓一只,公主先抓。龙在抓老鼠的时候,老鼠会因为害怕而每次从袋子中爬出来一只。问公主获胜的机会有多大?


题解:我们可以用dp[i][j]表示在有i只白鼠,j只黑鼠的时候公主获胜的概率。 于是我们能够得到以下几点:


1.当 b=0时, 0<i<=w,dp[i][0]=1;当w=0时,0<=i<=b,dp[0][i]=0 ;

2.公主直接抓到一只白鼠时,王妃赢了,概率为 i/(i+j) ;

3.公主抓到一只黑鼠,龙抓到一只白鼠,公主输了,概率为  j/(i+j)*(i)/(i+j-1) ;由于dp[i][j]表示公主获胜的概率,这种情况就不用考虑进入状态转移,我们接着往下看;

4.公主抓到一只黑鼠,龙抓到一只黑鼠,从袋子中跑了一只黑鼠,概率为 j/(i+j)*(j-1)/(i+j-1)*(j-2)/(i+j-2) ,转态就转移到了dp[i][j-3],当然出现这种情况的限制在于 j>=3 ;

5.公主抓到一只黑鼠,龙抓到一只黑鼠,从袋子中跑出来一只白鼠,概率为 j/(i+j)*(j-1)/(i+j-1)*i/(i+j-2) , 转态就转移到了 dp[i-1][j-2],出现这种情况 必须 j>=2 ;


代码如下:


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1010
double dp[maxn][maxn];
int main()
{
	int w,b;
	int i,j;
	while(scanf("%d%d",&w,&b)!=EOF)
	{
		memset(dp,0,sizeof(dp));
		for(i=1;i<=w;++i)
			dp[i][0]=1;
		for(i=1;i<=w;++i)
		{
			for(j=1;j<=b;++j)
			{
				dp[i][j]+=(double)i/(i+j);
				if(j>=3)//公主抓到黑鼠,龙也抓到黑鼠,从袋子中跑了一只黑鼠 
					dp[i][j]+=(double)j/(i+j)*(j-1)/(i+j-1)*(j-2)/(i+j-2)*dp[i][j-3];
				if(j>=2)//公主抓到黑鼠,龙也抓到黑鼠,从袋子中跑了一只白鼠 
					dp[i][j]+=(double)j/(i+j)*(j-1)/(i+j-1)*i/(i+j-2)*dp[i-1][j-2];
			}
		}
		printf("%.9lf\n",dp[w][b]);
	}
	return 0;
} 



### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值