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
C ( m , n ) = m ! n ! ( m − n ) ! C(m,n)=\frac{m!}{n!(m-n)!} (m,n)=n!(mn)!m!

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

问题链接UVA13075 POJ2613 ZOJ1863 Choose and divide
问题简述:给定p,q,r,s,计算C(p,q)/C(r,s)。
问题分析:采用迭代计算来实现。根据公式C(n,k)=C(n,k−1)∗(n−k+1)/k,得到迭代计算公式C(p,q)=(p-q+1)/q*C(p,q-1)。
程序说明:(略)
参考链接:(略)
题记:(略)

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;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页