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. 批量建文件夹【win7企业版】

    在记事本中notepad.exe按如下格式编辑好 md + 空格 + 文件夹名字(多个之间用空格隔开) 保存为.bat格式,运行即可批量创建文件夹.

  2. cocos2dx实现单机版三国杀(一)

    首先需要一个UI交互类 GameUI   -layer 一个游戏驱动类,负责游戏逻辑的循环 暂时定为GameScene- scene GameScene obj 调用update 更新游戏,addch ...

  3. Java系列学习(六)-数组

    1.方法: (1)定义: 完成特定功能块的代码,在有些语言中也叫函数 (2)格式: (3)方法的重载 [在同一个类中,方法名相同,参数列表不同,与返回值无关] 2.数组 (1)定义 存储同一种数据类型 ...

  4. 如何调用com组件中包含IntPtr类型参数的函数

    背景 公司的支付平台最近对接了西安移动的支付接口,接口中签名的方法是对方提供了一个com组件,组件中包含了一个签名的方法和一个验签的方法,注册了签名之后,在vs中进行了引用,引用之后,查看组件的定义如 ...

  5. js基础---object对象

    //**********************************复杂JSON举例**************************************** var Jsondata={d ...

  6. PHP魔术法__set和__get

    __set: 在给不可访问属性赋值时,__set() 会被调用.语法如下: public void __set ( string $name , mixed $value ) __get: 读取不可访 ...

  7. JSP中如何利用JS实现登录页面的跳转(JSP中如何利用JS实现跳转页面)

    <%!          <%                               url =              word =          }             ...

  8. C# WinForm窗体应用(第四天)

    一.点击登录按钮,将两个窗体进行连接,并进行用户名和密码验证. /// <summary> /// 登录设置 /// </summary> /// <param name ...

  9. 設置VS2015

    減少VsHub的資源占用 VsHub在某些環境下會挂,原因見這個帖子 其作用簡述如下: First, the service that detects and auto-updates extensi ...

  10. ( 转)Hibernate常用API

    http://blog.csdn.net/yerenyuan_pku/article/details/65103203 可在度娘上摘抄如下文字: Hibernate的核心类和接口一共有6个,分别为:S ...