UVA 10375 Choose and divide

Source: Internet
Author: User

Original question:
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 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'll 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. 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
Effect:
Ask you what the result of C (p,q) divided by C (r,s) is.

#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; }

Answer:
First written calculation list the formula, found that the last molecule is the product of three-step multiplier, the denominator is also the product of the three-order multiplier. Violence must be timed out and overflow, so consider using the unique decomposition theorem. The number of each factorial is decomposed into the form of the sum of the power of a bunch of prime numbers. The decomposition number of numerator denominator is poor after calculation, then it can be calculated directly with POW.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.