永夜初晗凝碧天

本博客现已全部转移到新地址,欲获取更多精彩文章,请访问http://acshiryu.github.io/

导航

POJ2249--一道简单的排列组合题

Binomial Showdown
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14435 Accepted: 4403

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);
刚开始时用递推,RE了几次,最后改成数组来,但有些细节没注意到,WA了几次,总的说来,这是一道比较简单的组合数学的基本功是的运用
排列组合的基本公式:

Pascal公式

和一些恒等式

要解答出这道题主要运用的就是恒等式(1)

我的提交情况

参考代码:

 1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 __int64 a[ 100000000];
9 int main()
10 {
11
12 __int64 m , n ;
13 while ( scanf("%I64d%I64d",&m,&n), m || n )
14 {
15 a[0]=1;
16 if ( n > m / 2 )
17 n = m - n ;
18
19 for ( int i = 1; i <= n ; i ++ )
20 a[i] = a[i-1] * ( m - i + 1 ) / i ;
21 printf("%I64d\n", a[n] );
22 }
23 return 0;
24 }

  

posted on 2011-08-05 11:24  ACShiryu  阅读(1034)  评论(4编辑  收藏  举报