欧拉计划 第十五题

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宫格中,无论哪种走法,一共都要走四步,向右走两步,向下走两步;

在20*20宫格中,无论哪种走法,一共都要走40步,向右走20步,向下走20步;

所以就变成了排列问题C42 = 4!/(2! * 2!);

Cmn = m!/(n! * (m - n)!);

注意

20的阶乘已经超过int类型,所以改成int64_t

(21~40)的乘积已经超过int64_t,所以边乘边除;

#include <stdio.h>
#include <inttypes.h>

int main(){
	int64_t sum = 1,m = 20;
	for(int64_t i = 40; i > 20; i--){
		sum *= i;
		while(sum % m == 0 && m != 1){
			sum /= m;
			m--;
		}
	}
	printf("%"PRId64"\n", sum);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38362049/article/details/80957468
今日推荐