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

UVa 11971 Polygon (数学,转化)

2016-08-07 18:30 417 查看
题意:一根长度为n的木条,随机选k个位置将其切成k+1段,问这k+1段能组成k+1条边的多边形的概率。

析:这个题,很明显和 n 是没有任何关系的,因为无论 n 是多少那切多少段都可以,只与切多少段有关。然后我们要转化一下,不能直接做,因为不好做。

转化为一个圆上选 m+1 个点,能不能组成多边形,很容易知道如果一个边大于一半圆的周长,那就组不成多边形。然后位置是随便选的,概率就是1,

然后其他 m-1 个点,就只能放那一半上,每个都有1/2的概率,然后 m 个,就是1/(2^m),然后每个点都可能是最长的,所以再乘以(m+1),

这是组不成的概率,然后用总概率1减去,就是组成的概率。注意long long,刚开始忘了,WA了一次。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}

int main(){
int T;  cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d %d", &n, &m);
LL a = m + 1;
LL b = 1LL<<m;
LL c = b - a;
LL g = __gcd(c, b);
printf("Case #%d: %lld/%lld\n", kase, c / g, b / g);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: