John has been given a segment of lenght N , however he needs a
polygon. In order to create a polygon he has cut given segment K times
at random positions (uniformly distributed cuts). Now he has K
+ 1 much shorter segments. What is the probability that he can assemble a polygon using all new segments? Input The number of tests T
( T 1000) is given on the rst line. T lines follow, each of them
contains two integers NK (1 N 10 6 ; 1 K 50) described above.
Output For each test case output a single line ` Case # T : F ‘. Where
T is the test case number (starting from 1) and F is the result as
simple fraction in form of N=D . Please refer to the sample output for
clarity.
不难发现答案只和k有关。
把问题转化成在一个圆上取k+1个点,切成k+1段,计算拼不成的概率,也就是有一段长度超过圆周一半的概率。
考虑这一段的某一个端点x,过x的直径把圆周分为两部分,如果有一段长度大于半个圆周,也就等价于剩下的k个点全在直径分成的两部分中的一个,概率为2*2^k。
点x总共有k+1种取法,每条边都被两个端点计算了两次,所以总的概率为1/2 * (k+1) * 2 * 2^k。
答案就是((1<< k)-k-1)/(1<< k)。
#include<cstdio>
#include<cstring>
#define LL long long
LL gcd(LL x,LL y)
{
return y?gcd(y,x%y):x;
}
int main()
{
int i,j,k,T,K=0;
LL x,y,z;
scanf("%d",&T);
while (T--)
{
scanf("%*d%d",&k);
x=1LL<<k;
y=x-k-1;
z=gcd(x,y);
x/=z;
y/=z;
printf("Case #%d: %lld/%lld\n",++K,y,x);
}
}