SPOJ BALLSUM - Ball sum(规律)

该博客介绍了SPOJ上的BALLSUM问题,要求计算从1到N的不重复数字中随机取出两个球,其和不超过K的概率。通过分析不同测试案例,博主发现了概率计算的规律,并提供了相应的公式。解决方案不依赖于模拟,而是利用发现的数学模式进行计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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




题意:输入n, k, 从n个数里面选两个,求概率选出来的两个数不大于k的概率

解析:由于数据比较大,不能模拟,一定是有规律的,自己可以多列举几种情况推下规律,比如 100 5  

选        1 2 3 4 

次数    3 2 2 1 

规律就是   所有和不大于k的个数为  (k-2)*(k-2+1) / 2 + (k-1)/2


代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<string>
#define N 209
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long LL;


int main()
{
    LL n, k;
    while(1)
    {
        cin >> n >> k;
        if(n == -1 && k == -1) break;
        if(k <= 2)
        {
            puts("0");
            continue;
        }
        LL a, b;
        a = (k - 2) * (k - 1)  / 2;
        a = a + (k - 1) / 2;
        b = n * (n - 1);

        LL g = __gcd(a, b);
        a = a / g;
        b = b / g;
        if(a >= b)
        {
            puts("1");
            continue;
        }
        printf("%lld/%lld\n", a, b);
    }
    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值