P14033

· · 题解

来翻译一下题解(写的感觉比原题解还抽象,本人萌新,轻喷)。

首先我们考虑能否查询一个集合里 0 的数量,我们先钦定 s_1 = 0,那么查询 f(S) 和 f(S \mid \{1\}) 我们就可以解出来集合 S 里 0 和 1 的数量。接下来的询问全部表示查询一个集合里 0 的数量。

然后假如我们现在有一个 q 次询问知道长度为 n 的序列的做法,设 S_{i} 表示这个做法的第 i 次操作查询的集合,v(S) 表示 S 内 1 的数量,我们声称我们可以有一个用 2q + 1 次询问知道长度为 2n + q 的序列的做法。

首先将长度为 2n + q 的序列划分成 3 个部分 A, B, C,其中 A 的长度是 n,B 的长度是 n,C 的长度是 q,然后考虑先用一次询问知道 B 中 1 的数量。接下来第 2i 次我们查询 S_{A, i} + S_{B, i} 中有多少个 1,然后第 2i + 1 次操作我们查询 S_{A, i} + (B - S_{B, i}) + C_i 中有多少个 1。其中 S_{A, i} 表示 A 序列中下标在 S_i 内的集合,S_{B, i} 表示 B 序列中下标在 S_i 内的集合,C_i 表示 C 序列第 i 个元素。

也就是我们查询了 v(S_{A, i}) + v(S_{B, i}) 和 v(S_{A, i}) - v(S_{B, i}) + C_i。首先根据这两个的和的奇偶性我们可以知道 C_i 的值(两个的和是 2v(S_{A, i}) + C_i,又因为 C_i\in \{0, 1\}),然后我们知道 v(S_{A, i}) + v(S_{B, i}) 和 v(S_{A, i}) - v(S_{B, i}),于是我们可以知道 v(S_{A, i}), v(S_{B, i})。然后我们就可以递归还原 A 和 B 了。

这样的话初始 n = 1, q = 0,我们发现可以迭代到 n = 1025, q = 255,然后我们只需要 2q + 2 = 512 次操作即可,可以拿到 76 pts。

:::info[感觉翻译的还是不到位,如果看不懂就看代码吧]

