[HDOJ6114] Chess(组合数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6114

其实就是n*m的格子里放棋子,当前层的棋子必须在上一层的右边,问多少种放法。

其实就是求组合数,注意n m大小。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 const LL mod = 1e9+7;
 6 const int maxn = 110000;
 7 int n, m;
 8 LL f[maxn];
 9 
10 void init() {
11   f[0] = f[1] = 1;
12   for(int i = 2; i < maxn; i++) f[i] = (f[i-1] * i) % mod;
13 }
14 
15 LL mul(LL x, LL n) {
16   LL ret = 1;
17   while(n) {
18     if(n & 1) ret = (ret * x) % mod;
19     n >>= 1;
20     x = (x * x) % mod;
21   }
22   return ret;
23 }
24 LL exgcd(LL a, LL b, LL &x, LL &y) {
25   if(b == 0) {
26     x = 1;
27     y = 0;
28     return a;
29   }
30   else {
31     LL ret = exgcd(b, a%b, x, y);
32     LL tmp = x;
33     x = y;
34     y = tmp - a / b * y;
35     return ret;
36   }
37 }
38 LL inv(LL a) {
39   LL x, y;
40   exgcd(a, mod, x, y);
41   return (x % mod + mod) % mod;
42 }
43 LL C(LL x, LL y) {
44   return f[x] * inv(f[x-y]) % mod * inv(f[y]) % mod;
45 }
46 
47 signed main() {
48     // freopen("in", "r", stdin);
49     init();
50     int T;
51     scanf("%d", &T);
52     while(T--) {
53         scanf("%d%d",&n,&m);
54         if(n == m) {
55             printf("1\n");
56             continue;
57         }
58         if(n > m) swap(n, m);
59         printf("%I64d\n", C(m,n));
60     }
61     return 0;
62 }

 

posted @ 2017-08-18 12:24  Kirai  阅读(150)  评论(0编辑  收藏  举报