Paths on a Grid
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 21297   Accepted: 5212

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

简单题,求解 C(n+m, m) .

代码:

#include <iostream>
#include <algorithm>
using namespace std; long long c(long long n,long long m)
{
long long ans=1;
for(int i=1;i<=m;i++)
ans=ans*(n--)/i;
return ans;
} int main()
{
long long n,m;
while(cin>>n>>m&&(n||m))
{
if(n<m)
swap(n,m);
cout<<c(n+m,m)<<endl;
}
return 0;
}

http://blog.csdn.net/lyy289065406/article/details/6648516在里面学到了两种新的求解组合数的方法。

处理阶乘有三种办法:

(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()函数

  1. unsigned comp(unsigned n,unsigned m)
  2. {
  3. unsigned a=m+n;
  4. unsigned b=(m<n?m:n);
  5. double cnm=1.0;
  6. while(b>0)
  7. cnm*=(double)(a--)/(double)(b--);
  8. cnm+=0.5;      //double转unsigned会强制截断小数。必须先四舍五入
  9. return (unsigned)cnm;
  10. }
  1. double comp(unsigned n,unsigned m)
  2. {
  3. unsigned a=m+n;
  4. unsigned b=(m<n?m:n);
  5. double cnm=1.0;
  6. while(b>0)
  7. cnm*=(double)(a--)/(double)(b--);
  8. return cnm;
  9. }
  1. cout<<fixed<<setprecision(0)<<comp(n,m)<<endl;
  2. //fixed是为了固定小数位数
  3. //setprecision()函数是会自己主动四舍五入的,所以不用像强制类型转换那样预先+0.5

[ACM] POJ 1942 Paths on a Grid (组合)的更多相关文章

  1. POJ 1942 Paths on a Grid(组合数)

    http://poj.org/problem?id=1942 题意 :在一个n*m的矩形上有n*m个网格,从左下角的网格划到右上角的网格,沿着边画,只能向上或向右走,问有多少条不重复的路 . 思路 : ...

  2. POJ 1942 Paths on a Grid

    // n*m 的格子 从左下角走到右上角的种数// 相当于从 n+m 的步数中选 m 步往上走// C(n+m,m) #include <iostream> #include <st ...

  3. poj 1924 Paths on a Grid(组合数学)

    题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...

  4. POJ1942 Paths on a Grid(组合)

    题目链接. 分析: #include <cstdio> #include <iostream> #include <map> #include <cstrin ...

  5. POJ 1942:Paths on a Grid

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22918   Accepted: 5651 ...

  6. 北大ACM - POJ试题分类(转自EXP)

    北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...

  7. Paths on a Grid(简单组合数学)

    Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...

  8. POJ1942——Paths on a Grid(组合数学)

    Paths on a Grid DescriptionImagine you are attending your math lesson at school. Once again, you are ...

  9. Paths on a Grid(规律)

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23270   Accepted: 5735 ...

随机推荐

  1. SAS进阶《深入解析SAS》之SAS数据挖掘的一般流程

    SAS进阶<深入解析SAS>之SAS数据挖掘的一般流程 1. 所谓数据挖掘,是指通过对大量的数据进行选择.探索与建模,来揭示包含在数据中以前不为人所知的模式或规律,从而为商业活动或科学研究 ...

  2. Mac sierra下 wget安装

    本文由@ray 出品,转载请注明出处.  文章链接:http://www.cnblogs.com/wolfray/p/8040699.html 没有Wget的日子是非常难过的,强大的Mac OS 下安 ...

  3. Linux 一些小知识点汇总(持续更新....)

    一.符号 1.$@:传递的参数. 2.$# :传递参数的数量. 3.$?:指上一次执行命令后的返回值.一般0表示运行成功. 补充:$?只表示上一个命令执行后的退出状态,当命令执行后,又执行了其他命令, ...

  4. JavaScript学习书签

    JavaScript常用正则表达式 闭包 JavaScipt DOM 变量提升

  5. ICMP,ARP协议

    ICMP ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息.控制 ...

  6. day07-列表类型/元组类型/字典类型/集合类型内置方法

    目录 列表类型内置方法 元组类型内置方法 字典类型内置方法 集合类型内置方法 列表类型内置方法 用来存多个元素,[]内用逗号隔开任意数据类型的元素 1. list()强制类型转换 print(list ...

  7. API开发管理平台eoLinker AMS 4.1版本发布:加入聚合空间,发布AMS专业版等

    eoLinker AMS是集API文档管理.API自动化测试.开发协作三位一体的综合API开发管理平台,是中国最大的在线API管理平台. eoLinker AMS 4.1更新内容: 1.新增" ...

  8. map集合遍历,放入id

    背景,需要从电脑导入excel表格到网页上然后表格中公司需要对应数据库的id 通过key-value方法来对应id Office office = new Office();office.setG00 ...

  9. 【剑指Offer】66、机器人的运动范围

      题目描述:   地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时 ...

  10. Bequeath Connection and SYS Logon

    The following example illustrates how to use the internal_logon and SYSDBA arguments to specify the ...