poj-1942-Paths on a Grid【组合数】
2016-10-12 15:54
477 查看
题目链接:点击打开链接
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
Sample Output
大意:就是从一个矩形的左下角走到右上角,有多少种走法?注意一下,这里每一次只能向右走或是向上走。
思路:只要给定了长 n 和 宽 m,那么要从左下角走到右上角,不管怎么走,就一定要往右走m次,往上走n次。这里看来就是一个排列组合问题了:从 n+m 个位置上挑出 n(m) 个位置为 “上(下)”,剩下的 m(n) 个位置就只能为 “下(上)” 了。不过有些细节需要注意的,浮点型向整型强转的时候把小数部分全部舍去(不是四舍五入)。
此题就不能打阶乘表,一是没有 n,m 的取值范围,二是结果没有对某个数取余
Paths on a Grid
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 25008 | Accepted: 6229 |
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
大意:就是从一个矩形的左下角走到右上角,有多少种走法?注意一下,这里每一次只能向右走或是向上走。
思路:只要给定了长 n 和 宽 m,那么要从左下角走到右上角,不管怎么走,就一定要往右走m次,往上走n次。这里看来就是一个排列组合问题了:从 n+m 个位置上挑出 n(m) 个位置为 “上(下)”,剩下的 m(n) 个位置就只能为 “下(上)” 了。不过有些细节需要注意的,浮点型向整型强转的时候把小数部分全部舍去(不是四舍五入)。
此题就不能打阶乘表,一是没有 n,m 的取值范围,二是结果没有对某个数取余
#include<cstdio> #include<algorithm> #include<cstring> #define LL long long using 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; }
相关文章推荐
- POJ 1942 Paths on a Grid 组合数学
- POJ1942 Paths on a Grid(组合)
- [ACM] POJ 1942 Paths on a Grid (组合)
- [ACM] 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 Paths on a Grid 组合以及处理阶乘
- POJ-1942-Paths on a Grid-组合水题
- poj 1942 Paths on a Grid 求组合数mCn的方法
- POJ 1942 Paths on a Grid 找规律+组合数学
- poj_1942 Paths on a Grid(组合数学 求C(a+b, a))
- 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(组合数学--组合数)
- POJ - 1942 D - Paths on a Grid 组合数
- POJ 1942-Paths on a Grid(组合数学-C(m+n,m))