您的位置:首页 > 其它

UVA - 10375 Choose and divide 唯一分解定理

2017-08-30 21:27 375 查看
题解:

     先预处理一下素数,然后用数组e[]求出每个素数的指数,C(p,q) / C(r,s), C(m,n)
= m!/(m
− n)!
n!,可以把m!,n!,m-n!的素数的指数求出进行加减,得出最后的结果。这个题完全用到了唯一分解定理。




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 valuesfor
p,
q,
r, and
s, respectively, separated by a single space. All the numbers will be smaller than 10,000with
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 frac-tion, 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


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<cstdlib>
#include<cmath>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define rep3(i,a) for(int i=0;i<a;i++)
#define rep1(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,a,b) for(int i=a;i>=b;i--)
#define mem(x) memset(x,0,sizeof(x))
#define sfd(a) scanf("%d",&a)
#define sfld(a) scanf("%lld",&a)
#define sfs(a) scanf("%s",s)
#define oi(a) printf("%d",a)
#define ol(a) printf("%lld",a)
#define maxn 10000
int primes[maxn];
int _prime[maxn];
int e[maxn];
int plength;
void make_prime()
{
plength=0;
memset(_prime,false,sizeof(_prime));
for(int i=2;i<=10000;i++)
{
if(!_prime[i])
{
for(int j=2*i;j<=10000;j+=i)
{
_prime[j]=true;
}
}
}
for(int i=2;i<=10000;i++)
{
if(!_prime[i])
primes[plength++]=i;
}
}
void add_integer(int n,int d)
{
for(int i=0;i<plength;i++)
{
while(n%primes[i]==0)
{
n/=primes[i];
e[i]+=d;
}
if(n==1)
break;
}
}
void add_factorial(int n,int d)
{
for(int i=1;i<=n;i++)
add_integer(i, d);
}
int main()
{
make_prime();
int p,q,r,s;
while(~scanf("%d%d%d%d",&p,&q,&r,&s))
{
memset(e,0,sizeof(e));
add_factorial(p, 1);
add_factorial(q, -1);
add_factorial(p-q, -1);
add_factorial(r, -1);
add_factorial(s , 1);
add_factorial(r-s, 1);
double ans=1;
int maxnn=max(p,r);
for(int i=0;i<=maxnn;i++)
{
ans*=pow(primes[i],e[i]);
}
printf("%.5lf\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: