Kickstart Practice Round 2017 Google

本文介绍了一种计算在随机投票顺序下,候选人A始终保持领先的概率的算法。通过动态规划求解不同投票组合下的未结束情况数,利用对数运算解决大规模数据下的计算问题。

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))  这样就不会溢出了  :)

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #define pi acos(-1.0)
 7 #define mj
 8 #define inf 0x3f3f3f
 9 #define  db double
10 typedef  long long  ll;
11 using namespace std;
12 const int N=1e5+5;
13 db  dp[2002][2002];
14 void init()
15 {
16     for(int i=0;i<2002;i++){
17         for(int j=0;j<2002;j++)
18             dp[i][j]=-1;
19     }
20     dp[0][0]=log(1.0);
21     for(int i=0;i<2002;i++){
22         for(int j=0;j<2002;j++){
23             if(i>j){
24                     if(dp[i-1][j]!=-1&&dp[i][j-1]!=-1) dp[i][j]=dp[i-1][j]+log(1+exp(dp[i][j-1]-dp[i-1][j]));
25                     else if(dp[i-1][j]==-1&&dp[i][j-1]!=-1) dp[i][j]=dp[i][j-1];
26                     else if(dp[i-1][j]!=-1&&dp[i][j-1]==-1) dp[i][j]=dp[i-1][j];
27                 }
28         }
29     }
30 }
31 int main()
32 {
33     #ifdef mj
34     freopen("data.in","r",stdin);
35     freopen("data.out","w",stdout);
36     #endif // mj
37     int t,n,m;
38     scanf("%d",&t);
39     init();
40     for(int k=1;k<=t;k++){
41         scanf("%d%d",&n,&m);
42         db ans=0;
43         for(int i=1;i<=m;i++){
44             ans+=(db)log(i)-(db)log(n+i);
45         }
46         ans+=(db)dp[n][m];
47         ans=exp(ans);
48         printf("Case #%d: %.8f\n",k,ans);
49     }
50     return 0;
51 }

 

转载于:https://www.cnblogs.com/mj-liylho/p/6422283.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值