hdu 5978 · To begin or not to begin【概率题】【手速题】

这道概率题很具有代表性,难度不高,无基础心细即可做。

【题意】

箱子里有1个红球和k个黑球,
两个人轮流不放回地从箱子里随机取出一个球,
当某人取到红球时获得胜利,游戏结束。
问先手是否获胜几率更大,
几率更大,输出”1”;
几率更小,输出”2”;
几率相等,输出”0”。

Tips:题目中,1≤k≤10^5,所以不考虑只有红球的情况(当然考虑进去推出的结论也适用)。

【提炼】

略。

【类型】

简单概率题(IIC类题)

【分析】

当k=1时,一个红球和一个黑球,先手获胜的概率P为
P(1+1)=12

当k=2时,一个红球和两个黑球,先手获胜的概率P为
P(2+1)=13+2312=23

当k=3时,一个红球和三个黑球,先手获胜的概率P为
P(3+1)=14+342312=12

当k=4时,一个红球和四个黑球,先手获胜的概率P为
P(4+1)=15+453413+45342312=35

故,先手获胜的概率P为

P(k+1)=1k+1+kk+1k1kk2k1++kk+1k1k12=k+22k+1

所以,我们只需判断 k+22 k+1k+22 两者大小关系即可

【时间复杂度&&优化】

O(1)

数学推导式不需优化。

简化:
显然,这里可以简化判断式子:
奇数表示优势相同;
偶数表示先手占优。

【代码】

/*
    coder:  Tangent Chang
    date:   2017/5/1
    Your internal mediocrity  is the moment when you lost the faith of being excellent!
*/

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<map>

using namespace std;

typedef long long ll;

const int maxn = 100001;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int EPS = -1e9;
const int MOD = 1000000007;

int main() {
    int n;
    while (~scanf("%d", &n)) {
        if (n & 1) printf("0\n");
        else printf("1\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值