// Start: 2025-09-15 10:24:39
#include <bits/stdc++.h>
#define For(i, j, k) for(int i = (j); i <= (k); ++i)
#define ForDown(i, j, k) for(int i = (j); i >= (k); --i)
#define Debug(fmt, args...) fprintf(stderr, "(func %s, line #%d): " fmt, __func__, __LINE__, ##args), fflush(stderr)
#define debug(fmt, args...) fprintf(stderr, fmt, ##args), fflush(stderr)
#define within :
#define LJY main
using namespace std;
typedef long long ll;
const int N = 1030;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
inline int read() {
    char ch = getchar(); int x = 0,f = 1;
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9')
        x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return x * f;
}
bool debug_tag = 1;
int n; bool vis[N], res[N]; vector<vector<int> > vec[10];
namespace interactive {
    bool a[N];
    void init() {For(i, 1, n) a[i] = rnd() & 1;}
    int query(vector<bool> vis) {
        int cnt[2] = {0, 0};
        For(i, 1, n) if(vis[i - 1]) cnt[a[i]]++; return cnt[0] * cnt[1];
    }
    bool check(vector<bool> res) {For(i, 1, n) if(a[i] != res[i - 1]) {debug("%d\n", i); return 0;} return 1;}
} // 内置交互库
int f[] = {0, 2, 6, 14, 30, 62, 126, 254, 510}, g[] = {1, 2, 5, 13, 33, 81, 193, 449, 1025};
int query(vector<int> vec) { // 查询 vec 集合里 1 的数量
    memset(vis, 0, sizeof(vis)); int len = 0;
    for(int x within vec) vis[x] = 1, len += (x <= n);
    printf("? "); For(i, 1, n) putchar(vis[i] + '0'); puts(""); fflush(stdout);
    int x = debug_tag ? interactive::query(vector<bool>(vis + 1, vis + 1 + n)) : read();
    vis[1] = 1; printf("? "); For(i, 1, n) putchar(vis[i] + '0'); puts(""); fflush(stdout);
    int y = debug_tag ? interactive::query(vector<bool>(vis + 1, vis + 1 + n)) : read();
    For(i, 0, len) if(i * (len - i) == x && (i + 1) * (len - i) == y) return i; assert(0);
}
void dfs(int l, int r, int dep, int sum, vector<int> lis) {
    if(dep == 1) {res[r] = lis[0]; res[l] = (l == 1 ? 1 : sum - res[r]); return;}
    int d = lis.back(), p = lis.size() / 2;
    vector<int> L, R;
    For(i, 0, p - 1) {
        int x = lis[i * 2], y = lis[i * 2 + 1] - d;
    // 还原 c_i 的值
        y -= (res[l + 2 * g[dep - 1] + i] = ((x + y) & 1)); sum -= res[l + 2 * g[dep - 1] + i];
        L.emplace_back((x + y) / 2), R.emplace_back((x - y) / 2); // 还原 v(S_{A, i}) 和 v_(S_{B, i})
    } dfs(l, l + g[dep - 1] - 1, dep - 1, sum - d, L);
    dfs(l + g[dep - 1], l + 2 * g[dep - 1] - 1, dep - 1, d, R);
}
void ljy() {
    n = read(); if(debug_tag) interactive::init();
    vector<int> lis; for(auto x within vec[8]) lis.emplace_back(query(x));
    dfs(1, 1030, 8, -1, lis);
    printf("! "); For(i, 1, n) printf("%d", res[i]); puts(""); fflush(stdout);
    int op = debug_tag ? interactive::check(vector<bool>(res + 1, res + 1 + n)) : read(); if(op) return;
    printf("! "); For(i, 1, n) printf("%d", res[i] ^= 1); puts(""); fflush(stdout);
    op = debug_tag ? interactive::check(vector<bool>(res + 1, res + 1 + n)) : read(); if(op) return;
    assert(0);
}
signed LJY() {
    vec[1] = {{2}};
    For(i, 2, 8) { /*求出每次迭代过程的查询集合 */
        int len = vec[i - 1].size();
        For(j, 0, len - 1) {
            auto x = vec[i - 1][j]; auto y = x; For(j, 1, g[i - 1]) vis[j] = 0;
            for(int p within x) y.emplace_back(p + g[i - 1]), vis[p] = 1; vec[i].emplace_back(y);
            y = x; For(j, 1, g[i - 1]) if(!vis[j]) y.emplace_back(j + g[i - 1]); y.emplace_back(j + 1 + g[i - 1] * 2);
            vec[i].emplace_back(y);
        } vector<int> tmp; For(j, g[i - 1] + 1, g[i - 1] * 2) tmp.emplace_back(j);
        vec[i].emplace_back(tmp);
    } int T = read(); while(T--) ljy();
    return 0;
}

:::

然后考虑优化,我们发现如果我们确定了前 i 项,那么这 i 项里一定有 0 和 1 的出现次数的 \max \ge \lceil \frac i2 \rceil。那么在查询 v(S) 的时候,如果 |S| \le \lceil \frac i2 \rceil,那么我们取出出现次数更大的那个值的所有元素扔进 S 里面去查询,我们就可以确定 0 有多少个 1 有多少个,因为出现次数更大的那个值在这个新的集合里一定比另一个值的出现次数多,所以我们此时可以只用 1 次查询就知道 v(S)。

然后我们考虑初始知道长度为 1 的序列,每次加入一个长度 \le \lceil \frac {len}2 \rceil 的序列,其中 len 表示当前知道的长度,然后用 76 pts 的做法确定这个新的序列。我们用如下的 dp 计算这个操作次数:

然后我们令 f_i 表示用 76 pts 的做法知道一个长度为 i 的序列需要多少次询问 v(S),初始 f_1 = 0,然后转移考虑:

  1. 直接加入一个数,f_i + 1 \to f_{i + 1}。
  2. 考虑 (n, q) \to (2n + q, 2q + 1) 的过程,也就是 2f_i + 1\to f_j(j \le 2i + f_i)。

然后令 g_i 表示知道一个长度为 i 的序列需要多少次询问,每次在后面加入一段,也就是 g_i + f_j + 1\to g_{i + j}(j \le \lceil \frac i2 \rceil),初始 g_1 = 0。

:::info[dp 程序]

