1119 机器人走方格 V2(组合)

基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
M * N的方格,一个机器人从左上走到右下,只能向右或向下走。有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10^9 + 7的结果。
 
Input
第1行,2个数M,N,中间用空格隔开。(2 <= m,n <= 1000000)
Output
输出走法的数量 Mod 10^9 + 7。
Input示例
2 3
Output示例
3


//挺懵逼的,虽然看出动规后是个杨辉三角,但不知道
杨辉三角的性质,第 n 行 第 m 位为 C(n-1,m-1) 这也是组合数递推的性质 C(n,m)=C(n-1,m)+C(n-1,m-1)
输出 C(n+m-2,m-1) 即可
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define MOD 1000000007
 5 
 6 LL n,m;
 7 
 8 LL qk_mi(LL a,LL b)
 9 {
10     LL res=1;
11     while (b)
12     {
13         if (b&1) res = res*a%MOD;
14         a=a*a%MOD;
15         b/=2;
16     }
17     return res;
18 }
19 
20 LL J(int x)
21 {
22     LL res=1;
23     for (int i=2;i<=x;i++)
24         res=res*i%MOD;
25     return res;
26 }
27 
28 LL C(LL x, LL y)
29 {
30     return J(x)*qk_mi(J(y)*J(x-y)%MOD,MOD-2)%MOD;
31 }
32 
33 int main()
34 {
35     scanf("%lld%lld",&n,&m);
36     LL ans = C(n+m-2,m-1);
37     printf("%lld",ans);
38     return 0;
39 }
View Code

 

 
posted @ 2017-09-23 19:30  happy_codes  阅读(311)  评论(0编辑  收藏  举报