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;
}

猜你喜欢

转载自blog.csdn.net/qq_38295645/article/details/80467925