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 ...
随机推荐
- 推荐一款非常好用的java反编译工具(转)
源: 推荐一款非常好用的java反编译工具
- Vue.js使用前
下载安装 node,npm,git 安装cnpm 淘宝cnpm镜像https://npm.taobao.org/,-g表示进行全局安装 npm install -g cnpm --registry=h ...
- 《OpenCV3 计算机视觉--Python语言实现 第二版》源代码及纠错
1.源代码下载地址 <OpenCV3 计算机视觉--Python语言实现 第二版>由我们翻译,英文书名<Learning OpenCV3 Computer Vision with P ...
- php中字符串长度和截取的函数
在做PHP开发的时候,由于我国的语言环境问题,所以我们常常需要对中文进行处理. 在PHP中,我们都知道有专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函 ...
- datalog
https://en.wikipedia.org/wiki/Datalog http://www.csd.uoc.gr/~hy562/1112_spring/instr_material/WhatYo ...
- IOS 使用GCD改善性能
1.GCD介绍 GCD:Grand Central Dispathch,核心中央调度,是一种异步技术.但是它是系统级的. 负责管理队列,是线程之上的抽象层.队列可以并行或串行运行,能够在系统级自动管理 ...
- Angular - - ngHref、ngSrc、ngCopy/ngCut/ngPaste
ngHref 在Angular程序没完成改变链接上用{{hash}}方式绑定的href值的时候,当用户点击该链接会跳到一个错误的页面. 格式:ng-href=”value” value:表达式. 使用 ...
- CNTV视频回看下载地址
这其实不是新方法了,这个方法早在五六年前大家就在用,至今也没失效.但是五六年前那时候这个方法可以下载到过去三个月内CNTV直播过的视频,而现在只能下到一个星期的.之所以要下载回看版本,是因为比栏目版本 ...
- Delphi中上指定进程(进程名)
procedure KillProcess(ExeName: string); const PROCESS_TERMINATE = $0001; //进程的PROCESS_TERMINATE访问权限 ...
- Canvas drawImage API
drawImage <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...