A - Marbles
来源:互联网 发布:室内设计书籍 知乎 编辑:程序博客网 时间:2024/05/20 16:43
这题很好,不过我真的是想了很久,在别人的指点下,先用递归试了可以,但是太慢,再改成dp,交上去time limit ,题目所给的T<=10000,所以对dp数组预处理,交上去一下子过了,0.016,代码比我刚开始想用数学方法排列组合写简短多了,那个数学方法有漏洞,这题的状态是 r 个 红球 b 个蓝球下的概率
dp[2][5] = 2/7 * dp[1][4] + 5/7 * dp[2][3]
dp[1][4] = 1/5 * dp[0][3] + 4/5 * dp[1][2]
dp[2][3] = 2/5 * dp[1][2] +( 3/5 * dp[2][1]) i>j 为0 以此类推
dp[i][0]=0 dp[0][i]=1;
Description
Your friend Jim has challenged you to a game. He has a bag containing red and blue marbles. There will be an odd number of marbles in the bag, and you go first. On your turn, you reach into the bag and remove a random marble from the bag; each marble may be selected with equal probability. After your turn is over, Jim will reach into the bag and remove a blue marble; if there is no blue marble for Jim to remove, then he wins. If the final marble removed from the bag is blue (by you or Jim), you will win. Otherwise, Jim wins.
Given the number of red and blue marbles in the bag, determine the probability that you win the game.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case begins with two integers R and B denoting the number of red and blue marbles respectively. You can assume that 0 ≤ R, B ≤ 500 and R+B is odd.
Output
For each case of input you have to print the case number and your winning probability. Errors less than 10-6 will be ignored.
Sample Input
5
1 2
2 3
2 5
11 6
4 11
Sample Output
Case 1: 0.3333333333
Case 2: 0.13333333
Case 3: 0.2285714286
Case 4: 0
Case 5: 0.1218337218
#include<stdio.h>#include<algorithm>using namespace std;double dp[550][550];/*double f(int x,int y){if(x==0) return 1;if(x>y) return 0;return 1.0*x/(x+y)*f(x-1,y-1)+1.0*y/(x+y)*f(x,y-2);}*/int main(){int r,t,b,ca=1;scanf("%d",&t);for(int i=1;i<=500;i++){dp[0][i]=1;dp[i][0]=0; }for(int i=1;i<=500;i++){for(int j=2;j<=500;j++) if(i<j){dp[i][j]=1.0*i/(i+j)*dp[i-1][j-1]+1.0*j/(i+j)*dp[i][j-2];}} while(t--){scanf("%d%d",&r,&b); printf("Case %d: %lf\n",ca++,dp[r][b]); }}
- A - Marbles
- 3019. A Game with Marbles
- poj1909 Marbles on a tree
- Marbles
- hdu 2574 A Game with Marbles
- UVa10672 - Marbles on a tree(BFS)
- ZOJ 2374 Marbles on a tree
- POJ 1909 Marbles on a tree 笔记
- hdu2374 A Game with Marbles(简单数学题)
- marbles-ACM
- 110707 Marbles
- SPOJ MARBLES
- pku 1909 Marbles on a tree(我都不知道这能不能算DP)
- 内部比赛 G A Game with Marbles
- uva 10090 marbles
- SPOJ MARBLES(简单组合)
- SPOJ 78 Marbles
- Jars and marbles 问题
- 了解clone
- 输入法框架
- HTML5学习3
- 序列化和拷贝
- CVS 服务器端的配置
- A - Marbles
- Hibernate 配置详解(10)
- .Net MVC4 被坑心得(七)filter 身份验证
- solr 4.4 Error filterStart 问题
- SharePoint 2013 显示用户Lync是否在线(集成Lync)
- 对比iOS网络类库:AFNetworking VS ASIHTTPRequest
- 你应该更新的Java知识之常用程序库(一)
- 低预算选购中高配车 海马M3 1.5L一个不错的选择
- MySQL 触发器简单实例