UVA13075 POJ2613 ZOJ1863 Choose and divide【组合计算＋迭代】

Choose and divide
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4366 Accepted: 1385

Description

The binomial coefficient C(m,n) is defined as
Ｃ ( m , n ) = m ! n ! ( m − n ) ! Ｃ(m,n)=\frac{m!}{n!(m-n)!}

Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s).

Input

Input consists of a sequence of lines. Each line contains four non-negative integer numbers giving values for p, q, r, and s, respectively, separated by a single space. All the numbers will be smaller than 10,000 with p>=q and r>=s.

Output

For each line of input, print a single line containing a real number with 5 digits of precision in the fraction, giving the number as described above. You may assume the result is not greater than 100,000,000.

Sample Input

10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998

Sample Output

0.12587
505606.46055
1.28223
0.48996
2.00000
3.99960

Source

Waterloo local 1999.10.02

AC的C++语言程序如下：

/* UVA13075 POJ2613 ZOJ1863 Choose and divide */

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
int p, q, r, s;
while(~scanf("%d%d%d%d", &p, &q, &r, &s)) {
double ans = 1;
if(p - q < q) q = p - q;
if(r - s < s) s = r - s;
for(int i = 1; i <= q || i <= s; i++) {
if(i <= q) ans = ans * (p - i + 1) / i;
if(i <= s) ans = ans * i / (r - i + 1);
}

printf("%.5f\n", ans);
}

return 0;
}


08-02 1万+

08-08 1200
07-29 1139
08-02 1243
08-02 1253
07-04 1883
04-05 40