首页 > 其他分享 >RMRC2016问题 B: Election(概率计算)

RMRC2016问题 B: Election(概率计算)

时间:2023-06-02 18:37:21浏览次数:33  
标签:votes 概率 candidate ll V1 EVERYONE Election print RMRC2016


石油大http://exam.upc.edu.cn/problem.php?cid=1242&pid=1


问题 B: Election


时间限制: 1 Sec  内存限制: 128 MB

提交: 83  解决: 12

[提交][状态][讨论版]

题目描述


After all the fundraising, campaigning and debating, the election day has finally arrived. Only two candidates remain on the ballot and you work as an aide to one of them.

Reports from the voting stations are starting to trickle in and you hope that you can soon declare a victory.

There are N voters and everyone votes for one of the two candidates (there are no spoiled ballots). In order to win, a candidate needs more than half of the votes. A certain number M≤N of ballots have been counted, and there are Vi votes for candidate i (V1+V2 = M), where V1 is the number of votes your candidate received.

Due to the historical data and results of highly scientific polls, you know that each of the remaining votes has a 50% chance to go to your candidate. That makes you think that you could announce the win before all the votes are counted. So, if the probability of winning strictly exceeds a certain threshold W, the victory is yours! We just hope you are sure of this, we don’t want any scandals...


输入


The first line of input contains a single positive integer T≤100 indicating the number of test cases. Next
T lines each contain four integers: N, V1, V2 and W as described above.
The input limits are as follows:
1≤N≤50
50≤W<100
V1,V2≥0
V1 + V2≤N


输出


For each test case print a single line containing the appropriate action:
If the probability that your candidate will win is strictly greater than W%, print
         GET A CRATE OF CHAMPAGNE FROM THE BASEMENT!
If your candidate has no chance of winning, print
         RECOUNT!
Otherwise, print
         PATIENCE, EVERYONE!


样例输入

4
5 0 3 75
5 0 2 75
6 1 0 50
7 4 0 75

样例输出

RECOUNT!
PATIENCE, EVERYONE!
PATIENCE, EVERYONE!
GET A CRATE OF CHAMPAGNE FROM THE BASEMENT!


【解析】:

题意,计算出剩余票数,投票,能使得1号获胜的概率,然后与w%比较。

以第三组示例解析:

剩余m=5票,1号得其中的3,4,5票便能获胜,所以1号获胜的概率:

p=C(m,3)*0.5^5 + C(m,4)*0.5^5 + C(m,5)*0.5^5

然后考虑到实数精度问题,过程中不进行除法,而是比较时,分子分母化简一下,使得运算过程全用long long即可

详见代码。

另,还有一处优化,如果算概率过程中,虽然p没加完,但已经大于概率w了,直接退出循环就行了。

【代码】:


#include <stdio.h>
#include <math.h>
typedef long long ll;
ll qpow(ll n,ll m){ll ans=1;while(m){if(m%2) 
		ans=(ans*n);m/=2;n=(n*n);}return ans;}

ll Cf[55][55];
int main()
{
 	//组合数打表 
	Cf[0][0]=1;//特殊  
	    for(int i=1;i<=51;i++)  
	        for(int j=0;j<=51;j++)
	            Cf[i][j]=(j==0)?1:(Cf[i-1][j]+Cf[i-1][j-1]);
	int n,v1,v2,w,T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d%d",&n,&v1,&v2,&w);
		int m=n-v1-v2;//剩余人数
		int sh=n/2+1;//获胜最少票数 
		int t=sh-v1;//还差几票
		ll p=0;
		int flag=0;
		for(int i=t>0?t:0;i<=m;i++)
		{
			p+=Cf[m][i];
			//printf("%lf\n",p/pow(2,m));
			if(p*100>w*qpow(2,m))
			{
				flag=1;break;
			}
		}
		if(flag)
			puts("GET A CRATE OF CHAMPAGNE FROM THE BASEMENT!");
		else if(2*v2>=n)
			puts("RECOUNT!");
		else
			puts("PATIENCE, EVERYONE!");
	}
	return 0;
}




