POJ_1942_Paths on a Grid 组合数学
2014-09-19 19:52
507 查看
跟小伙伴们出去吃饭。
题意:
一个(n+1)*(m+1)的网格,从左下的顶点出发,只能向上或向右走,要走到右上顶点,问有多少种走法?
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.
首先,肯定要走n+m步,其中必然有n部向上走m步向右走,所以答案就是C(N+M,N),n和m都比较大,可以使用第三种求的方法。
C(n,m)=m!/(n!*(n-m)!),上下同除(n-m)!就可以在O(n)时间中求出来答案了,然后题目中没有n和m都到10^9级别的数据。。。就过了。。。不过这种求组合数的方法还是值得一记的。
代码:
题意:
一个(n+1)*(m+1)的网格,从左下的顶点出发,只能向上或向右走,要走到右上顶点,问有多少种走法?
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.
首先,肯定要走n+m步,其中必然有n部向上走m步向右走,所以答案就是C(N+M,N),n和m都比较大,可以使用第三种求的方法。
C(n,m)=m!/(n!*(n-m)!),上下同除(n-m)!就可以在O(n)时间中求出来答案了,然后题目中没有n和m都到10^9级别的数据。。。就过了。。。不过这种求组合数的方法还是值得一记的。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> using namespace std; long long n,m; int main(){ while(scanf("%I64d%I64d",&n,&m)!=EOF){ if(!n&&!m) break; long long sum=n+m; long long minn=min(n,m); long long ans=1.0; for(long long i=1;i<=minn;++i) ans=ans*(sum-minn+i)/i; printf("%I64d\n",ans); } return 0; }
相关文章推荐
- POJ 1942 Paths on a Grid【组合数学】
- 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 (组合数学)
- POJ-1942-Paths on a Grid-组合数学
- POJ 1942 Paths on a Grid 找规律+组合数学
- Paths on a Grid POJ 1942 组合数学
- POJ1942——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(数学组合)
- POJ1942 Paths on a Grid (组合数学)
- POJ 1942-Paths on a Grid(组合数学-C(m+n,m))
- POJ 1942 Paths on a Grid【组合数学】
- POJ 1942 Paths on a Grid(组合数学)
- POJ 1942 Paths on a Grid (组合数学)
- poj 1942 Paths on a Grid 【数学题】