题解:P17170 未来 题解

· · 题解

思路

设最终对第 i 行的总异或值为 x_i,对第 j 列的总异或值为 y_j,则 A_{i,j} = x_i\oplus y_j。
考虑先固定 x_1 = 0,则 y_j = A_{1,j},x_i = A_{i,1} \oplus A_{1,1}。
接着检查是否对于所有的 A_{i,j} 都成立,若不成立则输出 -1。
假设我们最后某一个位置选择的值为 x,而上限为 r,记花费为 \operatorname{cost}(x,r)。

\begin{cases} 0, & \text{若 } x = 0 \\ 1, & \text{若 } 0 < x \le r \\ 2, & \text{若 } r < x \land \lfloor\log_2 r\rfloor = \lfloor\log_2 x\rfloor \\ \text{无解}, & \text{若 } \lfloor\log_2 r\rfloor < \lfloor\log_2 x\rfloor \end{cases}

于是我们构造 n+m 个形如 (b,r) 的二元组,其中 b 为 x_i 或 y_j,r 为对应的上限,我们的目标是找出一个非负整数 k,使得 \sum\operatorname{f}(b\oplus k,r) 最小,即使 2(n + m)-\sum([b\oplus k\le r] + [b\oplus k = 0]) 最小,即使 \sum([b\oplus k\le r] + [b = k]) 最大。
对于所有大于 r 的最高位的位 w,则 k 的第 w 位必须等于 b 的第 w 位,若无法满足则输出 -1。
对于剩下没有固定的位,我们构建一个 01-Trie。
对于 b\oplus k\le r,从高位到低位去比较。设 x 为 b 的该位,y 为 r 的该位。

对于 b = k,直接递归到叶子并令权值 +1 即可。
最后我们维护一个最大值即可,细节详见代码。
时间复杂度:O(nm+(n + m)\log V)。

代码

// 思路参考deepseek
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
#define clz __builtin_clz
int read() {
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
const int N = 5e4 + 10,W = 30;
int T;
int n,m;
struct Trie {
    int son[2];
    int val;
    int mx;
    #define son(i) trie[i].son
    #define val(i) trie[i].val
    #define mx(i) trie[i].mx
} trie[2 * W * N];
int cnt = 1;
void pushup(int p) {
    mx(p) = max(mx(son(p)[0]),mx(son(p)[1])) + val(p);
}
int lim[W];
void add1(int p,int w,int r,int x) {
    if (w == -1) {
        val(p)++;
        mx(p)++;
        return;
    }
    int a = r >> w & 1,b = x >> w & 1;
    if (a == 0) {
        if (lim[w] != (b ^ 1)) {
            if (son(p)[b] == 0) {
                son(p)[b] = ++cnt;
            }
            add1(son(p)[b],w - 1,r,x);
        }
    }
    else {
        if (lim[w] != (b ^ 1)) {
            if (son(p)[b] == 0) {
                son(p)[b] = ++cnt;
            }
            val(son(p)[b])++;
            mx(son(p)[b])++;
        }
        if (lim[w] != b) {
            if (son(p)[b ^ 1] == 0) {
                son(p)[b ^ 1] = ++cnt;
            }
            add1(son(p)[b ^ 1],w - 1,r,x);
        }
    }
    pushup(p);
}
void add2(int p,int w,int x) {
    if (w == -1) {
        val(p)++;
        mx(p)++;
        return;
    }
    int a = x >> w & 1;
    if (son(p)[a] == 0) {
        son(p)[a] = ++cnt;
    }
    add2(son(p)[a],w - 1,x);
    pushup(p);
}
int solve() {
    n = read(),m = read();
    vector<int> r(n),c(m),x(n),y(m);
    vector<vector<int>> a(n,vector<int>(m));
    for (int i = 0; i < n; i++) {
        r[i] = read();
    }
    for (int j = 0; j < m; j++) {
        c[j] = read();
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            a[i][j] = read();
        }
    }
    x[0] = 0;
    for (int j = 0; j < m; j++) {
        y[j] = x[0] ^ a[0][j];
    }
    for (int i = 1; i < n; i++) {
        x[i] = y[0] ^ a[i][0];
    }
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++) {
            if ((x[i] ^ y[j]) != a[i][j]) {
                return -1;
            }
        }
    }
    memset(lim,-1,sizeof(lim));
    for (int i = 0; i < n; i++) {
        for (int w = (r[i] == 0 ? 0 : 32 - clz(r[i])); w < 30; w++) {
            if (lim[w] == -1) {
                lim[w] = x[i] >> w & 1;
            }
            else if (lim[w] != (x[i] >> w & 1)) {
                return -1;
            }
        }
    }
    for (int j = 0; j < m; j++) {
        for (int w = (c[j] == 0 ? 0 : 32 - clz(c[j])); w < 30; w++) {
            if (lim[w] == -1) {
                lim[w] = y[j] >> w & 1;
            }
            else if (lim[w] != (y[j] >> w & 1)) {
                return -1;
            }
        }
    }
    son(1)[0] = son(1)[1] = 0;
    val(1) = mx(1) = 0;
    for (int i = 0; i < n; i++) {
        add1(1,29,r[i],x[i]);
        add2(1,29,x[i]);
    }
    for (int j = 0; j < m; j++) {
        add1(1,29,c[j],y[j]);
        add2(1,29,y[j]);
    }
    return 2 * (n + m) - mx(1);
    return 0;
}
int main() {
    T = read();
    while (T--) {
        cout << solve() << "\n";
    }
    return 0;
}