题目
M * N的方格,一个机器人从左上走到右下,只能向右或向下走。有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10^9 + 7的结果。
输入
第1行,2个数M,N,中间用空格隔开。(2 <= m,n <= 1000000)
输出
输出走法的数量 Mod 10^9 + 7。
输入样例
2 3
输出样例
3
思路:
由于只能向下或向右走,因此从(1,1)到(n,m)要向下走 n-1 步,向右走 m-1 步,共是 n-1+m-1=n+m-2 步
由于要选取不同的方案数,那么不同的方案在于是什么时候开始向下走 n-1 步的,那么即有 C(n+m-2,n-1)
最后要对结果取模,使用卢卡斯定理即可
源程序
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 50000+5;
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,-1,1};
using namespace std;
LL quick_pow(LL a,LL n,LL q) {
LL ret=1;
a%=q;
while(n) {
if(n&1)
ret=ret*a%q;
a=a*a%q;
n>>=1;
}
return ret;
}
LL getc(LL n,LL m,LL q) {
if(n<m)
return 0;
if(m>n-m)
m=n-m;
LL s1=1,s2=1;
for(int i=0; i<m; ++i) {
s1=s1*(n-i)%q;
s2=s2*(i+1)%q;
}
return s1*quick_pow(s2,q-2,q)%q;
}
LL lucas(LL n,LL m,LL q) {
if(!m)
return 1;
return getc(n%q,m%q,q)*lucas(n/q,m/q,q)%q;
}
int main() {
LL n,m;
cin>>n>>m;
LL ans=lucas(n+m-2,m-1,MOD);
printf("%lld\n",ans);
return 0;
}