LightOJ - 1265 概率dp

You are in a reality show, and the show is way too real that they threw into an island. Only two kinds of animals are in the island, the tigers and the deer. Though unfortunate but the truth is that, each day exactly two animals meet each other. So, the outcomes are one of the following

a)      If you and a tiger meet, the tiger will surely kill you.

b)      If a tiger and a deer meet, the tiger will eat the deer.

c)      If two deer meet, nothing happens.

d)      If you meet a deer, you may or may not kill the deer (depends on you).

e)      If two tigers meet, they will fight each other till death. So, both will be killed.

If in some day you are sure that you will not be killed, you leave the island immediately and thus win the reality show. And you can assume that two animals in each day are chosen uniformly at random from the set of living creatures in the island (including you).

Now you want to find the expected probability of you winning the game. Since in outcome (d), you can make your own decision, you want to maximize the probability.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing two integers t (0 ≤ t ≤ 1000) and d (0 ≤ d ≤ 1000) where t denotes the number of tigers and d denotes the number of deer.

Output

For each case, print the case number and the expected probability. Errors less than 10-6 will be ignored.

Sample Input

4

0 0

1 7

2 0

0 10

Sample Output

Case 1: 1

Case 2: 0

Case 3: 0.3333333333

Case 4: 1

大概题意是:岛内有n只老虎和m只鹿,每天都有两只动物呆在一起,问最后跑出岛的概率是多少。

(1)你和老虎,老虎吃掉你

(2)老虎和鹿,老虎吃掉鹿

(3)两只鹿,两只都是安全

(4)你和鹿呆,你可以选择杀或者不杀鹿

(5)两只老虎,两只都会死掉

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

const int maxn = 1005;
double  dp[maxn][maxn]; // i老虎,j只鹿逃跑的概率 
/*
dp[i][j]:
 
人和鹿呆在一起,发生的概率为C(1,j) / C(2,1 + i + j) * (dp[i][j] + dp[i][j - 1]) / 2
两只鹿呆在一起,发生的概率为C(2,J) / C(2,1 + i + j) * dp[i][j]
虎和鹿呆在一起,发生的概率为i * j / C(2,i + j + 1) * dp[i][j - 1] 鹿去掉 1
两只虎呆在一起,发生的概率为C(2,i) / C(2,1 + i + j) * dp[i - 2][j] 老虎去掉 2 

*/ 

int c(int n)
{
	return n*(n-1)/2;
}
int main()
{
	int t;
	cin >> t;
	for(int cas = 1; cas <= t; cas++)
	{
		int n, m;
		cin >> n >> m;
		memset(dp, 0, sizeof dp);
		for(int i = 0; i <= m; i++)
			dp[0][i] = 1; // 只有鹿的逃跑的概率为1 
		for(int i = 0; i <= n; i++)
			for(int j = 0; j <= m; j++)
			{
				if(i >= 2)
				{
					double s = (2.0 * i * j + j)/ c(i + j + 1);
					dp[i][j] = (s*dp[i][j - 1] + 2.0*c(i)/c(i + 1 + j)*dp[i - 2][j])/(2 - 1.0*(j + 2 * c(j))/c(i + j + 1)); 
				}		 
			}
		printf("Case %d: %.7f\n", cas, dp[n][m]);
	 } 
	 return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值