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. [转]Linux命令之iconv

    转自:http://lorna8023.blog.51cto.com/777608/420313 用途说明 iconv命令是用来转换文件的编码方式的(Convert encoding of given ...

  2. [ USACO 2018 OPEN ] Out of Sorts (Platinum)

    \(\\\) \(Description\) 对一长为\(N\)的数列\(A\)排序,不保证数列元素互异: 数列\(A\)中\(A[1...i]\)的最大值不大于\(A[i+1-N]\)的最小值,我们 ...

  3. JavaScript入门笔记

    第一章 JavaScript语法 1.1 初识JavaScript 1.3 数据类型 1.4 string和boolean类型 1.5 算数操作符 第二章 JavaScript流程控制语句 2.1 循 ...

  4. Android_撕衣服小案例

    一直都觉得做安卓开发挺有意思,最近一段时间都在学习这方面的知识以及练习敲代码这次要说的是一个简单有趣的案例,相信大家也是看了标题才进来的吧,是不是有点迫不及待的想看看效果图,嘿嘿,算了还是直接给上源码 ...

  5. Win32子窗口的创建

    本文主要是在一个主窗口下创建一个子窗口.主窗口有一个菜单,菜单下只有设置一个选项,点击设置选项,弹出设置界面,点击设置界面关闭则关闭.我在开发的时候遇到两个问题,第一就是一点设置关闭就整个应用都关了, ...

  6. JS——try catch throw

    本例检测输入变量的值.如果值是错误的,会抛出一个异常(错误).catch 会捕捉到这个错误,并显示一段自定义的错误消息: <script> function myFunction() { ...

  7. SQL基本操作——UNION

    UNION 操作符:用于合并两个或多个 SELECT 语句的结果集.请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...

  8. linux下tomcat启动正常,但是外部浏览器无法访问

    这种情况一般是由于系统防火墙设置问题导致的,这次遇到的系统是centos 7.2,防火墙由iptables改成了firewall,因此停止防火墙的命令应该是: systemctl disable fi ...

  9. [Windows Server 2008] 安装SQL SERVER 2008

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装SQL S ...

  10. js 学习笔记---BOM

    window对象 1. window 对象是Global对象,在全局作用域中声明的变量和函数都可以通过window.来访问.跟直接在window上添加属性效果一样.唯一的区别就是delete时,如果是 ...