您的位置:首页 > 其它

uva 10375 分解质因数算指数

2016-07-11 20:29 309 查看
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

题目大意:

给出p,q,r,s

算 C(p, q) / C(r, s)

因为最大值是10000,直接算阶乘会爆

所以对阶乘分解质因数,先打素数表,算出每个素数的指数,相乘即可。

注意下 p可能为0,

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;

#define MAX_N 10000

int prime[MAX_N],e[MAX_N],tot=0;
int check[MAX_N];

void IsPrime(){
memset(check,0,sizeof(check));
for(int i=2;i<MAX_N;i++){
if(!check[i])prime[tot++]=i;
for(int j=0;j<tot;j++){
if(prime[j]*i>=MAX_N)break;
check[prime[j]*i]=1;
if(i%prime[j]==0)break;
}
}
}
void factor(int n,int e[MAX_N],int x){
for(int i=0;i<tot;i++){
while(n%prime[i]==0){
e[i]+=x;
n/=prime[i];
}
if(n==1)break;
}
}
void f(int n,int x){
for(int i=1;i<=n;i++){
factor(i,e,x);
}
}
int main()
{
IsPrime();
int p,q,r,s;
while(cin>>p>>q>>r>>s){
memset(e,0,sizeof(e));
f(p,1);
f(s,1);
f(r-s,1);
f(p-q,-1);
f(q,-1);
f(r,-1);
double ans=1;
for(int i=0;i<tot;i++){
ans*=pow(prime[i],e[i]);
}
printf("%.5lf\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: