您的位置:首页 > 编程语言 > Go语言

uva 11971 Polygon

2016-09-27 19:11 399 查看
John has been given a segment of length 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 first line. T lines follow, each of them contains two integers N K (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.

Sample Input

2

1 1

2 2

Sample Output

Case #1: 0/1

Case #2: 1/4

中文:

给你一个长度为n的棍子,现在让你把这个棍子分成k+1段,问能形成多边形的概率是多少?

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll  gcd(ll a,ll b)
{
if(a%b==0)
return b;
return gcd(b,a%b);
}
int main()
{
ios::sync_with_stdio(false);
ll n,k;
int t,m=1;
cin>>t;
while(t--)
{
cin>>n>>k;
ll b=(ll)pow(2,k);
ll a=b-k-1;
cout<<"Case #"<<m++<<": ";
if(a==0)
cout<<0<<"/"<<1<<endl;
else
{
ll tmp=gcd(a,b);
cout<<a/tmp<<"/"<<b/tmp<<endl;
}
}
return 0;
}


解答:

紫书上的例题,也算是非常经典的一道题目。

首先把这个棍子围成一个圈,那么要形成k+1段就需要切k+1下。如果想形成一个矩形,那么最长的那条边不能超过这个棍子长度的一般。按照书上的思路,划一条“圆环”的直径,选择k个切割点都在圆环的直径的一侧的概率是12k,然后再直径连接的两点作为边界,以及之前切好的k个点当中,有k+1个小区间,在这些区间当中添加一个点,则半圆环靠近直径连接点两端之一的一个点加上半圆的另一侧就形成了一个大于圆环一半长度的小段。

如图



所以,违反规则的概率就是k+12k用1减去这个概率就是能形成矩形的概率。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: