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. Spring Boot之实现自动配置

    GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...

  2. Android的组件化和模块化

    Android随着业务的增多,而且后续新的需求的增加,代码的修改会变得非常频繁 然后最近在看组件化和模块化 公司的业务没有那么大,所以这种方式我并没有采取 但是还是需要了解下他的使用机制 还有优缺点之 ...

  3. apiClould微信三方登录,qq三方登录

    三方登录需要在config.xml文件中配置一下,res文件下需要新建一个key.xml文件中也需要配置,请点击链接查看详情 微信:https://docs.apicloud.com/Client-A ...

  4. 小程序模板中data传值有无...

    A:<template is="gemSelectColor" data="{{optionData}}" />B:<template is= ...

  5. Vue音乐项目笔记(一)

    看到一位小可爱的手记,这里记录一下自己需要注意的地方的链接 1.手写轮播图(上) https://blog.csdn.net/weixin_40814356/article/details/80298 ...

  6. DP 要求将承载量花光的01背包问题

    前言:很久没有发博客了,以后会捡起来,之后很长一段时间内我都会把精力放在攻克DP问题上,所以会经常上传一些DP学习笔记,把一些比较好的,没见过类型的DP问题都会传上来,希望能够变强吧. 因为今天很清醒 ...

  7. python 查看文件名和文件路径

    查看文件名和文件路径 1 >>> import os 2 >>> url = 'https://images0.cnblogs.com/i/311516/20140 ...

  8. 【IDEA】【4】遇到的问题

    前言: 1,jar包未导入到项目中 2,报错 cannot resolve symbol 3,左边栏只能看到文件看不到项目结构 4,报错 No valid Maven installation fou ...

  9. React文档(四)渲染元素

    元素是React应用的最小单位. 一个React元素描述了你在屏幕上所看到的东西: const element = <h1>Hello, world</h1>; 和浏览器页面中 ...

  10. input上传限定文件类型

    input上传限定文件类型 accept="image/*"   限定为只能上传图片 accept=”audio/*   限定为只能上传音频 accept=”video/*” 限定 ...