加载中...

POJ 1942 - Paths on a Grid


0x00 POJ 原题

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?

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

0x10 题目概述

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

0x20 解题思路

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

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

只要给定了长 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() 函数

详细看我的两个程序。

0x30 AC 源码

0x31 解题方法一:四舍五入+强制类型转换

/*强制类型转换输出*/

//Memory Time 
//220K   0MS 

#include<iostream>
#include<math.h>
using namespace std;

/*Compute (n+m)C min{n,m}*/

unsigned 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--);

    cnm+=0.5;      //double转unsigned会强制截断小数,必须先四舍五入
    return (unsigned)cnm;
}

int main(void)
{
    unsigned m,n;
    while(true)
    {
        cin>>m>>n;
        if(!m && !n)//承认这题的猥琐吧!竟然有其中一边为0的矩阵,一定要&&,用||会WA
            break;

        cout<<comp(n,m)<<endl;
    }
    return 0;
}

0x32 解题方法二: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;
}

相关资料


文章作者: EXP
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 EXP !
赏
  目录