Codeforces 367E Sereja and Intervals (dp + sqrt)

#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
using namespace std;  


#define inf 0x3f3f3f3f
#define N 100020
#define LL long long
#define mod 1000000007
#define ls (i << 1)
#define rs (ls | 1)
#define md (ll + rr >> 1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define B 350

int n, m, X;
int dp[40000020];

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

int &f(int i, int x, int y) {

	int t = i * (n + 2) * (n + 2) + x * (n + 2) + y;
	return dp[t];
}
int main() {
	scanf("%d%d%d", &n, &m, &X);
	if(n > m) {
		puts("0");
		return 0;
	}
	dp[0] = 1;
	for(int i = 1; i <= m; ++i) {
		for(int x = 0; x <= i && x <= n; ++x) {
			for(int y = 0; y <= x; ++y) {
				if(i == X) {
					if(y > 0) add(f(i, x, y), f(i - 1, x - 1, y - 1));
					else f(i, x, y) = 0;
					if(x > 0) add(f(i, x, y), f(i - 1, x - 1, y));
				}
				else {
					add(f(i, x, y), f(i - 1, x, y + 1));
					if(y > 0) add(f(i, x, y), f(i - 1, x - 1, y - 1));
					add(f(i, x, y), f(i - 1, x, y));
					if(x > 0) add(f(i, x, y), f(i - 1, x - 1, y));
				}
			}
		}
	}
	int ans = f(m, n, 0);
	for(int i = 1; i <= n; ++i) ans = 1LL * ans * i % mod;
	printf("%d\n", ans);
	return 0;
}

你可能感兴趣的:(codeforces,组合计数)