ColoringBalls

· · 题解

ARC089F ColoringBalls

题意

题解

考虑一个暴力做法:枚举最终的小球颜色序列,判断是否合法。

考虑如何判断,以 BRBRBWRRBRBBWWRRRRWRBBWWRRBBBR 为例:

可以发现我们枚举最终的小球颜色序列,会有非常多等价的情况。

由于每一段是相互独立的,因此我们把一个小球颜色序列无序地写成一个序列,表示每一段所在组的编号(例子中的 BRBRBWRRBRBBWWRRRRWRBBWWRRBBBR 写成的序列为 1,2,2,3,4),则写成的序列相同的是等价的。

这个序列类似整数拆分,所以其实可能的序列很少,在 n = 70 的时候也只有 418662 种。

最后的问题是,对于一个合法的序列,如何计算其对应了多少个最终的小球颜色序列呢?

依然以 1,2,2,3,4 为例,首先是一个多重组合数 \binom {5}{1,2,1,1},然后乘上一个方案数。

时间复杂度 $\mathcal O(能过)$。 ## 代码 ```cpp int n, k; string s; modint ans; inline bool pd(vi p) { vector<bool> v(k, 0); vi t(p.size(), -1); for (ui i = 0; i < p.size(); i++) { while (++t[i] < k && (v[t[i]] || s[t[i]] == 'b')); if (t[i] == k) return 0; v[t[i]] = 1; } while (p.size() && p.back() == 1) p.pop_back(); for (ui i = 0; i < p.size(); i++) { while (++t[i] < k && (v[t[i]] || s[t[i]] == 'r')); if (t[i] == k) return 0; v[t[i]] = 1; } while (p.size() && p.back() == 2) p.pop_back(); for (ui i = 0; i < p.size(); i++) for (int j = 2; j < p[i]; j++) { while (++t[i] < k && v[t[i]]); if (t[i] == k) return 0; v[t[i]] = 1; } return 1; } void dfs(int o, int l, int r, vi p) { if (!o) { if (!pd(p)) return; modint now = binom(n - l + r - 1, r - 1); while (p.size()) { int t = 0, x = p.back(); while (p.size() && p.back() == x) ++t, p.pop_back(); now *= binom(p.size() + t, t); } return ans += now, void(); } while (1) { dfs(o - 1, l, r, p); l += (o == 1 ? 1 : 2 * o - 3) + (l != 0); if (l > n) return; r += 2 * o; p.pb(o); } } int main() { rd(n, k), rds(s); init(1e6); vi p; dfs((n + 3) >> 1, 0, 1, p); print(ans); return 0; } ```