# uva557 - 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 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 557 - Burger(概率）

• u011328934
• 2013年11月03日 15:10
• 2193

## [UVA557] Burger && 概率

• shiyukun1998
• 2014年12月26日 12:43
• 751

## UVA557 Burger 数论 递推组合

• u010682557
• 2013年12月25日 23:28
• 840

## UVa557 - Burger

• squee_spoon
• 2014年08月02日 21:23
• 654

## UVA 557 - Burger(概率 递推）

Burger  When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, th...
• u011217342
• 2013年11月21日 20:31
• 1177

## UVa 557 - Burger（概率＋递推）

• wcr1996
• 2015年03月16日 17:47
• 1196

## uva557 - Burger(汉堡)

• shankeliupo
• 2013年05月25日 21:06
• 547

## uva557(概率)

• yeyeyeguoguo
• 2015年02月04日 00:08
• 449

## UVA557(概率）

• MasterAn
• 2017年12月10日 18:24
• 54

## 全概率公式和贝叶斯公式

• qq_33638791
• 2016年09月10日 19:18
• 469

举报原因： 您举报文章：uva557 - Burger 概率递推 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)