Kickstart Practice Round 2017 Google
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
Output
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的更多相关文章
- Kickstart Practice Round 2017---A
Problem The Constitution of a certain country states that the leader is the person with the name con ...
- 中国2017 Google 开发者大会第一天简单回顾
昨天有幸参加了中国2017 Google 开发者大会,在这第一天就收获满满,昨天太忙了,今天早晨来一起简单回顾一下,可以让没有参加的童鞋们感受一下现场的温度. 早早就来到了会议现场,外面看不出什么特别 ...
- Google kickstart 2022 Round A题解
Speed Typing 题意概述 给出两个字符串I和P,问能否通过删除P中若干个字符得到I?如果能的话,需要删除字符的个数是多少? 数据规模 \[1≤|I|,|P|≤10^5 \] 双指针 设置两个 ...
- 2017 google Round D APAC Test 题解
首先说明一下:我只是用暴力过了4道题的小数据,就是简单的枚举,大数据都不会做!下面的题解,是我从网上搜到的解答以及查看排行榜上大神的答案得出来的. 首先贴一下主要的题解来源:http://codefo ...
- 2017 google Round C APAC Test 题解
题解参考网上的答案,以及我自己的想法. 主要参考网站:http://codeforces.com/blog/entry/47181,http://codeforces.com/blog/entry/4 ...
- google kickstart 2018 round D A Candies
思路: 对于small数据,由于求和及奇数数量两个限制条件均满足区间单调性,可以直接使用尺取法(滑动窗口法)求解. 对于large数据,奇数数量依然是满足区间单调性的.首先使用尺取法,找到所有满足奇数 ...
- 这是一份很有诚意的2017 Google I/O大会的汇总 & 解析
前言 在刚过去的凌晨(北京时间 5月18日 1.00-3.00),一年一度的2017年Google I/O大会在美国谷歌山景城海岸线圆形剧场如期举行 Google I/O 大会:Innovation ...
- Practice Round China New Grad Test 2014 报告
今天有Google of Greater China Test for New Grads of 2014的练习赛,主要是为了过几天的校园招聘测试做练习用的,帮助熟悉平台,题目嘛,个人觉得除了A题外, ...
- 2017 google IO大会——5.17
大家好! 每年一度的全球互联网及新型技术的盛会 Google IO,今年的大会日期和地址已经公布了:大会将在5月17至19日在谷歌公司总部边上的会场,美国加州 Mountain View的 Shore ...
随机推荐
- ucos队列的实现--源码分析
之前说到事件,讲了事件,信号量和互斥信号量,还有一个队列没说,今天说说队列. 队列是用在任务之间传送多个消息的时候,a任务发送消息,b任务发送消息,然后c任务可以依次去提取出b和a传递的消息,不会造成 ...
- Unknown/unsupported storage engine: InnoDB
症状:无法启动mysql,在“mysql数据库目录/主机名.err”日志文件中报错 Unknown/unsupported storage engine: InnoDB原因:MySQL5.5.8 GA ...
- Phplot--一些记录
1.一张图片画俩次 需要设置 $phplot->SetPrintImage(0); 参考 http://www.phplot.com/phplotdocs/ex-twoplot1.html
- 关于jquery的$.ajax发接口的同步与异步问题
1.在使用$.ajax发接口时想对返回数据做一些处理后拿到其他方法中使用 发请求函数如下: function getProjectName(projectId){ project.projectNam ...
- 聊聊Node.js 独立日漏洞
背景 Node.js 社区近期在美国独立日周末的狂欢之时爆出漏洞 https://medium.com/@iojs/important-security-upgrades-for-node-js-an ...
- Struts2命令空间小结
sturts2命名空间小结,以tomcat为服务器 1. 命名空间配置为“/” <package name="default" namespace="/" ...
- readystate, 异步
EventUtil.addHandler(window, "load", function(){ //create a new <script/> element. v ...
- PHP导入导出Excel方法
看到这篇文章的时候,很是惊讶原作者的耐心,虽然我们在平时用的也 有一些,但没有作者列出来的全,写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xm ...
- js架构设计模式——MVC,MVP 和 MVVM 的图示及简单明了的区别说明
MVC,MVP 和 MVVM 的图示 复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身很容易理 ...
- Unity3D在NGUI中使用mask
过程是这样的:最近一直想做一个头像的mask效果,后来发现原来unity的mask需要用shader来写,网上找了不少资料,也能实现,不过大多数都是用render texture作为相机投影的text ...