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)