【搜索】【2012 Dhaka Regional】E - Poker End Games

【题目描述】 
Poker End Games
Input: Standard Input
Output: Standard Output
 
 
Alice and Bob loves playing poker with their friends. Unfortunately, they play poker way better than
their friends. So, almost always they are the last two players to play. Two of them can play for a long
time and it bores their friends. So they changed the rule little bit and decided that both of them will go
all in every round. Now, Alice is wondering what is the expected length of the game, and what is the
probability that she will win the game.
 
Let’s say at the beginning of round i, Alice has ai Taka (Currency of Bangladesh) and Bob has bi
Taka. and ci is the minimum of ai and bi. Alice and Bob are equally likely to win the round. If Alice
wins, she gets ci Taka from Bob, otherwise Bob gets ci Taka from her. Game ends when one of them
has 0 (Zero) Taka and obviously the person with 0 taka loses.
 
Given that the initial amount Alice has is a0 and the initial amount that Bob has is b0, you have to find
the probability that Alice is going to win and expected number of rounds the game is played. 
  
Input
Input file starts with a number T (0 < T ≤ 100). T test cases follow. The input for each test case is
contained in a single line and it consists two  integers a and b (0 < a, b ≤ 100). 
 
Output
For each case, print the case number followed by the expected number of rounds and the probability
that Alice will win. Print both result rounded to 6 digits after the decimal point. For both these values
errors less than 10-5 will be ignored.
 
Sample Input                                Output for Sample Input
2
1 1
2 1
 
Case 1: 1.000000 0.500000
Case 2: 2.000000 0.666667
 
Problemsetter: Tanaeem M. Moosa, Special Thanks: Md. Mahbubul Hasan 

 

【个人体会】这个题目完全地暴露了自己在比赛时候的问题。首先是题目意思没有理解透就开始YY,结果打了很久程序才发现样例都模拟不出来,还问了别人。然后是一开始做的时候没有仔细考虑清楚,预先算出了从初始开始一直胜直到结束需要几回合,一直败直到结束需要几回合,然后就把这2个数字当做搜索的层数限制。显然,这是很傻X的,因为并不是赢1局然后输1局就会回到初始情况的。。。总之,要反思一下。

 

【题目解析】 本题实际要求的是2个问,第一个问要求的是回合数的数学期望,第二个问要求的是胜利的概率。一开始纠结样例2的0.666667是怎么算出来,后来经人提示才猛然发现,求这个概率是一个无限的过程,应该一直模拟这个过程,直到所求的概率已经超出题目要求的精度范围。至于回合数的数学期望,不管是胜利还是失败都要算上。

 

【代码如下】

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <iomanip>
 4 
 5 #define rep(i, x) for (int i = 1; i <= x; i ++)
 6 
 7 #define FILE_IO
 8 
 9 using namespace std;
10 
11 double List[31], Ansr, Ans;
12 int T;
13 
14 void Solve(int a, int b, int sum)
15 {
16     if (sum > 30) return ;
17     if (a == 0 || b == 0)
18     {
19         Ansr += List[sum] * sum;
20         if (b == 0) Ans += List[sum];
21         return ;
22     }
23     int c = min(a, b);
24     Solve(a + c, b - c, sum + 1);
25     Solve(a - c, b + c, sum + 1);
26 }
27 
28 void Prework()
29 {
30     List[0] = 1; rep(i, 30) List[i] = List[i - 1] * 0.5;
31 }
32 
33 int main()
34 {
35     #ifdef FILE_IO
36     //freopen("test.in", "r", stdin);
37     #endif // FILE_IO
38     Prework();
39     cin >> T;
40     rep(i, T)
41     {
42         int a, b; cin >> a >> b;
43         Solve(a, b, 0);
44         cout << "Case " << i << ": ";
45         cout << setprecision(6) << setiosflags(ios :: fixed) << Ansr << " " << Ans << endl;
46         Ansr = Ans = 0;
47     }
48     return 0;
49 }

 

 

 

 

 

posted on 2012-12-27 15:16  孤星ぁ紫辰  阅读(200)  评论(1编辑  收藏  举报

导航