The binomial coefficient C(m, n) is defined as
C(m, n) = m! / (m − n)! 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
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
题意:已知C(m,n) = m!/(n!(m-n)!),输入整数p,q,r,s,计算C(p,q)/C(r,s).输出保证不超过10^8,保留五位小数。
题解:一道需要用到唯一分解定理的题目,数据范围是10000,先将10000的质数求出,然后统计最后的唯一分解式中每一个素数的指数即可。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 10005;
int prime[maxn];
int isprime[maxn];
int k;
int n,m,p,q;
int e[maxn];
void intc()
{
k = 0;
memset(e,0,sizeof(e));
memset(prime,0,sizeof(prime));
for(int i=2;i<=maxn;i++)
{
if(prime[i]==0)isprime[k++]=i;
for(int j=i*2;j<=maxn;j+=i)prime[j]=1;
}
}
void get_ans(int n,int d)
{
for(int i=0;i<k;i++)
{
while(n%isprime[i]==0)
{
n/=isprime[i];
e[i]+=d;
}
}
}
void add_integer(int n,int d)
{
for(int i=1;i<=n;i++)get_ans(i,d);
}
int main()
{
while(scanf("%d %d %d %d",&n,&m,&p,&q)!=EOF)
{
intc();
add_integer(n,1);
add_integer(m,-1);
add_integer(n-m,-1);
add_integer(p,-1);
add_integer(q,1);
add_integer(p-q,1);
double ans = 1;
for(int i=0;i<k;i++)
{
ans = ans *pow(isprime[i],e[i]);
}
printf("%.5f\n",ans);
}
return 0;
}