Uva 11971 Polygon(数论、概率)
链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3122
题目
Time Limit:1000MS Memory Limit:0KB
Description
John has been given a segment of lenght 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 ≤ 106 ; 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次切成k+1段,问可以形成一个多边形的概率是多少。
分析
从简单的模型开始思考,对于一根木棍切割成3段,能形成一个三角形的条件是最长木棍短于其他两根木棍长度之和。
所以本题能够形成多边形的条件就是最长的一根小于总长的
12
。先求不能够形成的概率,即第i根长度>=总长的
12
,此时其他k个点应该在另1-第i根长度,即:k个点在另外
12
的概率,故
12k
,而有k+1个点,所以
k+12k
,最后用1减,即为欲求概率
源码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,-1,sizeof x)
#define dbug cout<<"here"<<endl;
//#define LOCAL
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6+10;
const int MOD = 1000000007;
ull gcd(ull a, ull b){
if(b == 0)
return a;
return gcd(b, a%b);
}
ull quickPow(ull a, ull b){
ull ans = 1;
while(b){
if(b & 1){
ans = ans*a;
}
b = b >> 1;
a = a*a;
}
return ans;
}
int main(){
#ifdef LOCAL
freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin);
freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout);
#endif
int t;
cin >> t;
ll n,k;
int kase = 0;
while(t--){
cin >> n >> k;
ull dominator = quickPow(2, k);
ull up = dominator-k-1;
printf("Case #%d: ",++kase);
ull div = gcd(up, dominator);
cout << up/div << "/" << dominator/div << endl;
}
return 0;
}