WC simulation (1.12) Line segment tree of T3 small C

Line segment tree of small C

Topic background:

1.12 WC Simulation T3

Analysis: DP

 

It is said that this thing is the T1 in this field.... There is only one nm 2 violence in the examination room , that is, directly defining f[i][l][r] means that the current is the i -th interval, and the above A parenthesis is [l, r) , which passes the k = 1 part.

Consider the scalar calculation, notice that when n > m , there is no legal sequence of operations, so because nm <= 100000 , then n < 320 , we can regard the interval [l, r) as a pair of parentheses, and the left parenthesis is in l , the right bracket is in r , then for a position i , its final value is the number of left brackets on the left minus the number of right brackets, so we set dp[i][l][r] to indicate that the current position is i , the number of solutions with l left brackets on the left and r right brackets on the left, f[i][l][r] indicates that the current position is i , there are l left brackets on the left, and r right brackets on the left contribute And, every time f[i][l][r] += dp[i][l][r] * (l - r) k , transfer f , dp transfer only need to enumerate directly at the position of i + 1 Place left parentheses, right parentheses, left parentheses and right parentheses, or leave them alone. There are some special judgments for specific transfers, see the code for details.

 

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 years = 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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325577544&siteId=291194637