#include<cstdio> usingnamespace std; template <classType> voidread(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <classType, class ...rest> voidread(Type &x, rest &...y){ read(x), read(y...); } template <classType> voidwrite(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedeflonglong LL; constint N = 30, M = 20; int n, m; LL f[N][4][4][M][M]; intmain() { read(n, m); if (n < 3) returnputs("0"), 0; for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) if (i ^ j) ++f[2][i][j][0][0]; for (int p = 3; p <= n; ++p) for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) for (int k = 0; i ^ j && k < 4; ++k) for (int s = 0; j ^ k && s <= m; ++s) for (int t = 0; t < m; ++t) f[p][i][j][s + (j > k && j > i)][t + (j < k && j < i)] += f[p - 1][j][k][s][t]; LL res = 0; for (int i = 0; i < 4; ++i) for (int j = 0; j < 4; ++j) res += f[n][i][j][m][m - 1]; write(res); return0; }
注意到满足题目要求的个数的性质,重新设计状态,$f _ {p, i, k, j}$ 表示当前在第 $p$ 个位置,当前选择了 $i$ ,考虑处于第 $k$ 个峰的部分,$j = 0$ 表示下降,$j = 1$ 表示上升。写出转移方程,复杂度 $O(n ^ 2)$ : $$ \begin {cases} f _ {p, i, k, 0} = \sum _ {j > i} f _ {p - 1, j, k, 0} + f _ {p - 1, j, k , 1} \\ f _ {p, i, k, 1} = \sum _ {j < i} f _ {p - 1, j, k, 1} + f _ {p - 1, j, k - 1, 0} \end {cases} $$