题解:P17115 [Algo Beat 009 & MROI-R1] ANDOR

· · 题解

不知道官解在干什么。

调用经典结论 (x \operatorname{or} y) + (x \operatorname{and} y)=x+y,证明考虑二进制位下的加法。

由于只能问线性次,肯定想拿一个东西出来问很多次,考虑使用 2n-2 次询问得出 a_1+a_2,a_1+a_3,\cdots,a_1+a_n,全部加起来得到 (n-2)a_1+\sum\limits_{i=1}^{n}a_i=S,故 a_1=\dfrac{S-\frac{(n-1)n}{2}}{n-2},整个序列也就都知道了。

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> k; int sum = 0;
    for (int i = 2, x, y; i <= n; i++) {
        cout << "? and " << 1 << ' ' << i << endl; cin >> x;
        cout << "? or " << 1 << ' ' << i << endl; cin >> y;
        c[i] = x + y; sum += c[i];
    } a[1] = (sum - n * (n - 1) / 2) / (n - 2); cout << "! " << a[1] << ' ';
    for (int i = 2; i <= n; i++) cout << c[i] - a[1] << ' '; cout << endl;
    return 0;
}