小C的线段树
题目背景:
分析: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,转移f,dp转移只需要直接枚举在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; }