Project Euler Problem 15-Lattice paths
组合数,2n中选n个。向右走有n步,向下走有n步。共2n步。有n步是向右走的,计算向右走的这n步的所有情况,即C(2n,n)。
或者,每一步,只能从右边或者上边走过来,只有这两种情况,即step[i][j] = step[i-1][j]+step[i][j-1],递推即可。
#include <iostream>
using namespace std;
typedef long long ll;
ll C(ll n, ll m)
{
ll res = 1;
for(ll i = 1; i <= m; ++i)
res = res*(n-m+i)/i;
return res;
}
int main()
{
cout << C(40,20) << endl;
return 0;
}