[AGC028D](dp计数)

题解点我

Code

#include <bits/stdc++.h>

typedef long long LL;
typedef unsigned long long uLL;

#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) std::make_pair(x, y)
#define DE(x) cerr << x << endl;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define GO cerr << "GO" << endl;

using namespace std;

inline void proc_status()
{
	ifstream t("/proc/self/status");
	cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}
inline int read() 
{
    register int x = 0; register int f = 1; register char c;
    while (!isdigit(c = getchar())) if (c == '-') f = -1;
    while (x = (x << 1) + (x << 3) + (c xor 48), isdigit(c = getchar()));
    return x * f;
}
template<class T> inline void write(T x) 
{
    static char stk[30]; static int top = 0;
    if (x < 0) { x = -x, putchar('-'); }
    while (stk[++top] = x % 10 xor 48, x /= 10, x);
    while (putchar(stk[top--]), top);
}
template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }

const int maxN = 1000;
const int mod = 1e9 + 7;

void pls(int &x, int y)
{
	x += y;
	if (x >= mod) x -= mod;
	if (x < 0) x += mod;
}

int n, K, to[maxN], sum[maxN];
int dp[maxN][maxN], g[maxN];

int main() 
{
#ifndef ONLINE_JUDGE
    freopen("xhc.in", "r", stdin);
    freopen("xhc.out", "w", stdout);
#endif
	cin >> n >> K;
	for (int i = 1; i <= K; ++i)
	{
		int u, v;
		cin >> u >> v;
		to[u] = v;
		to[v] = u;
		sum[u]++;
		sum[v]++;
	}
	n <<= 1;
	for (int i = 1; i <= n; ++i)
		sum[i] += sum[i - 1];
	g[0] = 1;
	for (int i = 2; i <= n; i += 2) 
		g[i] = 1ll * (i - 1) * g[i - 2] % mod;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = i + 1; j <= n; j += 2)
		{
			bool flag = 0;
			for (int k = i; k <= j; ++k)
				if (to[k] and (to[k] < i || to[k] > j))
				{
					flag = 1;
					break;
				}
			if (flag)
				continue;
			dp[i][j] = g[j - i + 1 - sum[j] + sum[i - 1]];
			for (int l = i + 1; l < j; l += 2)
				pls(dp[i][j], -1ll * dp[i][l] * g[j - l- sum[j] + sum[l]] % mod);
		}
	}
	K <<= 1;
	int ans(0);
	for (int i = 1; i <= n; ++i)
		for (int j = i + 1; j <= n; j += 2)
			pls(ans, 1ll * dp[i][j] * g[n - (j - i + 1) - (K - (sum[j] - sum[i - 1]))] % mod);
	cout << ans << endl;
    return 0;
}
posted @ 2019-08-31 17:40  茶Tea  阅读(365)  评论(0编辑  收藏  举报