hut 1574 组合问题 解题报告

链接:http://openoj.awaysoft.com/JudgeOnline/problem.php?id=1574

Description

有n 种颜色的球,每种颜色的球的个数有任意多个,从中取m个,问有多少种取法

Input

每行2个正整数 n,m(<=31) ,n=0,m=0结束. 

Output

输出有多少种取法

Sample Input

2 2 3 3 0 0

Sample Output

3 10

本题是一个纯组合问题,题意也比较简单即:从无限多重集中取若干元素的组合数
N = C( n+m-1 , m )
再通过组合数性质C( n, m ) = C( n-1, m ) + C( n-1, m-1 ),直接打表无压力水过。
 1 #include <stdio.h>
2 #include <stdlib.h>
3 long long a[40][40];
4 void fun( )
5 {
6 a[1][0]=a[1][1]=1;
7 for( int i=2; i<40; ++i )
8 {
9 a[i][0]=1;
10 a[i][1]=i;
11 for( int j=2; j<40; ++j )
12 {
13 a[i][j]=a[i-1][j]+a[i-1][j-1];
14 }
15 }
16 }
17 int main()
18 {
19 fun( );
20 int n, m;
21 while( scanf( "%d%d", &n, &m ) != EOF , n+m)
22 {
23 printf( "%lld\n", a[n+m-1][m] );
24 }
25 return 0;
26 }

  

原文地址:https://www.cnblogs.com/jian1573/p/2130210.html