// Start: 2025-09-15 12:59:18
#include <bits/stdc++.h>
#define For(i, j, k) for(int i = (j); i <= (k); ++i)
#define ForDown(i, j, k) for(int i = (j); i >= (k); --i)
#define Debug(fmt, args...) fprintf(stderr, "(func %s, line #%d): " fmt, __func__, __LINE__, ##args), fflush(stderr)
#define debug(fmt, args...) fprintf(stderr, fmt, ##args), fflush(stderr)
#define within :
#define LJY main
using namespace std;
typedef long long ll;
const int N = 1005;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
inline int read() {
    char ch = getchar(); int x = 0,f = 1;
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9')
        x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return x * f;
}
int f[N], pf[N], g[N], pre[N];
signed LJY() {
    memset(f, 0x3f, sizeof(f)); f[1] = 0;
    For(i, 1, 1000) {
        if(f[i + 1] > f[i] + 1) f[i + 1] = f[i] + 1, pf[i + 1] = i;
        f[i + 1] = min(f[i + 1], f[i] + 1);
        int p = min(1000, i * 2 + f[i]);
        For(j, i + 1, p) if(f[j] > f[i] * 2 + 1) f[j] = f[i] * 2 + 1, pf[j] = i;
    } memset(g, 0x3f, sizeof(g)); g[1] = 0;
    For(i, 1, 1000) {
        For(j, 1, min(1000 - i, (i + 1) / 2)) {
            int w = g[i] + f[j] + 1;
            if(w < g[i + j]) g[i + j] = w, pre[i + j] = j;
        }
    } cout << g[1000] << endl; 
    return 0;
}

:::

我们发现此时 g_{1000} = 383,也就是在这个做法的情况下我们需要 385 次。

然后我们发现我们不只是有 (n, q) \to (2n + q, 2q + 1) 这种变换,我们也有 (n, q) \to (3n + 2q, 3q + 2) 的变换,也就是我们考虑将长度为 3n + 2q 的序列分成 5 段,长度分别为 n, n, n, q, q,然后先求出 B 和 C 的和,然后每次询问 v(S_{A, i}) + v(S_{B, i}) + v(S_{C, i}), v(S_{A, i}) - v(S_{B, i}) + v(S_{C, i}) + D_i,v(S_{A, i}) - v(S_{C, i}) + E_i,类似之前 76 分的做法我们可以解出 v(S_{A, i}), v(S_{B, i}), v(S_{C, i}), D_i, E_i,然后进行递归。

我们将这个变换加入 f 数组,重新跑 dp,发现 g_{1000} = 382,也就是需要 384 次询问,可以通过。

:::info[Code]

