agc001E - BBQ Hard(dp 组合数)

题意

题目链接

Sol

非常妙的一道题目。

首先,我们可以把\(C_{a_i + b_i + a_j + b_j}^{a_i + a_j}\)看做从\((-a_i, -b_i)\)走到\((a_j, b_j)\)的方案数

然后全都放的一起dp,\(f[i][j]\)表示从\((i, j)\)之前的所有点到\((i, j)\)的方案数

减去重复的即可


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10, mod = 1e9 +  7;
inline int read() {
	int x = 0, f = 1; char c = getchar();
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N, a[MAXN], b[MAXN], f[5001][5001], fac[10001], ifac[10001];
int add(int x, int y) {
	if(x + y < 0) return x + y + mod;
	return x + y > mod ? x + y - mod : x + y ;
}
int mul(int x, int y) {
	return 1ll * x * y % mod;
}
int fastpow(int a, int p) {
	int base = 1;
	while(p) {
		if(p & 1) base = mul(base, a);
		a = mul(a, a); p >>= 1;
	}
	return base;
}
void init() {
	fac[0] = 1;
	for(int i = 1; i <= 8000; i++) fac[i] = mul(i, fac[i - 1]);
	ifac[8000] = fastpow(fac[8000], mod - 2);
	for(int i = 8000; i; i--) ifac[i - 1] = mul(i, ifac[i]);
}
int id(int x) {
	return 2001 + x;
}
int C(int N, int M) {
	return 1ll * fac[N] * ifac[N - M] % mod * ifac[M] % mod;
}
main() {
//	freopen("a.in", "r", stdin);
	init();
	N = read();
	for(int i = 1; i <= N; i++) a[i] = read(), b[i] = read(), f[id(-a[i])][id(-b[i])]++;
    for(int i = 1; i <= 4221; i++)
    	for(int j = 1; j <= 4221; j++)
    			f[i][j] = add(f[i][j], add(f[i - 1][j], f[i][j - 1]));
    		//	printf("%d %d %d\n", i, j, f[i][j]);
    int sum = 0;
    for(int i = 1; i <= N; i++) 
		sum = add(sum, add(f[id(a[i])][id(b[i])], -C(a[i] + b[i] + a[i] + b[i], a[i] + a[i])));
		//这里会到8000.。。 
    sum = 1ll * sum * 500000004ll % mod;
    cout << sum % mod;
    return 0;
}
/*
8
2000 2000
1999 1998
1 1
1 1
2 1
1 3
2 1
3 3
*/
posted @ 2018-10-08 11:05  自为风月马前卒  阅读(401)  评论(0编辑  收藏  举报

Contact with me