TOJ 1407. Paths on a Grid(组合数)

来源:互联网 发布:罗塞塔石碑软件下载 编辑:程序博客网 时间:2024/06/06 04:59

题目链接:http://acm.tju.edu.cn/toj/showp1407.html


1407.   Paths on a Grid
Time Limit: 1.0 Seconds   Memory Limit: 65536K
Total Runs: 3485   Accepted Runs: 756



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 Specification

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 Specification

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 41 10 0

Sample Output

1262


Source: University of Ulm Local Contest 2002
Submit   List    Runs   Forum   Statistics


一道组合数的题:

大致题意:

给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有多少种走法,每步只能向上或者向右走

 

解题思路:

非常水的中学数学题,用组合做

先简单建立一个数学模型:

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

例如给定 m=5,n=4

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

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

等等。。。

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

那么数学模型就是

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

 

处理阶乘有三种办法:

(1)       传统意义上的直接递归,n的规模最多到20+,太小了,在本题不适用,而且非常慢

(2)       稍快一点的算法,就是利用log()化乘为加,n的规模虽然扩展到1000+,但是由于要用三重循环,一旦n规模变得更大,耗时就会非常之严重,时间复杂度达到O(n*m*(n-m)),本题规定了n,m用unsigned int32类型,就是说n,m的规模达到了21E以上,铁定TLE的。而且就算抛开时间不算,还存在一个致命的问题,就是精度损失随着n的增加会变得非常严重。

因为n有多大,就要进行n次对数运算,n规模一旦过大,就会丢失得非常严重了。所以这种方法是绝对不可取的,因为中途的精度丢失不是简单的四舍五入可以挽回的。

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

 

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

       double cnm=1.0;

       while(b>0)

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

 

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

 

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

例如当 a=5,b=2时


 

由于用了 double去计算组合数,那么最后要转化为 无符号整型 时就要处理精度问题,有两种方法:四舍五入+强制类型转换  或者 用 setprecision()函数


(注:以上内容出自:優YoU  http://user.qzone.qq.com/289065406/blog/1301543725)


这道题注意不能用递归,起先找到规律试图用递归去解,但是数据规模太大。

我写的代码:

#include <stdio.h>long long gird(long long n,long long m){long long a=m+n;double sum=1.0;long long b=(n<m?n:m);while(b>0)sum*=(double)(a--)/(double)(b--);sum+=0.5;return (long long)sum;}int main(){long long n,m;while(~scanf("%lld%lld",&n,&m)&&(n+m))printf("%lld\n",gird(n,m));} 


0 0