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. STM8的GPIO驱动

    芯片的外设一般按照这么几个流程来进行,GPIO,外部中断,定时器,串口,ADC,IIC,SPI,下面我就按照各个模式来写 首先是GPIO,STM8的GPIO拥有复用功能,这句话告诉我们必然需要配置IO ...

  2. Mybatis3.2.1整合Spring3.1

    Mybatis3.2.1整合Spring3.1 根 据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对 Mybatis ...

  3. js实现的文章输入检查与测速。

    在群里聊天,一个群友求助.说要实现 文章对比输入,出错了标红,正确的标绿. 同时还需要统计正确率. 我一开始以为很容易,结果搞了半天.最后折腾出来了. 这里的思路如下:利用js的数组.将文章和输入的内 ...

  4. MVC笔记1

    测试实体类 public class Test { public int id{get;set;} [Required(ErrorMessage="不能为空喔")] [String ...

  5. UVa 10684 - The jackpot

    题目大意:给一个序列,求最大连续和. 用sum[i]表示前i个元素之和,那么以第i个元素结尾的最大连续和就是sum[i]-sum[j] (j<i)的最大值,也就是找前i-1个位置sum[]的最小 ...

  6. iOS workspace 依次编译多个工程

    目的:当我封装一个framework的时候,需要检验当前的改动,但是又不想编译完framework,又要编译测试工程. 步骤: 1. 选择测试工程 2. Edit Scheme 3. 选中Build- ...

  7. php AES 加密类

    <?php class CryptAES { protected $cipher = MCRYPT_RIJNDAEL_128; protected $mode = MCRYPT_MODE_ECB ...

  8. 差分硬盘的merge(合并差异)实验分析

    实验: 1:在c:中新建father.vhd,及其差分硬盘son.vhd,command: diskpart create vdisk file=c:\father.vhd        #建立父硬盘 ...

  9. 排序问题思考(要求时间和空间复杂度尽可能的低)【Part 2】

    继上篇博文,今天我将先介绍一下什么是计数排序,将计数排序描述清楚后,再进行后续的桶排序方法解决这个问题. 通常情况下,一提到排序,大家第一反应就是比较,其实,今天我要说的这个计数排序,不是基于比较的排 ...

  10. CSS设置图片居中的方法

    如果是应用了表格,那么设置单元格为align="center"就可以使其中的一切内容居中.如果没有应用表格要想设置图片居中就有点困难了.困难来自不按"常规出牌" ...