Description
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
题意:你在岛上,岛上还有tiger,deer,每天会相遇一次,问你能存活下来的概率。
注意到:deer对你是否可以存活下来没有影响,不用管它
令dp[i]剩余i只tiger时,你存活下来的概率
则dp[0]=1.0;
若有奇数只tiger,你最后存活下来的概率为0
若有偶数只tiger,则dp[i]=(i-1)/(i+1)*dp[i-2];
先算出所有的dp[i]即可。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 4 const int MAXN=1010; 5 double dp[MAXN]; 6 7 int main() 8 { 9 dp[0]=1; 10 for(double i=1;i<1010;i++) 11 { 12 if(((int)i)%2) 13 dp[(int)i]=0.0; 14 dp[(int)i]=(i-1)/(i+1)*dp[(int)i-2]; 15 } 16 int test; 17 scanf("%d",&test); 18 int cas=1; 19 while(test--) 20 { 21 printf("Case %d: ",cas++); 22 int t,d; 23 scanf("%d%d",&t,&d); 24 printf("%.8f ",dp[t]); 25 } 26 return 0; 27 }