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. C#面向对象的方法写数组的功能

    上一篇文章用Java方法写出了可以对数组执行的功能,然后在用实例化后的对象调用这些方法来实现这些功能: 这篇随笔改用C#语言实现同样的功能 方法类:Array using System; using ...

  2. jsp页面指令

    JSP中共有三个指令: (1)page: 用于定义JSP文件中的全局属性 (2)include: 用于在JSP页面中包含另外一个文件的内容 (3)taglib: 此指令能够让用户自定义新的标签 第三个 ...

  3. CCSprite的使用方法大全

    一.精灵创建及初始化 1.从图片文件创建: CCSprite *sprite = [CCSprite spriteWithFile:@"ImageFileName.png"]; 默 ...

  4. mongo语句优化分析

    参考原文:http://www.mongoing.com/eshu_explain3 理想的查询状态由以下两种 普通查询: nReturned=totalKeysExamined & tota ...

  5. 夺命雷公狗—angularjs—19—angular-route

    ngRoute包括的内容 ng的路由机制是靠ngRoute提供的,通过hash和history两种方式实现了路由,可以检测浏览器是否支持history来灵活调用相应的方式.ng的路由(ngRoute) ...

  6. 夺命雷公狗ThinkPHP项目之----企业网站27之网站前台单页的完成(从百度编辑器里面取出文章数据)

    我们的单页面里主要是为了可以取出文章分类表的栏目内容,废话先不说, 我们的实现要点: 1...获取get过来的栏目cate_id 2...然后用条件查询栏目表 <?php namespace H ...

  7. 夺命雷公狗ThinkPHP项目之----企业网站15之文章删除的完成(单个)

    我们文章最后一步就到他的删除了,这个其实是最简单的一个,废话不多说,先来写控制器: public function del(){ $id = I('ar_id'); if(M('Article')-& ...

  8. 夺命雷公狗ThinkPHP项目之----企业网站11之栏目的删除完成

    我们删除要在分类模型中添加一个_before_delete的钩子函数,而且在删除一个分类时候,如果这个分类有子分类就不允许删除 model层代码如下所示: <?php namespace Adm ...

  9. Web服务器处理HTTP压缩之gzip、deflate压缩

    现如今在处理http请求的时候,由于请求的资源较多,如果不启用压缩的话,那么页面请求的流量将会非常大.启用gzip压缩,在一定程度上会大大的提高页面性能.   目录 一.什么是gzip 二.什么是de ...

  10. 1. 星际争霸之php面向对象(一)

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...