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. WIN32一些文件及窗口操作

    0,获取指定的系统路径: #include <shlobj.h>     #pragma comment(lib, "shell32.lib") TCHAR szPat ...

  2. 关于进程间通信的总结(IPC)

    一:三个问题 进程间通信简单的说有三个问题.第一个问题是一个进程如何把信息传递给另一个.第二个要处理的问题是是,要确保两个或更多的的进程在关键互动中不会出现交叉(即是进程互斥的问题),第三个问题是与正 ...

  3. 《SELinux安全上下文的管理(含图)》RedHat6.3——步骤详细、条理清晰

    1.为什么浏览器只识别/var/www/html下的文件? 2.为什么不识别别的目录下的index.html文件呢? 3.这里牵扯到身份证,先安装软件包. 4.打开selinux 5.建立一个新的目录 ...

  4. LESS学习总结

    之前在工作过程中,用到了Less,一直没有将学习心得整理归纳,今天终于空出时间整理了一下.   Less学习常用参考文档: Less 中文网 http://lesscss.cn/ 快速入门 | Les ...

  5. asp.net 后台 修改 javascript 变量

    Util.JSSetChange(Page, GetType()); public static void JSSetChange(Page page, Type cstype) { // Defin ...

  6. PHP中利用PCLZIP压缩解压文件

    <?php include_once('pclzip.lib.php'); $archive = new PclZip('archive.zip'); /* $v_list = $archive ...

  7. 淘宝IP地址库采集器c#代码

    这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下. 最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息.网上的Ip纯真数据库,下载下来一看 ...

  8. 监听EditText

    0.得到焦点的时候,作一些处理 public class AbcActivity extends Activity implements OnFocusChangeListener{ @Overrid ...

  9. 十二、mysql sql_mode 简学

    .一般默认情况下sql_mode默认为空,也就是不严格的sql检查 .如果sql_mode为空的情况下,测试: )); //定义一个name字段长度为定长2的tt3表 insert into tt3 ...

  10. 【Entity Framework 7】 完全不一样的玩法

    http://www.cnblogs.com/n-pei/p/4274907.html