POJ - 1942 D - Paths on a Grid
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
Output
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的更多相关文章
- POJ 1942:Paths on a Grid
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22918 Accepted: 5651 ...
- [ACM] POJ 1942 Paths on a Grid (组合)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21297 Accepted: 5212 ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- Paths on a Grid(简单组合数学)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...
- POJ1942——Paths on a Grid(组合数学)
Paths on a Grid DescriptionImagine you are attending your math lesson at school. Once again, you are ...
- Paths on a Grid(规律)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23270 Accepted: 5735 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- 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)$ 但 ...
- POJ 1942 Paths on a Grid(组合数)
http://poj.org/problem?id=1942 题意 :在一个n*m的矩形上有n*m个网格,从左下角的网格划到右上角的网格,沿着边画,只能向上或向右走,问有多少条不重复的路 . 思路 : ...
随机推荐
- Tomcat基本组件、其功能和处理请求的过程
一.Tomcat是一个基于组件的服务器,它的构成组件都是可配置的,其中最外层的组件是Catalina Servlet容器,其他的组件按照一定的格式要求配置在这个顶层容器中 Tomcat的各个组件是 ...
- 20181013xlVba据成绩条生成图片文件
Sub CreateGoalPictures() '声明变量 Dim Wb As Workbook Dim Sht As Worksheet Dim Shp As Shape Dim Pic, End ...
- (转)python 全栈开发,Day74(基于双下划线的跨表查询,聚合查询,分组查询,F查询,Q查询)
昨日内容回顾 # 一对多的添加方式1(推荐) # book=Book.objects.create(title="水浒传",price=100,pub_date="164 ...
- hadoop 企业应用案例--大众点评
hadoop 企业应用案例--大众点评 http://f.dataguru.cn/thread-260531-1-1.html
- Android VideoView播放网络视频简介(转)
最近项目中用到了很多视频播放的地方,不管是聊天发送的视频消息,还是类似内涵段子的视频列表,都会涉及这些知识,不过网上的知识都很零散,一会找缓存方法,一会找预览图片的方法,一会找视频动态修改尺寸的方法, ...
- 配置ssh免密登录
安装ssh sudo apt-get install ssh 产生密钥:ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa (rsa为ssh的加密方式 ...
- EF - Database First 开发方式
概述 Database First 又叫数据库优先的开发方式,是一种比较旧的开发方式,现在越来越多的企业已经不再使用此种开发方式. 当然,对于一些旧项目进行升级,在已经有了数据库的情况下,使用此种方式 ...
- Utils--字符串的帮助类
Utils--字符串的帮助类 一个关于字符串过滤的工具类,主要时针对username和password的过滤 package com.bw.yun.utils; import java.securit ...
- vijos1448校门外的树
描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:K=1,K=1,读入l.r表 ...
- tf.expand_dims 来增加维度
主要是因为tflearn官方的例子总是有embeding层,去掉的话要conv1d正常工作,需要加上expand_dims network = input_data(shape=[None, 100] ...
