uva 11971 Polygon

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减去这个概率就是能形成矩形的概率。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值