您的位置:首页 > 其它

Paths on a Grid(poj 1942)

2016-10-28 14:31 435 查看
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=a 2+2ab+b 2).
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?

题意:输入两个数m,n分别是矩形区域的长和宽,求从左下角走到右上角的方法,只能向右(R)或向上(U)走。

思路:这道题就是U,R的排列组合问题,有m个R,n个U,求不全相异的全排列。

数学公式:C(m+n,m)或C(m+n,n)

m,n都在unsigned 范围,没看到结果也在unsigned范围导致,用高精度计算了



#include
int main(){
long double sum;
unsigned long long m,n;
while(scanf("%lld%lld",&m,&n)!=EOF){
sum=1;
if(!m&&!n){
break;
}
long long a=m+n;
unsigned  int c=m>n?n:m;
for(unsigned int i=1;i<=c;i++){
sum*=(double)(a--)/(double)i;
printf("%.0f\n",sum);
}
printf("%.0f\n",sum);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: