1131:求组合数

来源:互联网 发布:阿里云ecs建站教程 编辑:程序博客网 时间:2024/06/05 16:23

1131:求组合数


Description


组合数Cnr(n,r)=n!/r!/(n-r)!,虽然组合数的计算简单但也不乏有些陷阱,这主要是因为语言中的数据类型在表示范围上是有限的。更何况还有中间结果溢出的现象,所以千万要小心。


Input


求组合数的数据都是成对(M与N)出现的,你可以假设结果不会超过64位有符号整数,每对整数M和N满足0=<m, n≤28,以EOF结束。


Output


输出该组合数,每个组合数换行。


Sample Input


5  2

18  13

28  28


Sample Output


10

8568

1


HINT


提示公式:C(n,r)=C(n,n-r)


#include<iostream>using namespace std;int main(){     long long int m,n,a,b;    while(cin>>m>>n)    {        a=1;        b=1;        int temp=m-n;        if(temp<n)            n=temp;        for(int i=1;i<=n;i++)        {            a=a*i;            b=b*m;            m--;        }    cout<<b/a<<endl;    }}


原创粉丝点击