WC模擬(1.12) T3 小C的線段樹

小C的線段樹

題目背景:

1.12 WC模擬T3

分析:DP

 

據說,這個玩意兒纔是本場T1······,考場上只想到一個nm2的暴力,就是,直接定義f[i][l][r]表示當前是第i個區間,上一個括號是[l, r),這樣過掉了k = 1的部分。

考慮標算,注意到當n > m時,是不存在合法操作序列的,所以因爲nm <= 100000,那麼n < 320,我們可以把區間[l, r),看成一對括號,左括號在l,右括號在r,那麼對於一個位置i,它最終的值就是它左邊的左括號數量減去右括號數量的,所以我們設dp[i][l][r]表示,當前位置是i,左邊有l個左括號,左邊有r個右括號的方案數,f[i][l][r]表示,當前位置是i,左邊有l個左括號,左邊有r個右括號的貢獻和,每一次f[i][l][r] += dp[i][l][r] * (l - r)k,轉移fdp轉移只需要直接枚舉在i + 1的位置上放置左括號,右括號,左括號和右括號,或者不放置就可以了。具體轉移存在一些特判,具體見代碼。

 

Source:

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>

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<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

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

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

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

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 350 + 10;
const int mod = 998244353;
int ans, n, m, k;
int f[2][MAXN][MAXN], dp[2][MAXN][MAXN], num[MAXN];

inline int mod_pow(int a, int b) {
	int ans = 1;
	for (; b; b >>= 1, a = (long long)a * a % mod)
		if (b & 1) ans = (long long)ans * a % mod;
	return ans;
}

inline void add(int &x, int t) {
	x += t, (x >= mod) ? (x -= mod) : (0);
}

inline void solve() {
	R(n), R(m), R(k), dp[0][0][0] = 1;
	if (n > m) std::cout << "0", exit(0);
	for (int i = 1; i <= n; ++i) num[i] = mod_pow(i, k);
	for (int i = 1, x = 1; i <= m; ++i, x ^= 1) {
		for (int l = 0; l <= n; ++l)
			for (int r = 0; r <= l; ++r) {
				dp[x][l][r] = f[x][l][r] = 0;
				add(dp[x][l][r], dp[x ^ 1][l][r]);
				add(f[x][l][r], f[x ^ 1][l][r]);
				if (l) {
					add(dp[x][l][r], dp[x ^ 1][l - 1][r]);
					add(f[x][l][r], f[x ^ 1][l - 1][r]);
				}
				if (r) {
					add(dp[x][l][r], dp[x ^ 1][l][r - 1]);
					add(f[x][l][r], f[x ^ 1][l][r - 1]);
				}
				if (l && r) {
					add(dp[x][l][r], dp[x ^ 1][l - 1][r - 1]);
					add(f[x][l][r], f[x ^ 1][l - 1][r - 1]);
				}
				add(f[x][l][r], (long long)dp[x][l][r] * num[l - r] % mod);
			}
	}
	std::cout << f[m & 1][n][n];			
}

int main() {
	freopen("segment.in", "r", stdin);
	freopen("segment.out", "w", stdout);
	solve();
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章