POJ 2249 暴力求组合数

Binomial Showdown
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22692   Accepted: 6925

Description

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

Input

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

Output

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

Sample Input

4 2
10 5
49 6
0 0

Sample Output

6
252
13983816

题意:输入n,k求C(n,k)。

挺无聊的一题,因为答案在2^32内,直接开long long 然后暴力就行了。

AC code:

#include<cstdio>
using namespace std;
typedef long long int64;
int64 C(int64 n,int64 k)
{
    int64 a=1,b=1;
    for(int i=1;i<=k;i++)
    {
        a*=(n+1-i);
        b*=i;
        if(!(a%b)){
            a/=b;
            b=1;
        }
    }
    return a/b;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int64 n,k;
    while(~scanf("%lld%lld",&n,&k)&&n+k)
    {
        if(k>n/2)    k=n-k;
        printf("%lld\n",C(n,k));
    }
}
View Code

 

posted on 2019-08-24 13:30  Caution_X  阅读(132)  评论(0编辑  收藏  举报

导航