1. 程式人生 > >BJ模擬(1) D1T1 計數

BJ模擬(1) D1T1 計數

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

template
inline bool R(T &x) {
	static char c;
	static bool iosig;
	for (iosig = false, c = read(); !isdigit(c); c = read()) {
		if (c == -1) return false;
		if (c == '-') iosig = true;
	}
	for (x = 0; isdigit(c); c = read()) 
		x = (x << 3) + (x << 1) + (c ^ '0');
	if (iosig) x = -x;
	return true;
}

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void writechar(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, oh - obuf, stdout), oh = obuf;
	*oh++ = c;
}

template
inline void W(T x) {
	static int buf[30], cnt;
	if (!x) writechar(48);
	else {
		if (x < 0) writechar('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) writechar(buf[cnt--]); 
	}
}

inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }

const int MAXN = 2010;
const int mod = 1e9 + 7;
int C[MAXN][MAXN], f[MAXN], g[MAXN];

int a, b, c, d;

inline void init(int a, int b, int *f) {
	if (a < b) swap(a, b);
	if (!a) return (void)(f[0] = 1);
	else if (!b) return (void)(f[a] = 1);
	for (int i = 0; i < 4; ++i) {
		int c = i + 1 >> 1;
		for (int j = 0; j <= min(b - c, a - 1); ++j) {
			int k = (a - 1 - j) + (b - c - j);
			int temp = 1LL * C[a - 1][j] * C[b - 1][j + c - 1] % mod;
			for (int l = k + 1; l <= a + b; ++l)
				f[l] = (f[l] + 1LL * C[a + b - 1 - k][l - k - 1] * temp) % mod;
		}
	}
}

int main() {
//	freopen("in.in", "r", stdin);
	R(a), R(b), R(c), R(d);
	for (int i = 0; i <= MAXN - 5; ++i) C[i][0] = C[i][i] = 1;
	for (int i = 0; i <= MAXN - 5; ++i)
		for (int j = 1; j <= i - 1; ++j)
			C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
	init(a, b, f), init(c, d, g);
	int ans = 0;
	for (int i = 0; i <= a + b; ++i) {
		ans = (ans + 2LL * f[i] * g[i] + 1LL * f[i] * g[i + 1]) % mod;
		if (i) ans = (ans + 1LL * f[i] * g[i - 1]) % mod;
	}
	ans = (ans % mod + mod) % mod;
	W(ans), flush();
	return 0; 
}