Tuesday 6 November 2018

UVA 369 - Combinations






#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m,i,c,sum,x,z,y;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        z=n-m;
        if(m>z)
        {
            x=z;
            y=m;
        }
        else
        {
            x=m;
            y=z;
        }
        sum=1;
        c=1;
        for(i=n;i>y;i--)
        {
            //cout<<i<<" "<<sum<<" "<<c<<endl;
            sum*=i;
            sum/=c;
            if(c>=x)
                break;
            c++;
        }
        cout<<n<<" things taken "<<m<<" at a time is "<<sum<<" exactly."<<endl;
    }
    return 0;
}

No comments:

Post a Comment