poj-1942-Paths on a Grid【组合数】
来源:互联网 发布:梁朝伟和汤唯真的知乎 编辑:程序博客网 时间:2024/05/04 05:21
题目链接:点击打开链接
Paths on a Grid
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 25008 Accepted: 6229
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?
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 41 10 0
Sample Output
1262
大意:就是从一个矩形的左下角走到右上角,有多少种走法?注意一下,这里每一次只能向右走或是向上走。
思路:只要给定了长 n 和 宽 m,那么要从左下角走到右上角,不管怎么走,就一定要往右走m次,往上走n次。这里看来就是一个排列组合问题了:从 n+m 个位置上挑出 n(m) 个位置为 “上(下)”,剩下的 m(n) 个位置就只能为 “下(上)” 了。不过有些细节需要注意的,浮点型向整型强转的时候把小数部分全部舍去(不是四舍五入)。
此题就不能打阶乘表,一是没有 n,m 的取值范围,二是结果没有对某个数取余
#include<cstdio>#include<algorithm>#include<cstring>#define LL long longusing namespace std;LL n,m;/*LL C(LL a,LL b){double ans=1.0;b=min(b,a-b);while(b){ans=ans*(a--)/(b--);}return 1LL*(ans+0.5); // 浮点型向整型强转的时候,会把小数部分舍去,但是不是四舍五入,+0.5 就是让四舍五入的 }*/double C(LL a,LL b) // 定义这种返回值的,就没那么多事儿了 {double ans=1.0;b=min(b,a-b);while(b){ans=ans*(a--)/(b--);}return ans;}int main(){while(scanf("%lld%lld",&n,&m)&&(n||m)){//printf("%lld\n",C(n+m,n));printf("%.lf\n",C(n+m,n));}return 0;}
0 0
- POJ 1942 Paths on a Grid 组合数
- poj 1942 Paths on a Grid(组合数模板)
- POJ 1942 Paths on a Grid 组合数的优化
- POJ 1942 Paths on a Grid(组合数公式)
- poj-1942-Paths on a Grid【组合数】
- POJ - 1942 D - Paths on a Grid 组合数
- POJ 1942 Paths on a Grid(求组合数)
- poj 1942 Paths on a Grid (求组合数)
- poj 1942 Paths on a Grid(组合数学--组合数)
- poj1942 Paths on a Grid(组合数)
- poj 1942 Paths on a Grid 求组合数mCn的方法
- 组合数的递归调用:poj 1942 Paths on a Grid
- POJ 1942 Paths on a Grid 组合数的应用 (计算路径总个数)
- poj 1942 Paths on a Grid 组合数学
- Paths on a Grid POJ 1942 组合数学
- POJ 1942 Paths on a Grid(组合数学)
- POJ 1942 Paths on a Grid 组合以及处理阶乘
- [ACM] POJ 1942 Paths on a Grid (组合)
- 指针常量,常指针,指向常量的常指针
- 拖拽中怎么修复bug
- MTD(f) A Minimax Algorithm faster than NegaScout
- csdn博客挂了。。。
- MaxTenuringThreshold 和 TargetSurvivorRatio参数说明
- poj-1942-Paths on a Grid【组合数】
- Python
- Oracle的任务调度
- 背景引申到状态栏
- 在FirePrime上编译安装ROS jade
- 出游的一点小预防
- 百度地图的使用--SDK_V4_0_0
- Gamma 简述
- 自定义 hadoop MapReduce InputFormat 切分输入文件