http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119
考虑从(1,1)->(n,m)必定会向下走n-1步,向右走m-1步,那么总的走法是C(n-1+m-1,m-1)。
关于组合数取模:大神博客:http://blog.csdn.net/acdreamers/article/details/8037918
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 5 using namespace std; 6 typedef long long LL; 7 const LL p = 1e9+7; 8 LL n,m; 9 10 LL quick_mod(LL a, LL b) 11 { 12 LL ans = 1; 13 a %= p; 14 while(b) 15 { 16 if(b & 1) 17 { 18 ans = ans * a % p; 19 b--; 20 } 21 b >>= 1; 22 a = a * a % p; 23 } 24 return ans; 25 } 26 27 LL C(LL n, LL m) 28 { 29 if(m > n) return 0; 30 LL ans = 1; 31 for(int i=1; i<=m; i++) 32 { 33 LL a = (n + i - m) % p; 34 LL b = i % p; 35 ans = ans * (a * quick_mod(b, p-2) % p) % p; 36 } 37 return ans; 38 } 39 40 LL Lucas(LL n, LL m) 41 { 42 if(m == 0) return 1; 43 //printf("%I64d ",C(n,m)); 44 return C(n % p, m % p) * Lucas(n / p, m / p) % p; 45 } 46 47 int main() 48 { 49 scanf("%I64d%I64d", &n, &m); 50 printf("%I64d ", Lucas(n+m-2,m-1)); 51 return 0; 52 }