跳到正文

题解

God-Like Problem

Tomwsc约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年6月16日
收录
2026年10月3日
更新
2026年11月8日
篇幅
约 7 分钟
获赞
1
题目
P15496

版本

  1. v1当前

很牛的题。

容易发现 1−0=11-0=1 的限制非常强,而我们显然可以将先拆成最大的两个部分即 (⋯ )−(⋯ )(\cdots)-(\cdots),同时这两个部分仍然可以继续拆分。故我们可以考虑递归地去实现这个东西。要想找到分割点也是容易的,只需要令第一个位置为 11,然后从最后开始往前试 11,第一次使答案为 11 时便可以得到这个分割点。

但是这样显然询问次数会爆炸,次数上限应该是 n2n^2 的。考虑到从中间找分割点需要的信息太多了,没有前途。于是考虑从前往后增量求解。

注意到,我们需要求解的表达式可以拍到一棵树上,而该树的中序遍历就是原来的表达式。每次新增一个未知量 i+1i+1 就相当于在 ii 到根的路径上新增一个节点,然后将 i+1i+1 挂到其右边。我们思考如何找到新建的节点所在的边。

首先,考虑一个二分的做法。假设我们判定其是否在 (u,sonu)(u, son_u) 这条边以上,则我们令在 uu 到根的所有子树的运算结果都为 11,否则为 00。因为 uu 到根的子树的中的 11 的奇偶性会影响最终的运算结果,所以我们不妨把新增的节点也赋成 11。此时,若其在这条边以下,则对运算结果没有影响,否则会使答案异或一。需要注意的是,子树个数的奇偶性是有极大影响的,所以我们需要先进行询问找出新增的节点是在奇数层还是偶数层,然后在这些边上二分。判断奇偶的具体方法便是直接全部子树和新增的点均赋值成 11 然后询问。

考虑将二分优化掉。发现每次只会在极右链新增一的深度,因此势能是定的。故我们可以每次直接暴力往上跳,因为有奇偶性的限制,这部分变成了 n2\frac{n}{2}。

故总数为 2.5n2.5n(判断奇偶性;判断是否需要往上跳;暴力往上跳)。

注意表达式树需要一个虚根。

Code
C++
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using i128 = __int128;

using PII = pair<int, int>;
using PLL = pair<ll, ll>;

#define fi first
#define se second

constexpr ll inf = (1ll << 62);
constexpr int N = 2e5 + 10;

template <typename T> void chkmax(T& a, T b) { a = max(a, b); }
template <typename T> void chkmin(T& a, T b) { a = min(a, b); }

void solve() {
	int n, lst = 2;
	cin >> n;
	int tot = n + 2;
	stack<int> st; st.push(n + 1), st.push(n + 2), st.push(2);
	vector<int> l(2 * n + 2), r(2 * n + 2), leaf;
	r[n + 1] = n + 2, l[n + 2] = 1, r[n + 2] = 2;
	leaf.emplace_back(1); leaf.emplace_back(2);
	function<void(int)> print = [&](int u) {
		if (l[u]) cout << "(", print(l[u]);
		if (u <= n) cout << "x";
		if (u > n) cout << "-";
		if (r[u]) print(r[u]), cout << ")";
	};
	int cnt = 0;
	for (int i = 3; i <= n; i++) {
		vector<int> qry(n + 1);
		cnt += 2;
		cout << "query ";
		for (auto j : leaf) qry[j] = 1; qry[i] = 1;
		for (int j = 1; j <= n; j++) cout << qry[j]; cout << endl;
		int flag;
		cin >> flag;
		if (!((int(leaf.size()) ^ flag) & 1)) st.pop(), leaf.pop_back();
		fill(qry.begin(), qry.end(), 0);
		cout << "query ";
		for (int j = 0; j < int(leaf.size()) - 2; j++) qry[leaf[j]] = 1; qry[i] = 1;
		for (int j = 1; j <= n; j++) cout << qry[j]; cout << endl;
		int x;
		cin >> x;
		// for (auto i : leaf) cout << i << " "; cout << endl;
		if (!((x ^ max(int(leaf.size()) - 2, 0)) & 1)) {
			// cerr << i << "\n";
			l[++tot] = st.top(), r[tot] = i, st.pop(), r[st.top()] = tot, st.push(tot), st.push(i), leaf.emplace_back(i);
		} else {
			// cerr << "Cinema\n";
			while (st.size() > 1) {
				fill(qry.begin(), qry.end(), 0);
				st.pop(), st.pop(), leaf.pop_back(), leaf.pop_back();
				cnt++;
				assert(cnt <= 500);
				cout << "query ";
				for (int j = 0; j < int(leaf.size()) - 2; j++) qry[leaf[j]] = 1; qry[i] = 1;
				for (int j = 1; j <= n; j++) cout << qry[j]; cout << endl;
				cin >> x;
				if (!((x ^ max(int(leaf.size()) - 2, 0)) & 1)) {
					l[++tot] = st.top(), r[tot] = i, st.pop(), r[st.top()] = tot, st.push(tot), st.push(i), leaf.emplace_back(i);
					break;
				}
			}
			if (st.size() == 1) l[++tot] = r[st.top()], r[st.top()] = tot, r[tot] = i, st.push(tot), st.push(i), leaf.emplace_back(i);
		}
		// print(r[n + 1]); cout << endl;
	}
	// cout << r[n + 1] << " " << r[r[n + 1]] << "\n"; return;
	cout << "answer ";
	print(r[n + 1]);
}

int main() {
	int t = 1;
	// cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
原文