// Start: 2025-09-15 10:24:39
#include <bits/stdc++.h>
#define For(i, j, k) for(int i = (j); i <= (k); ++i)
#define ForDown(i, j, k) for(int i = (j); i >= (k); --i)
#define Debug(fmt, args...) fprintf(stderr, "(func %s, line #%d): " fmt, __func__, __LINE__, ##args), fflush(stderr)
#define debug(fmt, args...) fprintf(stderr, fmt, ##args), fflush(stderr)
#define within :
#define LJY main
using namespace std;
typedef long long ll;
const int N = 1030;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
inline int read() {
    char ch = getchar(); int x = 0,f = 1;
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9')
        x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return x * f;
}
bool debug_tag = 1;
int n; bool vis[N], res[N];
vector<int> vc[2];
namespace interactive {
    bool a[N];
    void init() {For(i, 1, n) a[i] = (rnd() & 1);}
    int query(vector<bool> vis) {
        int cnt[2] = {0, 0};
        For(i, 1, n) if(vis[i - 1]) cnt[a[i]]++; return cnt[0] * cnt[1];
    }
    bool check(vector<bool> res) {For(i, 1, n) if(a[i] != res[i - 1]) {return 0;} return 1;}
}
int f[N], pf[N], g[N], pg[N];
int query(vector<int> vec) { // 查询 vec 集合里 0 的数量
    memset(vis, 0, sizeof(vis)); for(int x within vec) vis[x] = 1; int len = vec.size();
    if(vc[0].size() >= vc[1].size()) {
        assert(len <= vc[0].size());
        for(int id within vc[0]) vis[id] = 1; len += vc[0].size();
        printf("? "); For(i, 1, n) putchar(vis[i] + '0'); puts(""); fflush(stdout);
        int x = debug_tag ? interactive::query(vector<bool>(vis + 1, vis + 1 + n)) : read();
        For(i, 0, vec.size()) if(i * (len - i) == x) {return i;} assert(0);
    } else {
        assert(len <= vc[1].size());
        for(int id within vc[1]) vis[id] = 1; len += vc[1].size();
        printf("? "); For(i, 1, n) putchar(vis[i] + '0'); puts(""); fflush(stdout);
        int x = debug_tag ? interactive::query(vector<bool>(vis + 1, vis + 1 + n)) : read();
        For(i, 0, vec.size()) if(i * (len - i) == x) {return vec.size() - i;} assert(0);
    }
}
vector<int> rd, opt; int len;
vector<vector<int> > vec[N];
void dfs(int l, int r, int dep, int sum, vector<int> lis) { // 递归求答案
    if(dep == 0) {res[l] = sum; return;}
    if(opt[dep] == 0) {
        res[r] = lis.back(); lis.pop_back();
        dfs(l, r - 1, dep - 1, sum -= res[r], lis); return;
    } if(opt[dep] == 1) {
        int d = lis.back(), p = lis.size() / 2;
        vector<int> L, R;
        For(i, 0, p - 1) {
            int x = lis[i * 2], y = lis[i * 2 + 1] - d;
            if(l + 2 * rd[dep - 1] + i <= r) {
                y -= (res[l + 2 * rd[dep - 1] + i] = ((x + y) & 1)); sum -= res[l + 2 * rd[dep - 1] + i];}
            L.emplace_back((x + y) / 2), R.emplace_back((x - y) / 2);
        } dfs(l, l + rd[dep - 1] - 1, dep - 1, sum - d, L);
        dfs(l + rd[dep - 1], min(r, l + 2 * rd[dep - 1] - 1), dep - 1, d, R);
    } else {
        int d = lis[lis.size() - 2], e = lis.back(), p = lis.size() / 3;
        vector<int> L, M, R;
        For(i, 0, p - 1) {
            int x = lis[i * 3], y = lis[i * 3 + 1] - d, z = lis[i * 3 + 2] - e;
            if(l + 3 * rd[dep - 1] + i <= r) {
                y -= (res[l + 3 * rd[dep - 1] + i] = ((x + y) & 1)); sum -= res[l + 3 * rd[dep - 1] + i];}
            M.emplace_back((x - y) / 2); x = (x + y) / 2;
            if(l + 3 * rd[dep - 1] + i + p <= r) {
                z -= (res[l + 3 * rd[dep - 1] + i + p] = ((x + z) & 1)); sum -= res[l + 3 * rd[dep - 1] + i + p];}
            L.emplace_back((x + z) / 2), R.emplace_back((x - z) / 2);
        } dfs(l, l + rd[dep - 1] - 1, dep - 1, sum - d - e, L);
        dfs(l + rd[dep - 1], l + 2 * rd[dep - 1] - 1, dep - 1, d, M);
        dfs(l + 2 * rd[dep - 1], min(r, l + 3 * rd[dep - 1] - 1), dep - 1, e, R);
    }
}
void solve(int l, int r) {
    if(l == r) {res[l] = query({l}); return;}
    int len = r - l + 1, x = len; rd.clear();
    while(x > 1) rd.emplace_back(x), x = pf[x]; rd.emplace_back(1); reverse(rd.begin(), rd.end());
    len = rd.size(); vec[0] = {}; opt.resize(len);
    For(i, 1, len - 1) { // 预处理查询集合
        vec[i].clear(); int R = l + rd[i] - 1;
        if(f[rd[i]] == f[rd[i - 1]] + 1 && rd[i] == rd[i - 1] + 1) {
            opt[i] = 0; vec[i] = vec[i - 1];
            vec[i].emplace_back(vector<int>{rd[i] + l - 1}); 
        } else if(f[rd[i]] == f[rd[i - 1]] * 2 + 1 && rd[i] <= rd[i - 1] * 2 + f[rd[i - 1]]) { // (n, q) -> (2n + q, 2q + 1)
            opt[i] = 1; int len = vec[i - 1].size();
            For(j, 0, len - 1) {
                auto x = vec[i - 1][j]; auto y = x; For(j, 1, rd[i - 1]) vis[j] = 0;
                for(int p within x) vis[p - l + 1] = 1;
                For(j, 1, rd[i - 1])
                    if(vis[j] && j + rd[i - 1] + l - 1 <= R) y.emplace_back(j + rd[i - 1] + l - 1);
                vec[i].emplace_back(y); y = x;
                For(j, 1, rd[i - 1])
                    if(!vis[j] && j + rd[i - 1] + l - 1 <= R) y.emplace_back(j + rd[i - 1] + l - 1);
                if(l + rd[i - 1] * 2 + j <= R) y.emplace_back(l + rd[i - 1] * 2 + j);
                vec[i].emplace_back(y);
            } vector<int> tmp; For(j, rd[i - 1] + 1, rd[i - 1] * 2) if(j + l - 1 <= R) tmp.emplace_back(j + l - 1);
            vec[i].emplace_back(tmp);
        } else { // (n, q) -> (3n + 2q, 3q + 2)
            opt[i] = 2; int len = vec[i - 1].size();
            For(j, 0, len - 1) {
                auto x = vec[i - 1][j]; auto y = x; For(j, 1, rd[i - 1]) vis[j] = 0;
                for(int p within x) vis[p - l + 1] = 1;
                For(j, 1, rd[i - 1]) {
                    if(vis[j] && l + rd[i - 1] + j - 1 <= R) y.emplace_back(l + rd[i - 1] + j - 1);
                    if(vis[j] && l + rd[i - 1] * 2 + j - 1 <= R) y.emplace_back(l + 2 * rd[i - 1] + j - 1);
                } vec[i].emplace_back(y); y = x;
                For(j, 1, rd[i - 1]) {
                    if(!vis[j] && l + rd[i - 1] + j - 1 <= R) y.emplace_back(l + rd[i - 1] + j - 1);
                    if(vis[j] && l + rd[i - 1] * 2 + j - 1 <= R) y.emplace_back(l + 2 * rd[i - 1] + j - 1);
                } if(l + 3 * rd[i - 1] + j <= R) y.emplace_back(l + 3 * rd[i - 1] + j);
                vec[i].emplace_back(y); y = x;
                For(j, 1, rd[i - 1])
                    if(!vis[j] && l + rd[i - 1] * 2 + j - 1 <= R) y.emplace_back(l + 2 * rd[i - 1] + j - 1);
                if(l + 3 * rd[i - 1] + j + len <= R) y.emplace_back(l + 3 * rd[i - 1] + j + len);
                vec[i].emplace_back(y);
            } vector<int> tmp; For(j, rd[i - 1] + 1, rd[i - 1] * 2) if(j + l - 1 <= R) tmp.emplace_back(j + l - 1);
            vec[i].emplace_back(tmp); tmp.clear();
            For(j, 2 * rd[i - 1] + 1, rd[i - 1] * 3) if(j + l - 1 <= R) tmp.emplace_back(j + l - 1);
            vec[i].emplace_back(tmp);
        }
    } vector<int> tmp; For(i, l, r) tmp.emplace_back(i);
    vector<int> lis;
    for(auto x within vec[len - 1]) lis.emplace_back(query(x));
    dfs(l, r, len - 1, query(tmp), lis);
}
void ljy() {
    n = read(); vc[0].clear(), vc[1].clear(); if(debug_tag) interactive::init();
    vc[0].emplace_back(1); memset(res, 0, sizeof(res));
    vector<int> rd; int x = n; while(x > 1) rd.emplace_back(x), x -= pg[x]; reverse(rd.begin(), rd.end());
    for(int t within rd) {solve(x + 1, t); For(i, x + 1, t) vc[res[i]].emplace_back(i); x = t;}
    printf("! "); For(i, 1, n) printf("%d", res[i]); puts(""); fflush(stdout);
    int op = debug_tag ? interactive::check(vector<bool>(res + 1, res + 1 + n)) : read(); if(op) return;
    printf("! "); For(i, 1, n) printf("%d", res[i] ^= 1); puts(""); fflush(stdout);
    op = debug_tag ? interactive::check(vector<bool>(res + 1, res + 1 + n)) : read(); if(op) return;
    assert(0);
}
signed LJY() {
    memset(f, 0x3f, sizeof(f)); f[1] = 0;
    For(i, 1, 1000) {
        int p = min(1000, i * 2 + f[i]);
        For(j, i + 1, p) if(f[j] > f[i] * 2 + 1) f[j] = f[i] * 2 + 1, pf[j] = i;
        p = min(1000, i * 3 + 2 * f[i]);
        For(j, i + 1, p) if(f[j] > f[i] * 3 + 2) f[j] = f[i] * 3 + 2, pf[j] = i;
        if(f[i + 1] > f[i] + 1) f[i + 1] = f[i] + 1, pf[i + 1] = i;
    } memset(g, 0x3f, sizeof(g)); g[1] = 0;
    For(i, 1, 1000) {
        For(j, 1, min(1000 - i, (i + 1) / 2)) {
            int w = g[i] + f[j] + 1;
            if(w < g[i + j]) g[i + j] = w, pg[i + j] = j;
        }
    } int T = read(); while(T--) ljy();
    return 0;
}

:::