正文
增加 195 行删除 2 行
73 行未改动7475767778798081332 行未改动全部展开414415416417418419420421524 行未改动全部展开946947948949950951952953376 行未改动全部展开13301331133213331334133513361337791 行未改动全部展开21292130213121322133213421352136396 行未改动全部展开2533253425352536253725382539254025412542760 行未改动全部展开73 行未改动- 一般地,记 $p_{i,j}$ 为 $X=i,Y=j$ 的概率,则 $H(X,Y)=\sum p_{i,j} \log_2 p_{i,j}$。定义互信息 $I(X,Y)=H(X)+H(Y)-H(X,Y)$,用于衡量 $X,Y$ 之间的相关程度。## 还原序列 / 序列找点:::::warning[注意]{open}一定要看好是序列还是排列!!!332 行未改动全部展开```::::### [P12202 [COI 2022] 回文子串 / Madioničar](https://www.luogu.com.cn/problem/P12202)> 这是一道交互题。>524 行未改动全部展开```:::::### [ABC355E Guess the Sum](https://www.luogu.com.cn/problem/AT_abc355_e)> 这是一道交互题。>376 行未改动全部展开cout << "!" << endl, cin >> q;for (int l, r; q--; ) cin >> l >> r, cout << ans[l][r] << endl;}```:::::### 【未完成】[QOJ18206 Guess the Permutation](https://qoj.ac/problem/18206)791 行未改动全部展开```:::::### [CF1061F Lost Root](https://www.luogu.com.cn/problem/CF1061F)> 这是一道交互题。>396 行未改动全部展开```:::::### [P10539 [APIO2024] 魔术表演](https://www.luogu.com.cn/problem/P10539)> 这是一道通信题。>760 行未改动全部展开73 行未改动74757677787980818283332 行未改动全部展开416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485524 行未改动全部展开10101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077376 行未改动全部展开145414551456145714581459146014611462791 行未改动全部展开225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331396 行未改动全部展开27282729273027312732273327342735760 行未改动全部展开73 行未改动- 一般地,记 $p_{i,j}$ 为 $X=i,Y=j$ 的概率,则 $H(X,Y)=\sum p_{i,j} \log_2 p_{i,j}$。定义互信息 $I(X,Y)=H(X)+H(Y)-H(X,Y)$,用于衡量 $X,Y$ 之间的相关程度。对于一类交互问题,可以用信息熵对可行决策进行估价,选择最优决策。## 还原序列 / 序列找点:::::warning[注意]{open}一定要看好是序列还是排列!!!332 行未改动全部展开```::::### [P9599 [JOI Open 2018] 木琴 / Xylophone](https://www.luogu.com.cn/problem/P9599)> 这是一道交互题。>> 交互库隐藏了一个 $1\sim n$ 的排列 $a_i$。保证 $1$ 所在位置小于 $n$ 所在位置。你可以向交互库询问区间极差。>> 还原这个排列。>> $n \le 5 \times 10^3$,满分询问次数 $10^4$。:::::success[题解]询问 $(i,i+1)$ 可以得到差分数组 $|d_i|=|a_i-a_{i-1}|$。询问 $(i-1,i,i+1)$ 得到 $k$ 分讨:- 若 $a_{i-1} <a_i<a_{i+1}$,则 $k=a_{i+1}-a_{i-1}$。注意到 $d_{i+1}+d_i=k$ 且 $d_{i+1} >0 \land d_i >0$。- 若 $a_{i-1}>a_i>a_{i+1}$,则 $k=a_{i-1}-a_{i+1}$。注意到 $-d_{i+1}-d_i=k$ 且 $d_{i+1} < 0 \land d_i < 0$。- 若 $a_{i-1}<a_i>a_{i+1}$,则 $d_i > 0 \land d_{i+1}<0$。- 若 $a_{i-1}<a_{i+1}$,则 $k=a_i-a_{i-1}=d_i$。- 若 $a_{i+1}<a_{i-1}$,则 $k=a_i-a_{i+1}=-d_{i+1}$。- 若 $a_{i-1}>a_i<a_{i+1}$,则 $d_i< 0 \land d_{i+1}>0$。- 若 $a_{i-1}>a_{i+1}$,则 $k=a_{i-1}-a_i=-d_i$。- 若 $a_{i+1}>a_{i-1}$,则 $k=a_{i+1}-a_i=d_{i+1}$。综上所述,当且仅当 $k=|d_i|+|d_{i+1}|$ 时 $d_id_{i+1}>0$。钦定 $a_1=C \land d_2=\pm |d_2|$ 递推出 $a_1 \sim a_n$,则最小值对应 $1$,进而排列得以还原。在两种情况中取 $\text{pos}(1)<\text{pos}(n)$ 的那种即可。询问次数 $2n-3$。::::::::::info[代码]```cpp line-numbers#define ask queryint d[N], f[N], a[N];void solve(int N) {for (int i = 2; i <= N; i++) d[i] = ask(i-1, i);for (int i = 2; i < N; i++)f[i] = (ask(i-1, i+1) == d[i] + d[i+1]);for (int i = 2; i < N; i++) {if (f[i]) d[i+1] = d[i] > 0 ? d[i+1] : -d[i+1];else d[i+1] = d[i] > 0 ? -d[i+1] : d[i+1];}for (int i = 2; i <= N; i++) a[i] = a[i-1] + d[i];int p = min_element(a + 1, a + N + 1) - a;for (int i = 1; i <= p; i++) a[i] += 1 - a[p];for (int i = p+1; i <= N; i++) a[i] = a[i-1] + d[i];int x1 = find(a + 1, a + N + 1, 1) - a;int xn = find(a + 1, a + N + 1, N) - a;if (x1 > xn)for (int i = 1; i <= N; i++) a[i] = N - a[i] + 1;for (int i = 1; i <= N; i++) answer(i, a[i]);}```:::::### [P12202 [COI 2022] 回文子串 / Madioničar](https://www.luogu.com.cn/problem/P12202)> 这是一道交互题。>524 行未改动全部展开```:::::### [CodeChef UWCOI20F](https://www.codechef.com/problems/UWCOI20F)> 这是一道交互题。>> 交互库隐藏了一个长为 $n$ 的字符串 $S$。每次询问给出下标 $i$,询问 $z[i]$ 的值,其中 $z$ 是 Z 函数 $z[i]=\text{lcp}(S,S[i:])$。>> 找到最大的 $q$ 使得 $\exists T, S=T^q$。>> $n \le 10^6$,满分询问次数 $9$。:::::success[题解]只要求出 $S$ 的最小整周期 $k \mid n$,返回 $q=\frac{n}{k}$ 即可。根据 Z 函数的性质,我们有 $k \in \text{per}(S) \iff k+z[k]=n$。由周期性质 $k \in \text{per}(S) \land i \in \mathbb{N} \implies i \times k\in \text{per}(S)$。对 $n$ 进行质因数分解 $n=p_1^{\alpha_1} p_2^{\alpha_2}\cdots$,则 $k$ 必能表示为 $p_1^{\beta_1} p_2^{\beta_2}\cdots$ 的形式。取固定的 $i$,注意到 $k$ 的合法性关于 $\beta_i$ 单调,令其余 $\beta_j=\alpha_j$,只对 $\beta_i$ 二分求解。可以证明询问次数 $\sum \log \alpha_i\le 9$。::::::::::info[代码]```cppint mpow(int a, int b) {int res = 1;for (; b; b >>= 1) (b & 1) ? (res *= a) : a, a *= a;return res;}int solve(int n, int q) {auto check = [&](int x) -> bool {if (x == n) return true;return x + query(x) == n;};umap<int, int> a; int x = n;for (int i = 2; 1LL * i * i <= x; i++) {if (x % i) continue;while (x % i == 0) a[i]++, x /= i;}if (x != 1) a[x]++;int res = 1;for (auto [p, c] : a) {int b = n / mpow(p, c);int lo = 0, hi = c, u = 0;while (lo <= hi) {int mid = (lo + hi) >> 1;if (check(b * mpow(p, mid))) u = mid, hi = mid - 1;else lo = mid + 1;}res *= mpow(p, u);}return n / res;}```:::::### [ABC355E Guess the Sum](https://www.luogu.com.cn/problem/AT_abc355_e)> 这是一道交互题。>376 行未改动全部展开cout << "!" << endl, cin >> q;for (int l, r; q--; ) cin >> l >> r, cout << ans[l][r] << endl;}```:::::### 【未完成】[QOJ18206 Guess the Permutation](https://qoj.ac/problem/18206)791 行未改动全部展开```:::::### [QOJ23600 Invisible Edges](https://qoj.ac/problem/23600)> 这是一道通信题。>> - A:给定一个 $n$ 个点的无根森林,需要传递序列 $a_1 \sim a_n$,值域 $[1,2n]$。> - B:给定 $n$ 和标号打乱的 $a$ 序列,还原森林的所有边。>> 多测,$\sum n \le 2 \times 10^5$。:::::success[题解]利用括号序和有标号有根森林双射。对于 A,任意定根后 DFS 得到括号序,并将 $u$ 的左括号下标作为 $a_u$。B 收到 $b_1 \sim b_n$ 表示,新标号为 $i$ 的点,左括号下标是 $b_i$。将 $b_i$ 的位置设为 `(`,其余位置为 `)`,B 即可还原括号串。从左往右扫,维护一个栈,容易还原森林形态。::::::::::info[代码]A:```cpp line-numbersconst int N = 2e5 + 5;int n, m, num, a[N], vis[N];vector<int> e[N];void dfs(int u, int fa) {vis[u] = true, a[u] = ++num;for (int v : e[u]) if (v != fa) dfs(v, u);++num;}void _main() {cin >> n >> m;for (int i = 1; i <= n; i++) e[i].clear();for (int i = 1, u, v; i <= m; i++)cin >> u >> v, e[u].pb(v), e[v].pb(u);num = 0, fill(vis + 1, vis + n + 1, 0);for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i, -1);for (int i = 1; i <= n; i++) cout << a[i] << " \n"[i == n];}```B:```cpp line-numbersconst int N = 4e5 + 5;int n, b[N], s[N], fa[N], id[N];void _main() {cin >> n;fill(s + 1, s + 2*n + 1, 1);for (int i = 1; i <= n; i++) cin >> b[i], s[b[i]] = 0, id[b[i]] = i;vector<int> st;for (int i = 1; i <= 2*n; i++) {if (s[i] == 0) fa[i] = st.empty() ? 0 : st.back(), st.pb(i);else st.pop_back();}vector<pair<int, int>> e;for (int i = 1; i <= n; i++)if (fa[b[i]]) e.pb(i, id[fa[b[i]]]);cout << e.size() << '\n';for (auto [u, v] : e) cout << u << ' ' << v << '\n';}```:::::### [CF1061F Lost Root](https://www.luogu.com.cn/problem/CF1061F)> 这是一道交互题。>396 行未改动全部展开```:::::### [P10539 [APIO2024] 魔术表演](https://www.luogu.com.cn/problem/P10539)> 这是一道通信题。>760 行未改动全部展开