UVA 369题Combinations(一个排列组合问题)

从n个球中拿m个球有多少种方法?就是数学上的c(n,m)。你能用程序写出来么?比如要求c(100,6),数据那么大,你如何处理?  

                                                                Combinations          

Description

Computing the exact number of ways that N things can be takenM at a time can be a great challenge whenN 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:

displaymath41

Compute the EXACT value of:

displaymath43

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

 

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

The output from this program should be in the form:

N things taken M at a time isCexactly.

输入

100 6

20 5

18 6

0 0

输出

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和m,假设是n个球,编号为1.2.3.。。。。。。。n,从其中拿出m个球,求解有多少种拿法,即求c(n,m)。至于式子在上面也有。但问题也很明显,根据我们做数学题时的方法,算出各自的阶乘,数据太大,用int,long等都无法算出。为此我一直纠结着。然后我想到了递归,是的,递归很简单,代码很短,即根据c(n,m)=c(n,m-1)+c(n-1,m-1),而且当m==n和m==0时都有c(n,m)=1,运行结果也对了,我乐呵呵的去提交,却总是超时。。。也是,这个数据用这个算法,不超时才怪,我看到一些已经ac的都只花了一点点时间,我起初想着去优化这个递归,但做了无用功。。。这个题是我在比赛的最后半个小时,转换方法写出来的。哎,之前脑袋短路,没办法。嗯,我想到,既然最后结果是在long的范围内的,那么我在计算答案时,在计算分子时,每要乘以一个数,我都在分母里面找它的最大公约数,直到分母里已经没有能整除这个数了(除了1)。这样算下来,终于,用了乘法也没超出范围,时间是20ms。。。

#include<iostream>
#include<cmath>
using namespace std;
int gcd(int a,int b)   //计算最大公约数
{
	return (!b)?a:gcd(b,a%b);
}
int main()
{
	int m,n,a[100],top,low;    
	//这里top和low是用来记录分子分母各自阶乘的终点和起点,即这里c(n,m)=(low*(low+1)*(low+2)....*n)/top!
	long c;
	while(cin>>n>>m && (n||m))
	{
		int i,j,k,x;
		c=1;
		if(m>n-m+1)    //我们知道,c(n,m)=c(n,n-m),这里使得分子和分母没有同样的因子
		{
			low=m+1;
			top=n-m;
		}
		else
		{
			low=n-m+1;
			top=m;
		}
		//主要就是这里,每次要乘以一个分子,都在每个分母里找公约数,然后约去,化简.具体思想参见代码
		for(i=1;i<=top;i++)
			a[i]=i;
		for(i=low;i<=n;i++)
		{
			k=i;
			for(j=2;j<=top;j++)
			{
				x=gcd(k,a[j]);
				while(a[j]!=1 && x!=1)
				{
					k/=x;
					a[j]/=x;
					x=gcd(k,a[j]);
				}
			}
			c*=k;
		}
		cout<<n<<" things taken "<<m<<" at a time is "<<c<<" exactly."<<endl;
	}
	return 0;
}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值