Codeforces 367E Sereja and Intervals (dp + sqrt)

本文通过C++实现了一种动态规划算法,用于解决特定条件下的组合计数问题。该算法利用了预处理和状态转移方程,在给定限制条件下高效地计算出可能的状态数目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <algorithm>  
#include <queue>  
#include <set>  
#include <ctime>  
#include <cstdlib>  
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值