Problem B. Vote

A and B are the only two candidates competing in a certain election. We know from polls that exactly N voters support A, and exactly M voters support B. We also know that N is greater than M, so A will win.

Voters will show up at the polling place one at a time, in an order chosen uniformly at random from all possible (N + M)! orders. After each voter casts their vote, the polling place worker will update the results and note which candidate (if any) is winning so far. (If the votes are tied, neither candidate is considered to be winning.)

What is the probability that A stays in the lead the entire time -- that is, that A will always be winning after every vote?

Input

The input starts with one line containing one integer T, which is the number of test cases. Each test case consists of one line with two integers N and M: the numbers of voters supporting A and B, respectively.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the probability that A will always be winning after every vote.

y will be considered correct if y is within an absolute or relative error of 10-6 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

1 ≤ T ≤ 100.

Small dataset

0 ≤ M < N ≤ 10.

Large dataset

0 ≤ M < N ≤ 2000.

Sample

Input 
2
2  1
1 0
Output 
Case #1: 0.33333333
Case #2: 1.00000000

In sample case #1, there are 3 voters. Two of them support A -- we will call them A1 and A2 -- and one of them supports B. They can come to vote in six possible orders: A1 A2 B, A2 A1 B, A1 B A2, A2 B A1, B A1 A2, B A2 A1. Only the first two of those orders guarantee that Candidate A is winning after every vote. (For example, if the order is A1 B A2, then Candidate A is winning after the first vote but tied after the second vote.) So the answer is 2/6 = 0.333333...

In sample case #2, there is only 1 voter, and that voter supports A. There is only one possible order of arrival, and A will be winning after the one and only vote.

思路:

dp[i][j]:i个A与j个B的未结束(票数领先)的情况数,则dp[i][j]=dp[i-1][j]+dp[i][j-1] 表示由i-1个A,j个B或者i个A,j-1个B转化而来。dp[0][0]=1表示开始时未结束。

之后得到dp[n][m]为n个A,m个B的情况数,之后乘上n!*m!为总的排列数。

难点:

1.large dataset里n最大为2000,最后的排列数和dp可能大于2000!(3*10^5735),所以需要用对数运算。

2.乘除转化为对数加减,加法可转化为:c=log(e^a+e^b)->c=log(e^a(1+e^(b-a)))=a+log(1+e^(b-a))  这样就不会溢出了  :)

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define pi acos(-1.0)
#define mj
#define inf 0x3f3f3f
#define db double
typedef long long ll;
using namespace std;
const int N=1e5+;
db dp[][];
void init()
{
for(int i=;i<;i++){
for(int j=;j<;j++)
dp[i][j]=-;
}
dp[][]=log(1.0);
for(int i=;i<;i++){
for(int j=;j<;j++){
if(i>j){
if(dp[i-][j]!=-&&dp[i][j-]!=-) dp[i][j]=dp[i-][j]+log(+exp(dp[i][j-]-dp[i-][j]));
else if(dp[i-][j]==-&&dp[i][j-]!=-) dp[i][j]=dp[i][j-];
else if(dp[i-][j]!=-&&dp[i][j-]==-) dp[i][j]=dp[i-][j];
}
}
}
}
int main()
{
#ifdef mj
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // mj
int t,n,m;
scanf("%d",&t);
init();
for(int k=;k<=t;k++){
scanf("%d%d",&n,&m);
db ans=;
for(int i=;i<=m;i++){
ans+=(db)log(i)-(db)log(n+i);
}
ans+=(db)dp[n][m];
ans=exp(ans);
printf("Case #%d: %.8f\n",k,ans);
}
return ;
}

