Lattice paths
Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20×20 grid?
题目:
从一个2×2的方格的左上角开始,共有6条路径可达到右下角(不能后退)
请问,假如把上述方格的规模改成20×20,共有多少条路径呢?
这个题需要这样来理解,以2*2这个方格举例,方向只能往右或下,那右和下就分别为两次,就相当于,从4次中取2次右或下那么另一个方向他就定下来了,假如我先走两次右,那么后两次就只有下了;
所以可以通过排列数的计算公式来计算这个题 ,那么这个题的答案就可以通过: 来得到;最终就等于 :40 * ..... * 20 / 20 * ... * 1;那现在我们可以来实现代码了:
#include <stdio.h> int main() { long long m = 40, n = 20, ans = 1; while (m != 20) { if (n && ans % n == 0) ans /= n--;//if条件中的n防止n为0 else ans *= m--; } printf("%lld\n", ans); return 0; }
最终答案:137846528820