BZOJ 4402: Claris的剑 (组合数学)

转:ws_yzy的博客

之所以是 C ( n + m , m ) C(n+m,m) C(n+m,m)而不是 C ( n + m − 1 , m − 1 ) C(n+m-1,m-1) C(n+m1,m1)是因为插数对时不一定全部插满。

CODE

#pragma GCC optimize ("O2")
#include 
using namespace std;
const int MAXN = 2000005;
const int mod = 1e9 + 7;
typedef long long LL;
int n, m, inv[MAXN], fac[MAXN];
inline void pre(int N) {
	inv[0] = inv[1] = fac[0] = fac[1] = 1;
	for(int i = 2; i <= N; ++i)
		inv[i] = 1ll * (mod - mod/i) * inv[mod%i] % mod,
		fac[i] = 1ll * fac[i-1] * i % mod;
	for(int i = 2; i <= N; ++i)
		inv[i] = 1ll * inv[i-1] * inv[i] % mod;
}
inline int C(int n, int m) { return 1ll * fac[n] * inv[m] % mod * inv[n-m] % mod; }
inline int solve(int n, int m) {
	if(n < 0) return 0;
	n >>= 1;
	return C(n + m, m);
}
int main () {
	scanf("%d%d", &n, &m); pre(max(n, m));
	int ans = (n && m);
	for(int i = 2; i <= m; ++i)
		ans = (ans + solve(n-i, i-1)) % mod,
		ans = (ans + solve(n-i-1, i-1)) % mod;
	printf("%d\n", (ans + mod) % mod);
}

你可能感兴趣的:(bzoj,组合)