Kickstart Practice Round 2017 Google的更多相关文章

  1. Kickstart Practice Round 2017---A

    Problem The Constitution of a certain country states that the leader is the person with the name con ...

  2. 中国2017 Google 开发者大会第一天简单回顾

    昨天有幸参加了中国2017 Google 开发者大会,在这第一天就收获满满,昨天太忙了,今天早晨来一起简单回顾一下,可以让没有参加的童鞋们感受一下现场的温度. 早早就来到了会议现场,外面看不出什么特别 ...

  3. Google kickstart 2022 Round A题解

    Speed Typing 题意概述 给出两个字符串I和P,问能否通过删除P中若干个字符得到I?如果能的话,需要删除字符的个数是多少? 数据规模 \[1≤|I|,|P|≤10^5 \] 双指针 设置两个 ...

  4. 2017 google Round D APAC Test 题解

    首先说明一下:我只是用暴力过了4道题的小数据,就是简单的枚举,大数据都不会做!下面的题解,是我从网上搜到的解答以及查看排行榜上大神的答案得出来的. 首先贴一下主要的题解来源:http://codefo ...

  5. 2017 google Round C APAC Test 题解

    题解参考网上的答案,以及我自己的想法. 主要参考网站:http://codeforces.com/blog/entry/47181,http://codeforces.com/blog/entry/4 ...

  6. google kickstart 2018 round D A Candies

    思路: 对于small数据,由于求和及奇数数量两个限制条件均满足区间单调性,可以直接使用尺取法(滑动窗口法)求解. 对于large数据,奇数数量依然是满足区间单调性的.首先使用尺取法,找到所有满足奇数 ...

  7. 这是一份很有诚意的2017 Google I/O大会的汇总 & 解析

    前言 在刚过去的凌晨(北京时间 5月18日 1.00-3.00),一年一度的2017年Google I/O大会在美国谷歌山景城海岸线圆形剧场如期举行 Google I/O 大会:Innovation ...

  8. Practice Round China New Grad Test 2014 报告

    今天有Google of Greater China Test for New Grads of 2014的练习赛,主要是为了过几天的校园招聘测试做练习用的,帮助熟悉平台,题目嘛,个人觉得除了A题外, ...

  9. 2017 google IO大会——5.17

    大家好! 每年一度的全球互联网及新型技术的盛会 Google IO,今年的大会日期和地址已经公布了:大会将在5月17至19日在谷歌公司总部边上的会场,美国加州 Mountain View的 Shore ...

随机推荐

  1. Java通过JNI调用dll详细过程(转)

    源:Java通过JNI调用dll详细过程 最近项目有这样一个需求,在已有的CS软件中添加一个链接,将当前登录用户的用户名加密后放在url地址中,在BS的login方法里通过解密判断,如果为合法用户则无 ...

  2. ios 简单的plist文件读写操作(Document和NSUserDefaults)

    最近遇到ios上文件读写操作的有关知识,记录下来,以便以后查阅,同时分享与大家. 一,简单介绍一下常用的plist文件. 全名是:Property List,属性列表文件,它是一种用来存储串行化后的对 ...

  3. Java 之 HTML

    1.HTML a.定义:HTML指的是超文本标记语言 b.特点:HTML不是一种编程语言,而是一种标记语言 标记语言是一套标记标签 HTML使用标记标签来描述网页 c.HTML标签:①通常标签是成对出 ...

  4. sql server mdf碎片级数据库修复,数据库碎片级提取

    sql server mdf碎片级数据库修复,数据库碎片级提取   大家都知道MDF数据库文件一般都比较大,在磁盘中往往被存放到不连续的逻辑簇中,久而久之就形成了文件碎片,当文件被误删除或者格式化后, ...

  5. CART分类与回归树 学习笔记

    CART:Classification and regression tree,分类与回归树.(是二叉树) CART是决策树的一种,主要由特征选择,树的生成和剪枝三部分组成.它主要用来处理分类和回归问 ...

  6. iOS 类库列表

    1. LinqToObjectiveC  #import "NSArray+LinqExtensions.h" 它为NSArray添加了许多方法,能让你用流式API来转换.排序.分 ...

  7. 网站安全配置(Nginx)防止网站被攻击(包括使用了CDN加速之后的配置方法)

    原文链接:http://www.bzfshop.net/article/176.html 网站被攻击是一个永恒不变的话题,网站攻击的方式也是一个永恒不变的老套路.找几百个电脑(肉鸡),控制这些电脑同时 ...

  8. RAC 开启gsd和oc4j服务

    Oracle 11g RAC中,发现oc4j以及gsd服务都处于offline状态,这是Oracle 11g RAC默认情形.即便如此,并不影响数据库的使用,因为 oc4j 是用于WLM 的一个资源, ...

  9. TForm类

    显示给用户的窗体有两种:有模式和无模式的.具体使用哪一种窗体,取决于是否希望用户能够同时与这个窗体和其他窗体交互. 1.当打开一个模式窗体后,用户无法与应用程序的其他部分交互,知道用户关闭了这个窗体. ...

  10. MyBatis 一对多,多对一关联查询的时候Mapper的顺序

    要先写association,然后写collection:这是由DTD决定的: <resultMap ...> <association ...> </associati ...