uva557 - Burger 概率递推

原创 2013年12月03日 22:26:01

  Burger 

When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was held at the McDonald's restaurant at South Broadway 202, New York. There were 20 kids at the party, including Ben and Bill. Ronald McDonald had made 10 hamburgers and 10 cheeseburgers and when he served the kids he started with the girl directly sitting left of Bill. Ben was sitting to the right of Bill. Ronald flipped a (fair) coin to decide if the girl should have a hamburger or a cheeseburger, head for hamburger, tail for cheeseburger. He repeated this procedure with all the other 17 kids before serving Ben and Bill last. Though, when coming to Ben he didn't have to flip the coin anymore because there were no cheeseburgers left, only 2 hamburgers.


Ronald McDonald was quite surprised this happened, so he would like to know what the probability is of this kind of events. Calculate the probability that Ben and Bill will get the same type of burger using the procedure described above. Ronald McDonald always grills the same number of hamburgers and cheeseburgers.

Input 

The first line of the input-file contains the number of problems n , followed by n times:


a line with an even number [2,4,6,...,100000], which indicates the number of guests present at the party including Ben and Bill.

Output 

The output consists of n lines with on each line the probability (4 decimals precise) that Ben and Bill get the same type of burger.


Note: a variance of $\pm 0.0001$ is allowed in the output due to rounding differences.

Sample Input 

3
6
10
256

Sample Output 

0.6250
0.7266
0.9500

  N个人(N是偶数),两种汉堡各有N/2个,每个人按顺序扔硬币,如果是正面就吃第一种汉堡,反面就吃第二种,中途若一种被吃完就不用再扔。问最后两个人吃到同一种汉堡的概率。

  正的推公式很麻烦,如果反过来想,就是1减去两人吃不同汉堡的概率,也就是前面N-2个人把这两种汉堡各吃掉N/2-1个(中途不会结束)。等价于把前面N-2个人平均分成2组,为了简单起见,设有2n个人,则概率是C(2n - 2, n -1) * (1/2) ^ (2n - 2),但是N太大算不出来,所以要递推p[n]=p[n-1]*(1-0.5/(n-1))。最后再用1减就行了。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
double p[50010];
void init(){
    p[1]=1;
    for(int i=2;i<=50000;i++) p[i]=p[i-1]*(1-0.5/(i-1));
}
int main()
{
    freopen("in.txt","r",stdin);
    int T,N;
    init();
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        printf("%.4lf\n",1-p[N/2]);
    }
    return 0;
}




UVa 10288(Coupons) 数学期望(概率+递推) Java

数学:概率题。注意使用大整数。import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStr...

算法其实很有趣之——穷举法、递推、递归、分治、概率(算法需有通用性)

鸡兔同笼问题:今有鸡兔同笼,上有35头,下有94足,问鸡兔各几何? 这个问题曾经我的一个商人朋友跟我讲起过,像大多数人一样,我从数学的角度出发, 设鸡有 x 只,兔有 y 只, x + y...

poj 1322 Chocolate(递推/矩阵加速/高等代数+概率统计/数学)

poj 1322 Chocolate(递推/矩阵加速/高等代数+概率统计/数学)
  • PKU_ZZY
  • PKU_ZZY
  • 2016年05月22日 09:23
  • 397

利用递推解概率问题

1. 某种病菌,生命周期为一天,死亡之前会产生一些后代,生产 i 个后代的概率为 Pi ( i =0, ..., n-1) ,如果第一天有 k 个病菌,那么第 m 天病菌数量为 0 的概率是多少? ...

HDU 2048 神、上帝以及老天爷 (递推&错排概率)

思路: 1. N张字条的所有可能排列自然是N!(分母)。 现在的问题就是求N张字条的错排数f(N)(分子)。 首先我们考虑,如果前面N-1个人拿的都不是自己的票,即前N-1个人满足错排,现在又来了一个...

HDU - 4035(概率dp 用待定系数法解决递推依赖)

本题目的状态转移表达式是很好写的,但一个节点不仅依赖子节点,还会依赖父亲节点和1好节点的期望值。 假定 p = (1-ei-ki)/m ,(其中m为i节点的子节点和父亲节点总个数的和) ...

【bzoj 3450】【Tyvj 1952】Easy(概率与期望+递推)

那夜微风渐起,涟漪轻皱,鼻翼渐酸

概率,递推,找规律,高精度(FXTZ II,hdu 4043)

总结一下吧。。 像这种题,输入很简单,就几个数,然而正常算运算量很大,或时间复杂度很高,或分类讨论很麻烦的题,就试试找规律吧。只能说很可能是找规律,但也有例外吧。 这题的话,手算或计算机算多几个小...

BSOJ 4881 守望者的挑战---概率DP(更应该是递推)

守卫者的挑战 (guard.pas/c/cpp) 题目描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地。突然,眼前一道亮光闪过。“...

LIGHTOJ 1265 - Island of Survival [递推|概率dp]【杂类|动态规划】

题目连接:http://lightoj.com/volume_showproblem.php?problem=1265 ——————————————————————————–. 1265 - Is...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:uva557 - Burger 概率递推
举报原因:
原因补充:

(最多只允许输入30个字)