POJ 2249-binomial Showdown (permutation combination count)

Source: Internet
Author: User

Binomial showdown
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18457 Accepted: 5633

Description

In how many ways can you choose k elements out of n elements, not taking order into account?
Write a program to compute the this number.

Input

The input would contain one or more test cases.
Each test case is consists of one line containing, integers n (n>=1) and K (0<=k<=n).
Input is terminated by-zeroes for N and K.

Output

For each test case, print one line containing the required number. This number would always fit to an integer, i.e. it would be is less than 231.
Warning:don ' t underestimate the problem. The result would fit into a integer-but if all intermediate results arising during the computation would also fit into an Integer depends on your algorithm. The test cases'll go to the limit.

Sample Input

4 210 549) 60 0

Sample Output

625213983816

Test instructions: C (n,m);

Idea: This is one of the methods, that is, even multiply R whole business: C (n,k) =c (n,k-1) * (n-k+1)/k. Time complexity O (n);

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <algorithm> #include <set> #include <queue> #include <stack> #include < Map>using namespace Std;typedef long LL; ll work (ll n,ll m) {    if (M>N/2) m=n-m;    LL a=1,b=1;    for (int i=1;i<=m;i++) {        a*=n-i+1;        b*=i;        if (a%b==0) {            a/=b;            b=1;        }    }    return a/b;} int main () {    LL n,m;    while (~scanf ("%lld%lld", &n,&m)) {        if (!n&&!m) break;        printf ("%lld\n", Work (N,m));    }    return 0;}



POJ 2249-binomial Showdown (permutation combination count)

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.