POJ1942——Paths on a Grid(组合数学)
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(组合数学)的更多相关文章
- 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)$ 但 ...
- poj1942 Paths on a Grid
处理阶乘有三种办法:(1)传统意义上的直接递归,n的规模最多到20+,太小了,在本题不适用,而且非常慢(2)稍快一点的算法,就是利用log()化乘为加,n的规模虽然扩展到1000+,但是由于要用三重循 ...
- poj 1924 Paths on a Grid(组合数学)
题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...
- POJ1942 Paths on a Grid(组合)
题目链接. 分析: #include <cstdio> #include <iostream> #include <map> #include <cstrin ...
- Paths on a Grid(简单组合数学)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...
- Paths on a Grid(规律)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23270 Accepted: 5735 ...
- [ACM] POJ 1942 Paths on a Grid (组合)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21297 Accepted: 5212 ...
- POJ 1942:Paths on a Grid
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22918 Accepted: 5651 ...
- Paths on a Grid POJ - 1942 组合数学 (组合数的快速计算)
题意:格路问题 没什么难度 难点在于如何快速计算相对较大的组合数 思路:运用手写计算组合数的方式进行计算 如c(8,3) 如果手算就是 8*7*6/(3*2*1)这样可以很快得解出 计算代码为: ...
随机推荐
- Libcurl笔记一
一:1,全局初始化及释放:CURLcode curl_global_init(long flags) flags: CURL_GLOBAL_ALL //初始化所有的可能的调用. CURL_GLOBAL ...
- OpenCV和Matplotlib色彩空间模式不一致的问题
当用OpenCV读取彩色图像时,OpenCV是以(BGR)的顺序存储图像数据的,而Matplotlib是以(RGB)的顺序显示图像的. 可以用下面的程序来证明这一点 import cv2 import ...
- 隐藏内容_网络推广_seo中级视频教程详解
课程背景:SEO(Search Engine Optimization),汉译为搜索引擎优化.搜索引擎优化是一种利用搜索引擎的搜索规则来提高目的网站在有关搜索引擎内的排名的方式.SEO目的理解是:为网 ...
- GDB 进行调试 使用心得
GDB 进行调试 使用心得 转 1: 对于在应用程序中加入参数进行调试的方法: 直接用 gdb app -p1 -p2 这样进行调试是不行的. 需要像以下这样使用: #gdb app ...
- Jquery实现搜索框提示功能
博客的前某一篇文章中http://www.cnblogs.com/hEnius/p/2013-07-01.html写过一个用Ajax来实现一个文本框输入的提示功能.最近在一个管理项目的项目中,使用后发 ...
- wamp Server2.5 配置 自定义目录
煎熬了两天终于找到了方法!!! 前提先改成中文 右键"W"图表-> Language -> chinese; 成功改为中文. 自定义目录步骤: 一.添加一个Alias ...
- QQ空间g_tk加密算法PHP版
QQ空间g_tk加密算法PHP版 //G_tk计算 function getGTK($skey){ $hash = 5381; for($i=0;$i<strlen($skey);++$i){ ...
- SQL*Loader使用详解(一)
1.SQL*Loader介绍 1)SQL*Loader是一个从外部文件指加载数据到Oracle数据库的工具.语法类似于DB2的Load语法,但SQL*Loader支持各种load格式.选择性load和 ...
- MVC使用的MetaModel代码生成器模板
代码生成器能使从一些重复的工作中缓解下来 在最近开发MVC项目中使用到了MetaModel用来设定Model的显示名称,数据限制的代码生成模板,自己第一做代码生成模板还有很多缺陷. 下面是模板代码: ...
- Lua基础之Function
概述:1.定义和调用 2.多返回值3.可变参数 原文地址 http://blog.csdn.net/dingkun520wy/article/details/50275387 1.定义和调用 函数,在 ...