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=a 2+2ab+b 2). 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

题意:输入n,m,代表n*m的矩阵,求从左下角到右上角的方法有多少种

思路:用我们平常的加法原理可以得出这个答案,但是n,m范围是unsigned int我们不能开这么大的数组

我们可以发现其实我们肯定会走n+m步,其中n步向上,m步向右,我们走上,我们路线肯定是由

n个上,m个右组成,所以我们在这求出一个组合数C(n+m,m),代表从这个路线顺序里面挑出

m个位置为向右走,用C(n+m,n)也是一样的结果

 
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
unsigned C(unsigned n,unsigned m)//n,m的每个地方都记得用unsigned类型
{
if(m>n-m) m=n-m;
unsigned t1,t2;
t1=n;
t2=m;
double vis=1.0;
while(t2>)//用double型存储组合数,可以每次进行约分,如果是实在是整型不好存储的话那就只能使用这个进行约分了
{
vis*=(double)(t1--)/(double)(t2--);
}
return (unsigned)(vis+0.5);//四舍五入
}
unsigned n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!m && !n)
break;
cout<<C(n+m,n)<<endl;
}
}

POJ - 1942 D - Paths on a Grid的更多相关文章

  1. POJ 1942:Paths on a Grid

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

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

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

  3. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  4. Paths on a Grid(简单组合数学)

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

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

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

  6. Paths on a Grid(规律)

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

  7. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

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

  9. POJ 1942 Paths on a Grid(组合数)

    http://poj.org/problem?id=1942 题意 :在一个n*m的矩形上有n*m个网格,从左下角的网格划到右上角的网格,沿着边画,只能向上或向右走,问有多少条不重复的路 . 思路 : ...

随机推荐

  1. Vue.js的后端数据支持:使用Express建立app, 并使用MongoDB数据库。

    需要用到的backed tech stack: Node: JavaScript on the server/backend. That's basically what it is, but mor ...

  2. BASE64图片转字符串

    Java代码图片字符串互转 /** * 将base64字符串转成图片 * TODO * @param imgStr base64图片字符串 * @param path 目标输出路径 * @return ...

  3. 31. Next Permutation (java 字典序生成下一个排列)

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  4. 『MXNet』im2rec脚本使用以及数据读取

    一.im2rec用法简介 首先看文档: usage: im2rec.py [-h] [--list] [--exts EXTS [EXTS ...]] [--chunks CHUNKS] [--tra ...

  5. 字符串加密解密(Base64)

    var Base64 = { // private property _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx ...

  6. php导出大数据scv

    1.我系统要导出30w的数据,刚开始我使用的是: ini_set('memory_limit', '1024M');set_time_limit(0); header("Content-ty ...

  7. Centos7 JDK安装过程中 解决java -version 报错: bash: /home/jdk1.8.0_161/bin/java: Permission denied

    1.执行Linux命令 -----vim /etc/profile  编辑profile  文件,在里面添加: #set java enviroment JAVA_HOME=/opt/JavaHome ...

  8. centos命令行系列之centos查看磁盘空间大小

    df -h 扩展: 1.查看当前文件夹所有文件大小 du -sh 2.查看指定文件下所有文件大小 du -h /data/ 3.查看指定文件大小 du -h install.log 4.查指定文件夹大 ...

  9. openssl实现双向认证教程(服务端代码+客户端代码+证书生成)

    一.背景说明 1.1 面临问题 最近一份产品检测报告建议使用基于pki的认证方式,由于产品已实现https,商量之下认为其意思是使用双向认证以处理中间人形式攻击. <信息安全工程>中接触过 ...

  10. Win32汇编环境搭建教程(MASM32 SDK)

    一.说明 常用的32位汇编编译器有微软的MASM.Borland的TASM和NASM. 编译器 开发者 优点 缺点 MASM 微软 微软自家软件和系统兼容性好:支持invoke/.if等伪指令将汇编变 ...