标签:votes,概率,candidate,ll,V1,EVERYONE,Election,print,RMRC2016
From: https://blog.51cto.com/u_16125110/6404528

相关文章

  • 随机(50%概率)分配数据到工序1或工序2
    #生成一些随机数据foriinrange(100):ifnp.random.rand()<0.5:df.loc[i]=['工序1',np.random.randint(1,31)]else:df.loc[i]=['工序2',np.random.randint(1,31)]随机(50%概率)分配数据到工序1或工序2......
  • 概率生成函数
    概率生成函数认识概率生成函数,形如\[f(x)=\sum_{i=0}^{+\infty}P(X=i)x^i\]也就是 i 次项的系数是随机变量 X 等于 i 的概率。这个东西有两个用处:1关于概率\(f(1)=1\)其实就是把 \(f(x)\) 的所有系数加起来,而这里的系数就是概率2关于期望想一想,上面的......
  • Flip-Flop Hardening and Selection for Soft Error and Delay Fault Resilience
    Flip-FlopHardeningandSelectionforSoftErrorandDelayFaultResilience​​https://ieeexplore.ieee.org/document/5372275Thetraditionaltestmodelofgo/no-gotestingbeingquestionedbyincreasingdelayfaultmanifestationshasbecomeevenfurtherc......
  • <el-table-column type="selection" 多选框旁边多了个点
      问题效果 解决方式 ......
  • R语言中的Stan概率编程MCMC采样的贝叶斯模型|附代码数据
    原文链接:http://tecdat.cn/?p=11161最近我们被客户要求撰写关于贝叶斯模型的研究报告,包括一些图形和统计输出。概率编程使我们能够实现统计模型,而不必担心技术细节。这对于基于MCMC采样的贝叶斯模型特别有用R语言中RStan贝叶斯层次模型分析示例stan简介Stan是用于贝叶斯推理......
  • Failed to execute 'setSelectionRange' on 'HTMLInputElement'
    jcubic commented on7Jan2016WhenIusenumberinputI'vegoterrorinGoogleChromeUncaughtInvalidStateError:Failedtoexecute'setSelectionRange'on'HTMLInputElement':Theinputelement'stype('number')......
  • Revit二次开发实战02(选择对象Selection)
    Revit二次开发实战 Selection主要用于和用户交互,通过用户的选择,设置操作对象,以便进行处理;Selection属于界面操作的范畴,因此位于UIDocument类下面,而不是Document类下面;可以选择一个对象、多个对象、选择点、选择矩形框、框选多个对象等;通过过滤器可以提供一个强大的功能,可以......
  • Luogu P3978 [TJOI2015] 概率论
    定义\(f_i\)为\(i\)个节点组成的二叉树数量,\(g_i\)为\(i\)个节点组成的二叉树的叶子节点个数之和设当前\(i\)个节点组成的二叉树有\(a\)个叶子,容易发现分别删掉其中的\(1\)个叶子节点就能得到一个对应的\(i-1\)个节点的二叉树,总共会有\(a\)颗,可以发现每一个叶......
  • 采用拉丁超立方采样的电力系统概率潮流计算 (自适应核密
    采用拉丁超立方采样的电力系统概率潮流计算(自适应核密度估计,自适应带宽核密度估计)拉丁超立方采样属于分层采样,是一种有效的用采样值反映随机变量的整体分布的方法。其目的是要保证所有的采样区域都能够被采样点覆盖。该方法分成以下两步:①采样。对每个输入随机变量进行采样,确保随......
  • 概率生成函数
    概率生成函数最近联测打到了两道能用概率生成函数直接秒的题。但是我不会概率生成函数。概率生成函数.gb对于非负整数范围内的随机变量\(X\),令\(p_i\)表示\(X=i\)的概率,那么我们定义\(X\)的概率生成函数\(P\)为\(p\)的普通生成函数,即\(P=\sum_zp_iz^i\)。我们对......