51nod 1627 瞬间移动

题意:

有一个n * m的矩形,一开始从(1, 1)开始,每次能够到达右下方格子的一个格子,求到达(n, m)的方案数。

 

题解:

很显然最多走min (n - 2, m - 2) + 1步,枚举步数K,答案为 求和 C(n - 2, K - 1) * C(m - 2, K - 1)

 

代码:

#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;
const int N = 1e5 + 7;
#define LL long long

int n, m;
LL F[N], ans;
map <int, LL> inv;

int Pow (int x, int cnt) {
	int ret = 1;
	while (cnt) {
		if (cnt & 1) ret = (LL) ret * x % mod;
		x = (LL) x * x % mod;
		cnt >>= 1;
	}
	return ret;
}

int main () {
	scanf ("%d%d", &n, &m);
	F[0] = 1, inv[0] = 1;
	for (int i = 1; i < N; ++i) {
		F[i] = F[i-1] * i % mod;
		inv[i] = Pow (F[i], mod - 2);
	} 
	int lim = min (n - 2, m - 2);
	for (int i = 1; i <= lim + 1; ++i) {
		LL ans1 = F[n-2] * inv[i-1] % mod * inv[n-i-1] % mod;
		LL ans2 = F[m-2] * inv[i-1] % mod * inv[m-i-1] % mod;
		ans = (ans + ans1 * ans2 % mod) % mod;
	}
	cout << ans << endl;
	return 0;
}

 

posted @ 2016-11-02 21:09  xgtao  阅读(289)  评论(0编辑  收藏  举报