Paths on a Grid

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 23008 Accepted: 5683

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

Ulm Local 2002

从(0,0)走到(n,m)总共需要你n+m步,其中n步向右,m步向左,问题就转化为n+m步中选n步向右的方法或选m步向上的方法,(两种方式等价).注意处理数据超范围的问题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef unsigned long long LL; const int MAX = 1e5+10; int main()
{
LL n,m;
LL sum;
LL up,dn;
while(scanf("%I64u %I64u",&n,&m)&&(n||m))
{
sum=n+m;
n=min(n,m);
up=1;
dn=1;
for(LL i=1,j=sum;i<=n;i++,j--)
{
up*=j;
dn*=i;
if(up%dn==0)
{
up/=dn;
dn=1;
}
}
printf("%I64u\n",up);
}
return 0;
}

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

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

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

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

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

  3. Paths on a Grid(规律)

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

  4. 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)$ 但 ...

  5. POJ 1942:Paths on a Grid

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

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

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

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

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

  8. POJ - 1942 D - Paths on a Grid

    Imagine you are attending your math lesson at school. Once again, you are bored because your teacher ...

  9. 搭建selenium grid简单配置

    1.使用selenium提供的服务端独立jar包 :服务端.客户端都是运行于java7环境. 2.启动hub: hub配置文件如下: Java -jar selenium-server-standal ...

随机推荐

  1. html 标签内部元素上下居中

    <div style="width: 200px; height: 200px; border: 1px solid red; line-height: 200px;"> ...

  2. 转:python webdriver API 之 验证码问题

    对于 web 应用来说,大部分的系统在用户登录时都要求用户输入验证码,验证码的类型的很多,有字母数字的,有汉字的,甚至还要用户输入一条算术题的答案的,对于系统来说使用验证码可以有效果的防止采用机器猜测 ...

  3. java中时间的比较

    进入要比较从库中取出的时间,期初使用比较date.getTime()的值,但是当时间的年月日都相同时,时分秒较早的getTime()值比时分秒较晚的getTime()的值要大,至今笔者还不知这是为什么 ...

  4. springday03-go2

    新建springmvc01项目1.创建项目,导入jar包 拷贝jar/spring/first下的五个spring的jar包,以及jar/spring/mvc下的两个mvcjar包放在lib下 2.创 ...

  5. JSon_零基础_007_将JSon格式的"数组"字符串转换为Java对象"数组"

    将JSon格式的"数组"字符串转换为Java对象"数组". 应用此技术从一个json对象字符串格式中得到一个java对应的对象. JSONObject是一个“n ...

  6. js 获取select 中option 的个数

    //document.writeln(document.getElementById("sel").options.length); //document.writeln(docu ...

  7. 有趣的linux命令

    安装工具 debian => apt-get (In Debian like OS) red hat=> yum -y (In Red Hat like OS) mac => bre ...

  8. Cloudera CDH 、Impala本地通过Parcel安装配置详解

    一.Parcel本地源与Package本地源的区别 本地通过Parcel安装过程与本地通过Package安装过程完全一致,不同的是两者的本地源的配置. 区别如下: Package本地源:软件包是.rp ...

  9. TortoiseSVN,排除不想提交文件的方法(转)

    转自:tortoisesvnsubversionfilebuilddialoglist 下面是英文帮助: 利用TortoiseSVN的修改列表 功能可以实现,在新版本中TortoiseSVN特地预置了 ...

  10. 如何在WPF应用程序中使用视频处理控件TVideoGrabber

    要在WPF 中使用 TVideoGrabber 组件,需要像下面的方法来使用 VS.NET(DLL) 版本的组件: ——复制TVideoGrabber_x.x.x.x_x86.dll到c:/windo ...