LightOJ1265---Island of Survival (概率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

Output for Sample Input

4

0 0

1 7

2 0

0 10

Case 1: 1

Case 2: 0

Case 3: 0.3333333333

Case 4: 1

每天都会有2种动物相遇

dp[i][j]表示有i只tiger,j只deer时,可以逃脱的概率

当然i=0的话,概率就是=1,这样就可以递推求解了

具体的方程自己想一下很容易就可以写出来

/*************************************************************************
    > File Name: h.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年05月04日 星期一 14时34分12秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

double dp[1010][1010];

int main() {
    int tt;
    scanf("%d", &tt);
    int icase = 1;
    while (tt--) {
        int t, d;
        scanf("%d%d", &t, &d);
        for (int i = 0; i <= t; ++i) {
            for (int j = 0; j <= d; ++j) {
                dp[i][j] = 0.0;
            }
        }
        for (int i = 0; i <= d; ++i) {
            dp[0][i] = 1.0;
        }
        for (int i = 1; i <= t; ++i) {
            for (int j = 0; j <= d; ++j) {
                double r1 = 0, r2 = 0;
                if (i >= 2) {
                    r1 += dp[i - 2][j] * (i * (i - 1)) * 1.0 / ((i + j + 1) * (i + j));
                }
                if (j >= 1) {
                    r1 += dp[i][j - 1] * (i * j * 2.0 / ((i + j + 1) * (i + j)));
                }
                double p = 0;
                if (j >= 2) {
                    p = (j * (j - 1) * 1.0) / ((i + j + 1) * (i + j));
                }
//              r1 /= (1 - p - j * 1.0 / ((i + j + 1) * (i + j)));
                if (j >= 1) {
                    r2 = r1;
                    r2 += j * 2.0 / ((i + j + 1) * (i + j)) * dp[i][j - 1];
                    r2 /= (1 - p);
                }
                r1 /= (1 - p - j * 2.0 / ((i + j + 1) * (i + j)));
                dp[i][j] = max(r1, r2);
            }
        }
        printf("Case %d: %.12f\n", icase++, dp[t][d]);
    }
    return 0;
}
时间: 2024-10-09 17:38:23

LightOJ1265---Island of Survival (概率dp)的相关文章

LightOJ 1265 Island of Survival 概率DP

H - Island of Survival Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1265 Description You are in a reality show, and the show is way too real that they threw into an island. Only two kinds o

LightOJ 1065 Island of Survival (概率DP?)

题意:有 t 只老虎,d只鹿,还有一个人,每天都要有两个生物碰面,1.老虎和老虎碰面,两只老虎就会同归于尽 2.老虎和人碰面或者和鹿碰面,老虎都会吃掉对方 3.人和鹿碰面,人可以选择杀或者不杀该鹿4.鹿和鹿碰面,没事问人存活下来的概率 析:最后存活肯定是老虎没了,首先可以用概率dp来解决,dp[i][j] 表示 还剩下 i 考虑, j 只鹿存活的概率是多少. 然后每次分析这几种情况即可. 还有一种思路就是只要考虑老虎没了,只要老虎没了就能存活,只要计算老虎全死完的概率就好,首先如果老虎是奇数,是

Island of Survival 概率

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; int t,n,d; int main() { scanf("%d",&t); int cas=1; while(t--) { scanf("%d%d",&n,&d); printf("Ca

light oj 1265 - Island of Survival(概率dp)

1265 - Island of Survival PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB 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 t

LightOJ 1268 Island of Survival 数学神解/不用写那些guapi概率dp

谢谢Wen_kr的翻译 题目大意: 你困在一个岛上,上面有老虎和鹿,分别有 只与 只. 如果有一天,你和老虎相遇,那么你死,你和鹿相遇,那么你可以选择杀死或者不杀死这头鹿,老虎和鹿相遇,那么鹿死,鹿和鹿相遇,什么也不会发生,老虎与老虎相遇,那么它们会杀死对方. 每天仅能有一对动物相遇,问你不死并且所有老虎死掉的最大概率. 第一种解法:概率dp 设dp[i][j]为i头老虎j头鹿满足条件的概率...... 这个解法不重要,重要的是下一个. 第二种解法:数学贪心 尽量让老虎自相残杀 即当前老虎为t,

Codeforces Div.301D Bad Luck Island(概率dp+记忆化搜索)

一道概率dp问题. 题目链接:http://codeforces.com/contest/540/problem/D 题目大意:一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j][k]来存储i个石头,j个剪刀,k个布时,某物种的存活概率,共dp三次,算出三个物种分别的概率. 首先,我们需要把对应想求的物种概率初始化,这里以石头为例,那么对于i从1到r,不难理解dp[i][0][0]=

CodeForces 540D Bad Luck Island 概率dp

CodeForces 540D 应该是简单概率dp,由于写得少显得十分蠢萌 求期望逆推,求概率正推,大概是这么个意思,贴一发留恋 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define db double const int maxn=108; db dp[maxn][maxn][maxn]; int main() { int i,j,n,m,k,p; whi

Codeforces 28C [概率DP]

/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队伍的期望. 思路: 概率dp dp[i][j][k]代表前i个浴室有j个人最长队伍是k的概率. 枚举第i个浴室的人数.然后转移的时候其实是一个二项分布. */ #include<bits/stdc++.h> using namespace std; int jilu[55]; double dp[

hdu 3076 ssworld VS DDD (概率dp)

///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次p0*p1 三次 p0^2*p1,即A赢的概率为p1+p*p1+p^2*p1+...p^n*p1,n->无穷 ///即a_win=p1/(1-p);b_win=p2/(1-p); ///dp[i][j]表示a赢了j次,b赢了i次的概率 ///dp[i][j]=dp[i-1][j]*b_win+dp[