POJ 2249-Binomial Showdown(排列组合计数)

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 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

题意:求C(n,m);

思路:这个是其中一种办法,就是连乘r个整商:C(n,k)=C(n,k-1)*(n-k+1)/k。时间复杂度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 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;
}
时间: 2024-08-03 21:18:41

POJ 2249-Binomial Showdown(排列组合计数)的相关文章

poj 2249 Binomial Showdown(组合数 公式优化)

//  组合数学,开始了-- 题目地址 : poj 2249 Binomial Showdown 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. Eac

poj 1715 Hexadecimal Numbers 排列组合

1 /** 2 大意: 给定16进制数的16个字母,,求第k大的数,,要求数的长度最大为8.,并且每个数互不相同. 3 思路: 从高到低挨个枚举,每一位能组成的排列数 ,拿最高位来说,能做成的排列数为15*A(15,len-i) 4 第二位 A(14,len-2)..这样就可以找到k大的数的长度 5 接下来 .找第k大的数.同上理 ,挨个枚举每一位即可..若加上该位的排列数大于k,则该位就是这个数,继续枚举下一位 6 **/ 7 8 /** 大神思路 9 首先确定数字串的长度Len:从大到小枚举

一般的排列组合计数公式

在n个不同的元素中: 若取 r 个按次序排列, 则成为从n中取r个排列,其排列数为:P( n, r )=( n! ) / ( n-r )!  . 如取出 r 个二不考虑起次序,则称为 从n中取 r 个组合, 其组合数为:C( n, r ) = ( n! )/[ ( r! ) *( n-r )! ] . 进行阶乘运算的数值较大, 直接计算分子和分母的话,效率低切容易溢出. 一 : 采取:连乘 r 个整商法 C( n, r ) = [ (n-r-1) / r ] * [ (n-r-2)/(r-1)

poj 3252 Round Numbers 【推导&#183;排列组合】

以sample为例子 [2,12]区间的RoundNumbers(简称RN)个数:Rn[2,12]=Rn[0,12]-Rn[0,1] 即:Rn[start,finish]=Rn[0,finish]-Rn[0,start-1] 所以关键是给定一个X,求出Rn[0,X] 现在假设X=10100100  这个X的二进制总共是8位,任何一个小于8位的二进制都小于X 第一部分,求出长度为[0,7]区间内的二进制是RoundNumber的个数  对于一个长度为Len的二进制(最高位为1),如何求出他的Rou

POJ 1496 POJ 1850 组合计数

Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8256 Accepted: 3906 Description Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is t

POJ 3252 组合计数

Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9149 Accepted: 3248 Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro,

POJ 1019 组合计数

链接:POJ 1019 /***************************************** author : Grant Yuan time : 2014/10/19 14:38 source : POJ 1019 algorithm: 组合计数 ******************************************/ #include <iostream> #include <cstdio> #include <algorithm> #

概率论1 计数-排列-组合

作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 概率 概率论研究随机事件.它源于赌徒的研究.赌博中有许多随机事件,比如投掷一个骰子,是否只凭运气呢? 赌徒逐渐发现随机事件的规律.投掷两个骰子是常见的赌博游戏.如果重复很多次,那么总数为2的次数会比总数7的次数少.这就是赌徒把握到的规律:尽管我无法预知事件的具体结果,但我可以了解每种结果出现的可能性.这是概率论的核心. "概率"到底是什么?这在数学上还有争议."

【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值

[题意]n位同学(其中一位是B神),m门必修课,每门必修课的分数是[1,Ui].B神碾压了k位同学(所有课分数<=B神),且第x门课有rx-1位同学的分数高于B神,求满足条件的分数情况数.当有一位同学的一门必修课分数不同时视为两种情况不同.n,m<=100,Ui<=10^9. [算法]计数DP+排列组合+拉格朗日插值 [题解]把分数作为状态不现实,只能逐门课考虑. 设$f[i][j]$表示前i门课,有j个同学被碾压的情况数,则有: $$f[i][j]=g(i)\cdot\sum_{k=j