UVa 10375 Choose and divide (组合数相除)

10375 - Choose and divide

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1316

The binomial coefficient C(m,n) is defined as

        m!

C(m,n) = --------

        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).

The 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.

The 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

Output for Sample Input

0.12587

505606.46055

1.28223

0.48996

2.00000

3.99960

思路:不要被题目那个公式误导了~自己算一算几个组合数,比如C(100,4),发现分子分母都是4个乘数,那就好办了,写一个for循环每次一除一乘就是。

完整代码:

/*0.022s*/

#include<cstdio>
#include<algorithm>
using namespace std;  

int main()
{
    int p, q, r, s,maxn,i;
    double ans;
    while (~scanf("%d%d%d%d", &p, &q, &r, &s))
    {
        q = max(q, p - q), s = max(s, r - s);
        maxn = max(q, s);
        ans = 1.0;
        for (i = 1; i <= maxn; ++i)
        {
            if (i <= q) ans = ans / i * (p - q + i);
            if (i <= s) ans = ans / (r - s + i) * i;
        }
        printf("%.5f\n", ans);
    }
    return 0;
}

作者署名:csdn博客 synapse7

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索input
, for
, numbers
, 辗转相除
, line
, and
The
divide、new divide、bigdecimal divide、divide and conquer、digital divide,以便于您获取更多的相关知识。

时间: 2024-08-29 07:37:42

UVa 10375 Choose and divide (组合数相除)的相关文章

UVa 147 Dollars:经典DP&amp;amp;硬币组合数&amp;amp;整数拆分

147 - Dollars Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 和UVa 357一样. 注意所有数除以5再算. 完整代码: /*0.019s*/ #include<cstdio> #include<cstring> const int coin[11

LeetCode 29 Divide Two Integers(两个整数相除)(*)

翻译 不用乘法.除法.取余操作,将两个数相除. 如果它溢出了,返回MAX_INT 原文 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 代码 一心扑到了递归上,可惜没能写出来----烦躁至极还是找了别人的答案-- class Solution { public: int divide(int dividend, int d

UVa 369 Combinations:如何用double算组合数

369 - Combinations Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=305 Computing the exact number of ways that N things can be taken M at a time can be a

UVa 357 Let Me Count The Ways:经典DP&amp;amp;硬币组合数&amp;amp;整数拆分

357 - Let Me Count The Ways Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=293 After making a purchase at a large department store, Mel's change was 17

UVa 10247 Complete Tree Labeling:组合数学&amp;amp;高精度

10247 - Complete Tree Labeling Time limit: 15.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1188 A complete k-ary tree is a k-ary tree in which all leaves have same d

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10020:Minimal coverage

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=961 类型: 贪心 原题: The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the mini

UVa 507:Jill Rides Again

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=448 类型: 最大连续和 原题: Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill oft

UVa 270:Lining Up

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=206 原题: ``How am I ever going to solve this problem?" said the pilot. Indeed, the pilot was not facing an easy task. She h