码迷,mamicode.com
首页 > 其他好文 > 详细

SPOJ BALLSUM - Ball sum

时间:2017-08-19 21:20:09      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:font   form   example   ref   can   表示   ==   main   div   

题目链接http://www.spoj.com/problems/BALLSUM/

题目大意:问从N个数中选两个数和小于等于K的概率值,用分数表示。

解题思路:假设要选择小于等于5的数字,那么可选项有(1,4) (1,3) (1,2) (2,3),可以发现实际上是k-2 + k-4 +...当该项为0时停止即可。N个数里面选择两个可以直接n(n-1)/2,表示成分数则可以同除以GCD。

代码:

 1 const int maxn = 1e6 + 5;
 2 ll n, k;
 3 
 4 ll gcd(ll a, ll b){
 5     return b == 0? a: gcd(b, a % b);
 6 }
 7 void solve(){
 8     ll x = k - 2, up = 0;
 9     while(x > 0){
10         up += x;
11         x -= 2;
12     }
13     if(up == 0){
14         puts("0");
15         return;
16     }
17     ll down = n * (n - 1) / 2;
18     ll g = gcd(up, down);
19     up /= g; down /= g;
20     printf("%lld/%lld\n", up, down);
21     return;    
22 } 
23 int main(){
24     while(scanf("%lld %lld", &n, &k) && n != -1){
25         solve();
26     }
27 }

题目:

BALLSUM - Ball sum

 

You have a bag filled with N balls.Each Ball has a distinct number from 1 to N printed on it.All the numbers are distinct. You withdraw two balls from the bag and take their sum. You need to calculate the probability that the sum is not greater than the given number K(<=N). The Answer should be displayed in the form of p/q.(except when the answer is 0 or 1)

Input

Input consists of various test cases. Each test case consist of two integer inputs,N and K. (0<=K<=N<=1000000000) The program stops taking input when N and K equals -1

Output

Output the result in the form of p/q.(Except when the answer is 0 or 1)

Example

Input:
3 2
100 5
10 6
-1 -1

Output:
0
2/2475
2/15

 

SPOJ BALLSUM - Ball sum

标签:font   form   example   ref   can   表示   ==   main   div   

原文地址:http://www.cnblogs.com/bolderic/p/7397725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!