Paths on a Grid 组合数学

Paths on a Grid

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 27868 Accepted: 6989

Description

Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time he's explaining that (a+b)2=a2+2ab+b2). So you decide to waste your time with drawing modern art instead.

Fortunately you have a piece of squared paper and you choose a rectangle of size n*m on the paper. Let's call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the left:


Really a masterpiece, isn't it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?

Input

The input contains several testcases. Each is specified by two unsigned 32-bit integers n and m, denoting the size of the rectangle. As you can observe, the number of lines of the corresponding grid is one more in each dimension. Input is terminated by n=m=0.

Output

For each test case output on a line the number of different art works that can be generated using the procedure described above. That is, how many paths are there on a grid where each step of the path consists of moving one unit to the right or one unit up? You may safely assume that this number fits into a 32-bit unsigned integer.

Sample Input

5 4
1 1
0 0

Sample Output

126
2

Source

Ulm Local 2002

只能向右或向上走,求到右上角有多少

只要给定了长m和高n,那么要从左下角走到右上角,不管怎么走,一定要往右走m次,往上走n次

例如给定 m=5,n=4

那么可以  上上上上上右右右右

又可以    上右上右上右上右上

等等。。。

关键是“上”和“右”的先后问题,就是组合问题了

那么数学模型就是

从n+m个位置,选择n个位放“上” (那么剩下m个位一定是“右”)

 处理阶乘最好的办法:拆分阶乘,逐项相除,再乘以前面所有项之积。这种方法用一个循环就OK了,时间复杂度只有O(n-m),非常可观。

 下面我根据程序详细说说算法(3):

       double cnm=1.0;

       while(b>0)

              cnm*=(double)(a- -)/(double)(b- -);

 

这是我写的函数原型,计算的是 aCb

 

这种算法巧妙地利用了分子分母的关系,而不是把公示中的3个阶乘单独处理。

 

四舍五入+强制类型转换

#include<iostream>
using namespace std;
unsigned cmn(unsigned a,unsigned b)
{
	unsigned n=a+b;
	unsigned m=a>b?b:a;
	double sum=1.0;
	while(m!=0)
	{
		sum*=(double)n*1.0/(double)m;   // 需要强制转换类型,否则精度损失 
		n--;
		m--;
	}
	return (unsigned)(sum+0.5);  //加上0.5取整 
}
int main()
{
	unsigned n,m;
	while(cin>>n>>m)
	{
		if(n==0&&m==0)
		break;
		cout<<cmn(n,m)<<endl;
	}
	return 0;
}

2. setprecision()函数

/*自定义精度输出*/
 
//Memory Time 
//220K   0MS 
 
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
 
/*Compute (n+m)C min{n,m}*/
 
double comp(unsigned n,unsigned m)
{
	unsigned a=m+n;
	unsigned b=(m<n?m:n);
	double cnm=1.0;
	while(b>0)
		cnm*=(double)(a--)/(double)(b--);
 
	return cnm;
}
 
int main(void)
{
	unsigned m,n;
	while(true)
	{
		cin>>m>>n;
		if(!m && !n)
			break;
 
		cout<<fixed<<setprecision(0)<<comp(n,m)<<endl;  
		//fixed是为了固定小数位数
		//setprecision()函数是会自动四舍五入的,所以不用像强制类型转换那样预先+0.5
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值