NOIP模拟 Round 22 T4 永恒的盛宴 - 题解

· · 题解

Description

有 n 只动物,。现在第 i 只动物想吃第 a_i 只动物(i\ne a_i),且所有的动物都是正常状态。 所有动物按照排列 p 的顺序行动。

第 i 回合:

  1. 如果 a_{p_i} 没被吃掉,且属于虚弱状态,则它被 p_i 吃掉。p_i 状态不变。
  2. 否则 p_i 因饥饿进入虚弱状态。

对于所有排列 p,求期望有多少个动物被吃掉。

Solution

设 t_i 表示 i 行动的时间。

考虑计算贡献,对于环上一个长为 $len$ 的极长 $t$ 下降子段,贡献为 $\lfloor \frac{len}{2}\rfloor$。从段尾开始数,奇数位会被吃掉。 然后可以枚举每个位置所在的极长下降子段,计算贡献。 这样无法拓展,而且组合数不好计算。这是以段为单位计算答案,不妨考虑一个点什么时候能吃掉它想吃的点。 等价于它在一个极长下降子段,从后往前的偶数位。 难点在与极长子段不好计算,考虑容斥。相当于求在以他开头的任意子段中,奇数长度段的方案数减偶数长度的方案数。 ![](https://pic1.imgdb.cn/item/68ee37dfc5157e1a886f0c7d.png) 相当于途中红点不能出现在第一**段**,蓝点不能出现在第二**段**。 dp 时带上 $\sum{d_i}$,表示第 $i$ 段中出现了几个不能出现的点。容斥即可。 另外特判环和链相交的点(详见代码,把那个点度数 -1 跑即可)。 ### Code ```cpp #include <bits/stdc++.h> using namespace std; #define ll long long const int MAXN = 500, mod = 998244353; int fac[3 * MAXN + 5], invfac[3 * MAXN + 5]; int qpow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = (ll)ans * a % mod; a = (ll)a * a % mod; b >>= 1; } return ans; } int C(int n, int m) {if (n < m) return 0; return (ll)fac[n] * invfac[m] % mod * invfac[n - m] % mod;} int n, a[MAXN + 5], c[MAXN + 5]; int f[MAXN + 5][MAXN + 5]; // 前 i 个点,\sum_d = j int t[MAXN + 5]; // 第 i 个点是什么 bool vis[MAXN + 5]; signed main() { #ifndef LOCAL freopen("d.in", "r", stdin); freopen("d.out", "w", stdout); #else freopen("./d/d5.in", "r", stdin); // freopen("code.out", "w", stdout); clock_t BEGIN_TIME = clock(); #endif fac[0] = invfac[0] = 1; for (int i = 1; i <= 3 * MAXN; i ++) fac[i] = (ll)fac[i - 1] * i % mod, invfac[i] = qpow(fac[i], mod - 2); scanf("%d", &n); for (int i = 1; i <= n; i ++) scanf("%d", &a[i]), c[a[i]] ++; for (int i = 1; i <= n; i ++) c[i] --; int ans = 0; for (int ss = 1; ss <= n; ss ++) { t[1] = ss; int x = ss, len = 1; memset(vis, 0, sizeof(vis)); vis[ss] = 1; while (!vis[a[x]]) { t[++ len] = a[x]; x = a[x]; vis[x] = 1; } int s = 0; memset(f, 0, sizeof(f)); f[1][0] = 1; for (int i = 2; i < len; i ++) { s += c[t[i]]; for (int j = 0; j <= s; j ++) for (int d = 0; d <= c[t[i]] && d <= j; d ++) f[i][j] = (f[i][j] + (ll)f[i - 1][j - d] * C(c[t[i]], d) % mod * fac[d] % mod) % mod; int cur = 0; for (int d = 0; d <= s; d ++) if (d & 1) cur = (cur - (ll)f[i][d] * C(i + d + s - d, s - d) % mod * fac[s - d] % mod + mod) % mod; else cur = (cur + (ll)f[i][d] * C(i + d + s - d, s - d) % mod * fac[s - d] % mod) % mod; if (i % 2 == 0) ans = (ans + (ll)cur * invfac[s + i] % mod) % mod; else ans = ((ll)ans - (ll)cur * invfac[s + i] % mod + mod) % mod; } s = 0; c[a[t[len]]] --; memset(f, 0, sizeof(f)); f[1][0] = 1; for (int i = 2; i <= len; i ++) { s += c[t[i]]; for (int j = 0; j <= s; j ++) for (int d = 0; d <= c[t[i]] && d <= j; d ++) f[i][j] = (f[i][j] + (ll)f[i - 1][j - d] * C(c[t[i]], d) % mod * fac[d] % mod) % mod; int cur = 0; for (int d = 0; d <= s; d ++) if (d & 1) cur = (cur - (ll)f[i][d] * C(i + d + s - d, s - d) % mod * fac[s - d] % mod + mod) % mod; else cur = (cur + (ll)f[i][d] * C(i + d + s - d, s - d) % mod * fac[s - d] % mod) % mod; if (i == len) if (i % 2 == 0) ans = (ans + (ll)cur * invfac[s + i] % mod) % mod; else ans = ((ll)ans - (ll)cur * invfac[s + i] % mod + mod) % mod; } c[a[t[len]]] ++; } cout<<ans; #ifdef LOCAL clock_t END_TIME = clock(); cerr<<endl<<fixed<<setprecision(3)<<(double)(END_TIME - BEGIN_TIME) / CLOCKS_PER_SEC; #endif return 0; } ```