题目链接: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;
}