题解:AT_arc145_e [ARC145E] Adjacent XOR

· · 题解

solution

考虑把逆向操作,原问题等价于每次可以选择一个 k,然后从小到大依次执行 b _ {i} \gets b _ {i} \operatorname{xor} b _ {i - 1},其中 i 在 1 到 k 间,要求将 b 数组变成 a 数组。

若有解,显然的,有 a _ {i} \operatorname{xor} b _ {i} 可以被表示成 b _ {1} 至 b _ {i - 1} 中若干个数的异或和。

由于操作 i 时,不会影响 i + 1 到 n 的数,所以考虑倒着做。

假设我们现在要确定 i,考虑把 b _ {1} 到 b _ {i - 1} 插入线性基,并给线性基中的每个非零元素维护它们出现的时间,也就是代码中的 tot。

对于任意的 j 在 1 到 i - 1,我们记 S _ {j} 表示线性组合出 b _ {j} 用到的线性基元素集合,记 S _ {i} 表示线性组合出 a _ {i} \operatorname{xor} b _ {i} 用到的线性基元素集合,这样的一个 S 可以状压成一个二进制数。

现在我们需要解决的是,保证在所有 S _ {j} 中,S _ {i} 中的元素出现了奇数次,其它元素出现了偶数次,这样最后操作一次 i 就可以使 a _ {i} = b _ {i}。

更具体的,我们可以用一个 c _ {j} 表示状压后的 S _ {j},我们可以直接在 c 数组上运作,我们对新进来线性基的这个数做一个判断,若它能新出来一个基底,则记 pos _ {k} = j 其中 k 为 c _ {j} 二进制下为 1 的最高位,因为我们是按照时间标记基,所以最高位一定是最晚的,新加入的基。

我们从出现时间晚的线性基元素开始操作,如果当前元素需要调整(不满足上述奇偶性相关),我们只需要操作 pos _ {j} + 1 这个前缀,就可以保证其出现次数奇偶性变化,因为它是最早出现在 b 数组中的位置,比他早的位置一定都为 0,若对于 pos _ {j} + 1 操作,那么一定只有 pos _ {j} + 1 这个位置被更改,那么该位的奇偶性一定就会满足。

更详细的见代码。

code

#include <bits/stdc++.h>
#define int long long
#define For for (int i = 60; i >= 0; i--)
using namespace std;

const int N = 1010;

int a[N], b[N], c[N], pos[70], n;

struct LinearBasic {
    int v[70], id[70], tot;

    inline bool check (int x) {
        For if ((x >> i) & 1) 
            if (v[i]) x ^= v[i];
            else return false;
        return true;
    } 

    inline int insert (int x) {
        int res = 0;
        For if ((x >> i) & 1) 
            if (v[i]) x ^= v[i], res |= (1ll << id[i]);
            else return v[i] = x, id[i] = tot++, res | (1ll << id[i]);
        return res;
    }

    inline void clear () {
        memset (v, 0, sizeof (v)), memset (id, 0, sizeof (id)), tot = 0;
    }
} p; 

vector <int> ans;

inline void work (int x) {
    ans.push_back (x);
    for (int i = 1; i <= x; i++) b[i] ^= b[i - 1], c[i] ^= c[i - 1];
}

signed main () {
    ios :: sync_with_stdio (false), cin.tie (0), cout.tie (0), cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    for (int i = 1; i <= n; i++)
        if (!p.check (a[i] ^ b[i])) return cout << "No", 0;
        else p.insert (b[i]);
    for (int i = n; i; i--) {
        p.clear ();
        for (int j = 1; j <= i; j++) {
            bool flag = p.check (b[j]); 
            c[j] = p.insert (b[j]);
            if (!flag) pos[__lg (c[j])] = j;
        }
        int v = p.insert (a[i]);
        for (int j = p.tot - 1; j >= 0; j--) {
            int res = 0;
            for (int k = 1; k <= i; k++) res ^= c[k];
            if (((res >> j) & 1) != ((v >> j) & 1)) work (pos[j] + 1);
        } 
        work (i);
    }
    reverse (begin (ans), end (ans)), cout << "Yes" << endl << ans.size () << endl;
    for (int Ans : ans) cout << Ans << " ";
    return 0;
}