POJ 2249 暴力求组合数

Posted cautx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于POJ 2249 暴力求组合数的主要内容,如果未能解决你的问题,请参考以下文章

排列组合

POJ-2992 Divisors---组合数求因子数目

poj1942(求组合数)

poj3252(组合数)

poj2992 divisors 求组合数的约数个数,阶乘的质因数分解

poj3219--二项式系数--组合数的奇偶性