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. Challenges are the stuff of contests. Therefore, you are to make just such a computation given the following:
GIVEN: 5 <= N <= 100; 5 <= M <= 100; M <= N

Compute the EXACT value of: C = N! / (N-M)!M!

You may assume that the final value of C will fit in a 32-bit Pascal
LongInt or a C long. For the record, the exact value of 100! is:

93,326,215,443,944,152,681,699,238,856,266,700,490,715,968,264,381,621,

468,592,963,895,217,599,993,229,915,608,941,463,976,156,518,286,253,

697,920,827,223,758,251,185,210,916,864,000,000,000,000,000,000,000,000

Input

The
input to this program will be one or more lines each containing zero or
more leading spaces, a value for N, one or more spaces, and a value for
M. The last line of the input file will contain a dummy N, M pair with
both values equal to zero. Your program should terminate when this line
is read.

Output

The output from this program should be in the form:

N things taken M at a time is C exactly.

Sample Input

100  6
20  5
18  6
0  0

Sample Output

100 things taken 6 at a time is 1192052400 exactly.
20 things taken 5 at a time is 15504 exactly.
18 things taken 6 at a time is 18564 exactly.

题意:

输入n,k,然后算一下组合数就行了,关键:“You may assume that the final value of C will fit in a 32-bit”,所以还是很无聊的一题。

AC code:

#include<cstdio>
using namespace std;
typedef long long ll;
ll c[110][110];
void prepare()
{
    for(int i=0;i<=110;i++)    c[i][0]=1;
    for(int i=1;i<=110;i++)
        for(int j=1;j<=110;j++)
            c[i][j]=c[i-1][j]+c[i-1][j-1];
}
int main()
{
    //freopen("input.txt","r",stdin);
    prepare();
    ll n,k;
    while(~scanf("%lld%lld",&n,&k)&&n)
    {
        printf("%lld things taken %lld at a time is %lld exactly.\n",n,k,c[n][k]);
    }
    return 0;
}

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

时间: 2024-11-11 12:42:52

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

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

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

动态规划之求组合数

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

求组合数

组合数的计算虽说简单但也不乏有些陷阱,这主要是因为语言中的数据类型在表示范围上是有限的.更何况还有中间结果溢出的现象,所以千万要小心. 输入 求组合数的数据都是成对(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  有疑问?点这里^_^ 题目描写叙述 输入

hdu 2519 求组合数

求组合数 如果求C5 3 就是5*4*3/3*2*1 也就是(5/3)*(4/2)*(3/1) Sample Input5 //T3 2 //C3 25 34 43 68 0 Sample Output310101 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # def

hdu 4927 java求组合数(大数)

import java.util.Scanner; import java.math.BigInteger; public class Main { private static int [] a = new int[3005]; private static int T; private static int n; public static void solve() { BigInteger rs = BigInteger.ZERO; BigInteger temp = BigInteger