首页 > 代码库 > UVA 11971 - Polygon(概率+几何概型)

UVA 11971 - Polygon(概率+几何概型)

UVA 11971 - Polygon

题目链接

题意:给一条长为n的线段,要选k个点,分成k + 1段,问这k + 1段能组成k + 1边形的概率

思路:对于n边形而言,n - 1条边的和要大于另外那条边,然后先考虑3边和4边形的情况,根据公式在坐标系中画出来的图,总面积为x,而不满足的面积被分成几块,每块面积为x/2k,然后在观察发现一共是k + 1块,所以符合的面积为x?x?(k+1)/2k,这样一来除以总面积就得到了概率1?(k+1)/2k

代码:

#include <cstdio>
#include <cstring>

const int N = 55;
typedef long long ll;

int t, k;
ll mi[N];

ll gcd(ll a, ll b) {
    while (b) {
	ll tmp = b;
	b = a % b;
	a = tmp;
    }
    return a;
}

int main() {
    mi[0] = 1;
    for (int i = 1; i <= 50; i++)
	mi[i] = mi[i - 1] * 2;
    scanf("%d", &t);
    int cas = 0;
    while (t--) {
	scanf("%*d%d", &k);
	printf("Case #%d: ", ++cas);
	if (k == 1) {
	    printf("0/1\n");
	    continue;
	}
	ll zi = mi[k] - k - 1;
	ll mu = mi[k];
	ll d = gcd(zi, mu);
	printf("%lld/%lld\n", zi / d, mu / d);
    }
    return 0;
}