紫书例题 10-21 UVa 11971 连续概率
来源:互联网 发布:javascript注册页面 编辑:程序博客网 时间:2024/05/17 06:33
题意:有一根长度为n的木条,随机选k个位置把它们切成k+1段小木条。求这些小木条能组成一个多边形的概率。
分析:不难发现本题的答案与n无关。在一条直线上切似乎难以处理,可以把直线接成一个圆,多切一下,即在圆上随机选k+1个点,把圆周切成k+1段。根据对称性,两个问题的答案相同。新问题就要容易处理得多了:“组不成多边形”的概率就是其中一个小木条至少跨越了半个圆周的概率。设这个最长的小木条从点i开始逆时针跨越了至少半个圆周,则其他所有点都在这半个圆周之外。
除了点i之外其他每个点位于灰色部分的概率均为1/2,因此总概率为1/2 k 。点i的取法
有k+1种,因此“组不成多边形”的概率为(k+1)/2 k ,能组成多边形的概率为1-(k+1)/2 k 。
题目要求分数表示,套一个分数类模板,注意要用LL。
#include <bits/stdc++.h>using namespace std;const int maxn = 510;typedef long long LL;struct Fraction{ LL a, b; //a代表分子,b代表分母 Fraction(){ a = 0; b = 1; } void reduction(){ if(a == 0){ b = 1; return; } LL gcdnum = __gcd(a, b); a /= gcdnum; b /= gcdnum; } Fraction(LL num){ //整数 a = num; b = 1; } Fraction(LL x, LL y){ a = x, b = y; this->reduction(); } void operator = (const LL &num){ a = num; b = 1; this -> reduction(); } void operator = (const Fraction &y){ a = y.a; b = y.b; this -> reduction(); } Fraction operator + (const Fraction &y) const{ LL gcdnum = __gcd(b, y.b); Fraction tmp = Fraction(a * (y.b / gcdnum) + y.a * (b / gcdnum) , b / gcdnum * y.b); tmp.reduction(); return tmp; } Fraction operator + (const LL &y) const{ return ((*this) + Fraction(y)); } Fraction operator - (const Fraction &y) const{ return ((*this) + Fraction(-y.a, y.b)); } Fraction operator - (const LL &y) const{ return ((*this) - Fraction(y)); } Fraction operator * (const Fraction &y) const{ Fraction tmp = Fraction(a * y.a, b * y.b); tmp.reduction(); return tmp; } Fraction operator / (const Fraction &y) const{ return ((*this) * Fraction(y.b, y.a)); }};int main(){ int T, ks = 0; LL n , k; scanf("%d", &T); while(T--){ scanf("%lld%lld", &n, &k); LL x = (1LL<<k) - k - 1; LL y = (1LL<<k); Fraction xx; xx = Fraction(x, y); printf("Case #%d: %lld/%lld\n", ++ks, xx.a, xx.b); } return 0;}
0 0
- 紫书例题 10-21 UVa 11971 连续概率
- uva 11971 Polygon 连续概率
- 紫书 例题10-18 概率 UVa 11346
- uva 11971 - Polygon(连续概率问题)
- 紫书例题 10-8 Uva 1262
- 紫书例题 10-26 UVa 11440
- 紫书搜索 例题7-10 UVA
- uva 10900 富翁 连续概率
- 连续概率(概率,uva 11346)
- 紫书例题10-16 UVa 12230 过河
- 紫书数论例题10-5 uva 12716 找规律
- 例题10-19 概率 UVa11346
- 例题10-10 UVA - 10491 Cows and Cars 奶牛与轿车 (全概率)
- 紫书搜索 例题7-4 UVA
- 紫书搜索 例题7-5 UVA
- 紫书搜索 例题7-6 UVA
- 紫书搜索 例题7-9 UVA
- 紫书搜索 例题7-12 UVA
- url加时间戳避免再次请求当前路径出现的缓存问题
- 标准IO_输入api_上
- 玲珑学院 - -1083 - XJT Love Digits
- Windows Server AppFabric分布式缓存详解
- POJ 3176 Cow Bowling(水题)
- 紫书例题 10-21 UVa 11971 连续概率
- Windows sockets (Winsock)的含义
- BootStrap+SpringMVC+Spring+MyBatis增删改查功能
- 如何旁路内核协议栈
- 【LeetCode】Repeated Substring Pattern 解题报告
- python中的paste deploy
- Java多线程编程:Callable、Future和FutureTask浅析
- HTML5-Canvas绘图练习1
- android:windowSoftInputMode属性详解