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个网格,从左下角的网格划到右上角的网格,沿着边画,只能向上或向右走,问有多少条不重复的路 . 思路 : ...
随机推荐
- LeetCode--292--Nim游戏
问题描述: 你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函 ...
- Confluence 6 如何让我的小组成员知道那些内容是重要的
如果你的 Confluence 中已经有了很多内容,定义那些内容是重要看起是一件艰巨的任务 —— 但是下面的一些特性能够帮助你的小组确定那些内容是他们应该关心的. 我的空间(My Spaces) 添加 ...
- 7 selenium 模块
selenium 模块 一.简介 1.Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. 2.自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接 ...
- Find a way HDU - 2612
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...
- cmd下可以启动java,输入javac提示 不“存在”
方法:手动把JDK安装目录的bin目录配置到PATH环境变量里
- 02 爬虫数据解析之re,xpath,beautifulsoup
一.正则匹配 简单用法演示: 字符: print(re.findall(".","abccc31223dn哈哈")) ### . 匹配除了换行符以外的任意字符, ...
- 巧用JSON
在开发的过程中,对json的接触基本是前端页面搭建完成后,对后台数据的请求.如果接口尚未提供,一般情况下会先按规定的要求写好需要的json模拟出请求的后台数据.json的格式有很多种,关注的主体是da ...
- vue开发移动端项目 过渡动画问题
App.vue: <div id="app"> <div class="content"> <transition :name ...
- ORACLE PACKAGE中查看包的依赖关系
SELECT dd.* FROM dba_dependencies dd WHERE NAME <> referenced_name AND referenced_type <> ...
- [CodeForces - 614B] B - Gena's Code
B - Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There ...
