Paths on a Grid

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

题目大意:

    给定一个M*N的方格。问有多少种走法使其从左下角到右上角。

解题思路:

    简单的组合数学。

    从左下角到右上角。毕竟要向右M步,向上N步。共计M+N步。求Com[M+N][M]即可。

    PS:Com[M+N][M]=Com[M+N][N] 在求Com的时候,可以选择min(M,N)来进行计算。否则超时。。。

    PS2:注意被调写法的正确性。若先算分子后算分母会爆longlong。

Code:

 /*************************************************************************
> File Name: poj1942.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月21日 星期二 20时12分35秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define MAXN 100000
using namespace std;
long long c(long long a,long long b)
{
long long ret=;
for (long long i=;i<=a;i++)
{
ret=ret*(b--)/i; /*注意其正确性,每经过i个数,必有一个能被i整除*/
}
return ret;
}
int main()
{
long long a,b;
cout<<c(,)<<endl;
while (cin>>a>>b)
{
if (a>b) swap(a,b);
if (!a&&!b) break;
cout<<c(a,a+b)<<endl;
}
return ;
}

POJ1942——Paths on a Grid(组合数学)的更多相关文章

  1. poj1942 Paths on a Grid(无mod大组合数)

    poj1942 Paths on a Grid 题意:给定一个长m高n$(n,m \in unsigned 32-bit)$的矩形,问有几种走法.$n=m=0$时终止. 显然的$C(m+n,n)$ 但 ...

  2. poj1942 Paths on a Grid

    处理阶乘有三种办法:(1)传统意义上的直接递归,n的规模最多到20+,太小了,在本题不适用,而且非常慢(2)稍快一点的算法,就是利用log()化乘为加,n的规模虽然扩展到1000+,但是由于要用三重循 ...

  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. Paths on a Grid(简单组合数学)

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

  6. Paths on a Grid(规律)

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

  7. [ACM] POJ 1942 Paths on a Grid (组合)

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

  8. POJ 1942:Paths on a Grid

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

  9. Paths on a Grid POJ - 1942 组合数学 (组合数的快速计算)

    题意:格路问题 没什么难度 难点在于如何快速计算相对较大的组合数 思路:运用手写计算组合数的方式进行计算  如c(8,3) 如果手算就是   8*7*6/(3*2*1)这样可以很快得解出 计算代码为: ...

随机推荐

  1. 永远的月亮 2007? (献给L之二)

    文/安然 您是我心中永远的月亮 已经走远在曾年少的梦想 但是,蒙胧而明亮的月光永远珍藏 夜夜升起般不忘…… 多年后的路上依旧会有迷茫 梦中,又一次回到您的课堂 感受您暴躁的激情和无言深情的期望 当又一 ...

  2. NOIP2011(提高组)DAY2---观光公交(vijosP1741)

    描述 风景迷人的小城Y市,拥有n个美丽的景点.由于慕名而来的游客越来越多,Y市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第0分钟出现在1号景点,随后依次前往2.3.4……n号景 ...

  3. Exploit搭建

    1,三连下小水管真是慢.去洗澡先. 2,环境变量Path里添加Python安装目录.直接cd到git下来的目录运行sqlmap.py 更新sqlmap,sqlmap.py –update 或 git ...

  4. Android 自定义CheckBox 样式

    新建Android XML文件,类型选Drawable,根结点选selector,在这定义具体的样式. <?xml version="1.0" encoding=" ...

  5. TCP/IP:链路层

    链路层主要目的: 1.        为IP模块发送和接收IP数据报. 2.        为ARP模块发送ARP请求和接收ARP应答. 3.        为RARP发送RARP请求和接收RARP应 ...

  6. HTTP Error 500.21解决方案

    Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决方案   今天在测试网站的时候,在浏览器中输入http://localhost/时 ...

  7. 【Python笔记】异常处理

    1 什么是异常 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行.一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误. 当Pytho ...

  8. 【IOCP】 IOCP模型属于一种通讯模型- 较难

    http://baike.baidu.com/link?url=e9vXkKd2aHp8VDr1XTURdwQB4K85r28IYjeMwRIyuaXtsrCsXHY1eohiFgsDXRYRlj6x ...

  9. SQLSERVER 更改默认端口号

    最近这几天,服务器的数据库(SQLSERVER)老是遭受到攻击,有人不断地轮训想登陆数据库,从SQL的日志里可以看出来,一开始我是通过本地安全策略禁用了对应的几个攻击ip,同时把数据库的sa账号给禁用 ...

  10. EXTJS 4.2 资料 控件之Grid 那些事

    最近在学习Extjs4.2 ,积累文章,看得不错,再此留年: //表格数据最起码有列.数据.转换原始数据这3项 Ext.onReady(function(){ //定义列 var columns = ...