M * N的方格,一个机器人从左上走到右下,只能向右或向下走。有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10^9 + 7的结果。
Input
第1行,2个数M,N,中间用空格隔开。(2 <= m,n <= 1000000)
Output
输出走法的数量 Mod 10^9 + 7。
Sample Input
2 3
Sample Output
3
这道题,看到1e9+7就明白不用欧拉公式那么麻烦,直接上费马小定理即可。
完整代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll M,N;
ll jc(ll exp)
{
ll ans=1;
while(exp>1)
{
ans=(ans*exp)%mod;
exp--;
}
return ans;
}
ll feima(ll exp, ll x)
{
ll ans=1;
ll a=exp;
while(x)
{
if(x&1)
{
ans=a*ans%mod;
}
a=a*a%mod;
x>>=1;
}
return ans;
}
int main()
{
while(scanf("%lld%lld",&M,&N)!=EOF)
{
ll up=0;
up=jc(M+N-2);
ll down=0;
down=(jc(N-1)*jc(M-1))%mod;
down=feima(down, mod-2);
printf("%lld\n",(up*down)%mod);
}
return 0;
}