Choose and divide

来源:互联网 发布:淘宝前端团队 编辑:程序博客网 时间:2024/06/06 06:59
The binomial coefficient C(m,n) is defined as
         m!C(m,n) = --------         n!(m-n)!
Given four natural numbers pqr, 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 pqr, 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 993 45 84 59145 95 143 92995 487 996 4882000 1000 1999 9999998 4999 9996 4998

Output for Sample Input

0.12587505606.460551.282230.489962.000003.99960
#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<cmath>using namespace std;#define MAX(a,b) ((a)>(b) ? (a):(b))#define MIN(a,b) ((a)<(b) ? (a):(b))vector<int> v;int ans[10010];bool prime(int n){int m=floor(sqrt(n)+0.5);for(int i=2;i<=m;i++) if(n%i==0) return 0;return 1;}int integer(int n,int d){for(int i=0;i<v.size();i++){while(n%v[i]==0){n/=v[i];ans[v[i]]+=d;}if(n==1) break;}}void calculate(int n,int d){for(int j=2;j<=n;j++) integer(j,d);}int main(){int n,m,n1,m1,i;for(i=2;i<=10000;i++){if(prime(i)) v.push_back(i);}while(scanf("%d%d%d%d",&n,&m,&n1,&m1)!=EOF){double s=1.0; memset(ans,0,sizeof(ans));calculate(m,-1); calculate(n-m,-1);calculate(n1,-1);calculate(n,1);calculate(m1,1);calculate(n1-m1,1);for(vector<int>::iterator it=v.begin();it<v.end();it++){s*=pow(*it,ans[*it]);}printf("%.5lf\n",s);}return 0;}


0 0
原创粉丝点击