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));
    }
}

原文地址:https://www.cnblogs.com/cautx/p/11404384.html

时间: 2024-10-03 00:03:46

POJ 2249 暴力求组合数的相关文章

POJ 1306 暴力求组合数

Combinations Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11049   Accepted: 5013 Description Computing the exact number of ways that N things can be taken M at a time can be a great challenge when N and/or M become very large. Challen

POJ 2992 Divisors 求组合数因子个数

题目来源:POJ 2992 Divisors 题意:... 思路:素数分解的唯一性 一个数可以被分解成若干素数相乘 p1^x1*p2^x2*...*pn^xn 根据乘法原理 因子数为 (x1+1)*(x2+1)*...*(xn+1) 不能直接求出组合数 会溢出 也不能把每个乘的数分解因子 这样会超时 C(N,M)=N!/(M!*(N-M)!) 另dp[i][j] 代表为i的阶乘中j因子的个数(j是素数) 那么i素数的个数为dp[n][i]-dp[m][i]-dp[n-m][i] 最后for循环从

求组合数C(m,n)的多种计算方法

https://ac.nowcoder.com/discuss/187813?type=101&order=0&pos=1&page=0 https://blog.csdn.net/shadandeajian/article/details/82084087 1.简单法---适合n,m很小 #include<bits/stdc++.h> using namespace std; const int MAXN = 1000; int C[MAXN+1][MAXN+1];

ZOJ 1938 Binomial &amp;&amp;poj 2249 (Binomial Showdown )(睡前一水)

链接:click here 题意: 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. 给你整数n和k,让你求组合数c(n,k). 代码: #include <cstdio> #include <cstring> #include <math.h> type

动态规划之求组合数

时间:2014.05.29 地点:基地 -------------------------------------------------------- 一.关于动态规划 如果问题是由交叠的子问题构成,则可用动态规划的方式求解.我们在将一个大问题划分为子问题的的过程中,如果递推关系中包含的子问题和大问题具有相同的形式,但由于子问题的交叠性质,我们用递归解决的代价往往很大,这时可考虑动态规划,即对每个交叠的子问题只求解一次,并把结果存储在记录表中,最后得出原始问题的解.直观上,好像动态规划是采取空

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

求组合数

组合数的计算虽说简单但也不乏有些陷阱,这主要是因为语言中的数据类型在表示范围上是有限的.更何况还有中间结果溢出的现象,所以千万要小心. 输入 求组合数的数据都是成对(M与N)出现的,每对整数M和N满足0<m, n≤20,以EOF结束. 输出 输出该组合数.每个组合数换行. 样例输入 5 2 18 13 样例输出 10 8568 代码 #include<stdio.h> int main(){int isum=1;int m,n,k;while(scanf("%d%d"

POJ 2329 (暴力+搜索bfs)

Nearest number - 2 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3943 Accepted: 1210 Description Input is the matrix A of N by N non-negative integers. A distance between two elements Aij and Apq is defined as |i ? p| + |j ? q|. Your pro

[2011山东ACM省赛] Binomial Coeffcients(求组合数)

Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; color:rgb(83,113,197); text-decoration:none; padding-top:0px"> Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 输入