uva 10375 Choose and divide

原题:
Choose and divide
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 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
大意:
问你c(p,q)除以c(r,s)的结果是多少。

#include <bits/stdc++.h>
using namespace std;
int p[10001],inde;
bool tag[100000];
long long a[10001],b[10001];
vector<pair<double,double>> c;
void get_prime()
{
    int cnt=0;
    for(int i=2;i<10001;i++)
    {
        if(!tag[i])
            p[cnt++]=i;
        for(int j=0;j<cnt&&p[j]*i<10001;j++)
        {
            tag[i*p[j]]=1;
            if(i%p[j]==0)
                break;
        }
    }
}
void decomposition(int x,long long v[])
{
    for(int i=2;i<=x;i++)
    {
        int k=0;
        int t=i;
        while(t>1)
        {
            if(t%p[k]==0)
            {
                t/=p[k];
                v[p[k]]++;
            }
            else
                k++;
        }
    }
}
bool cmp(pair<double,double> a,pair<double,double> b)
{
    if(a.second!=b.second)
        return a.second<b.second;
    else
        return a.first<b.first;
}
int main()
{
    ios::sync_with_stdio(false);
    get_prime();
    int p,q,r,s;
    double ans;
    while(cin>>p>>q>>r>>s)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int x=r-s;
        int y=p-q;
        c.clear();
        ans=1;
        decomposition(p,a);
        decomposition(x,a);
        decomposition(s,a);
        decomposition(q,b);
        decomposition(r,b);
        decomposition(y,b);
        for(int i=2;i<=10000;i++)
        {
            if(a[i]-b[i]!=0)
            c.push_back(make_pair(i,a[i]-b[i]));
        }
        sort(c.begin(),c.end(),cmp);
        int l=0,r=c.size()-1;
        while(l<=r)
        {
            ans*=pow(c[l].first,c[l].second);
            l++;
            if(l>r)
                break;
            ans*=pow(c[r].first,c[r].second);
            r--;
        }
        cout<<fixed<<setprecision(5)<<ans<<endl;
    }
    return 0;
}

解答:
先笔算列出公式,发现最后分子是三个阶乘数的积,分母也是三个阶乘数的积。暴力肯定超时而且会溢出,所以考虑用唯一分解定理计算。把每个阶乘的数都分解成一堆质数的幂的积的形式。计算后分子分母的分解数做差,然后直接用pow计算即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值