题解:P12989 [GCJ 2022 #1B] ASeDatAb
大爆标,提供一个 \(13\) 次的做法。
考虑充分利用返回的 \(\text{popcount}\):设原串的 \(\text{popcount}\) 为 \(c\),查询后新串的 \(\text{popcount}\) 为 \(c'\),查询串的 \(\text{popcount}\) 为 \(t\),原串和查询串的交的 \(\text{popcount}\) 为 \(x\),那么可以解出来 \(x=\dfrac{c+t-c'}{2}\)。下文中的查询操作都表示利用这个性质求出 \(x\bmod{2}\) 的值。
一个前后四位相同的串循环位移后前后四位依然相同,据此考虑折半,如果我们能把原串操作成前后四位相同,就只需递归下去确定这四位。设当前串的位从低到高依次为 \(a_0,\cdots,a_7\),再设 \(d_i=a_i\oplus a_{i+4}(0\leq i<4)\),我们的目标是把 \(d\) 全部操作成 \(0\)。
先操作 \(\texttt{00000000}\) 得到 \(d_0\oplus d_1\oplus d_2\oplus d_3\),若值为 \(1\),则操作 \(\texttt{00000001}\) 把异或和变成 \(0\)。
考虑隔位配对,\(d_0\oplus d_1\oplus d_2\oplus d_3=0\Leftrightarrow d_0\oplus d_2=d_1\oplus d_3\)。我们查询 \(\texttt{01010101}\) 即可得到 \(d_0\oplus d_2\),若值为 \(1\),则操作 \(\texttt{00000011}\) 使得 \(d_0=d_2,d_1=d_3\)。
现在我们尝试让 \(d_0,d_1\) 相等。我们查询 \(\texttt{00110011}\) 即可得到 \(d_0\oplus d_1\),若值为 \(1\),则操作 \(\texttt{00000101}\) 使得 \(d_0=d_1\)。
现在 \(d_0=d_1=d_2=d_3\)。我们查询 \(\texttt{00010001}\) 得到 \(d_0\),若值为 \(1\),则操作 \(\texttt{00001111}\) 使得 \(d\) 全部变成 \(0\)。
现在我们可以折半下去了,注意折半前得到的 \(\text{popcount}\) 除以 \(2\) 后即可得到折半后 \(d\) 的异或和。对于一个四位串,若 \(d_0\oplus d_1=1\),我们先操作 \(\texttt{0001}\) 使得 \(d_0=d_1\)。接下来查询 \(\texttt{0101}\) 得到 \(d_0\),若值为 \(1\),则操作 \(\texttt{0011}\) 使得 \(d\) 全部变成 \(0\)。
对于一个二位串,若 \(d_0=1\),我们操作 \(\texttt{01}\) 即可把 \(d_0\) 变成 \(0\)。
若最终还没有清零,说明当前串是 \(\texttt{11111111}\),操作 \(\texttt{11111111}\) 即可。
这样就得到了最坏 \(13\) 步的做法。
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128;
using ui = unsigned int;
using ull = unsigned long long;
using u128 = unsigned __int128;
using ld = long double;
using pii = pair<int, int>;
template<typename T> T lowbit(T x) { return x & -x; }
template<typename T> void chkMin(T &x, T y) { x = y < x ? y : x; }
template<typename T> void chkMax(T &x, T y) { x = x < y ? y : x; }
constexpr int lg2(ui x) { return 31 ^ __builtin_clz(x); }
constexpr int lg2(ull x) { return 63 ^ __builtin_clzll(x); }
int tc;
void solve() {
auto query = [&](string s) {
int len = s.size();
while (s.size() != 8) s += s;
cout << s << endl;
int res;
cin >> res;
return res / (8 / len);
};
int c = query("00000000");
if (c & 1) {
c = query("00000001");
if (!c) return;
}
auto oper = [&](const string &s1, const string &s2) -> bool {
int nc = query(s1);
if (!nc) return true;
int x = c + ranges::count(s1, '1') - nc >> 1;
c = nc;
if (x & 1) {
c = query(s2);
if (!c) return true;
}
return false;
};
if (oper("01010101", "00000011")) return;
if (oper("00110011", "00000101")) return;
if (oper("00010001", "00001111")) return;
c >>= 1;
if (c & 1) {
c = query("0001");
if (!c) return;
}
if (oper("0101", "0011")) return;
c >>= 1;
if (c & 1) {
c = query("01");
if (!c) return;
}
query("11111111");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> tc;
while (tc--) solve();
return 0;
}

浙公网安备 33010602011771号