题解:CF2194F1 Again Trees... (Easy Version)

· · 题解

Easy Version

暴力做法是设 f_{u,S} 表示考虑 u 的子树,且 u 当前所在连通块的异或和为 S 的方案数。转移有两种:

复杂度 n \times (10^9)^2。

设 s_u 表示 u 的子树异或和。注意到 f_{u,S} \ne 0 的必要条件是,s_u \oplus S 是 b 的一个子集的异或和。即除了自己所在的连通块,其他已经完成的连通块的异或和的取值只有 2^k 种。于是第二维状态数减少到了 2^k。总复杂度 \mathcal O(n 4^k)。

具体的,设 f_{u,S} 表示考虑 u 的子树且除了 u 所在连通块外,b 中每个元素在这些连通块内出现次数的奇偶性为 S 的方案数(出现两次就能消掉,所以只关心奇偶性)。转移:

:::success[Code]

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10, P = 1e9 + 7;

int n, a[N], b[4], k, sum[N];
vector<int> g[N];

void add(int &a, int b) {
    a += b;
    if (a >= P) a -= P;
}

int id(int x) {
    for (int i = 0; i < k; ++ i )
        if (b[i] == x) return i;
    return -1;
}

int f[N][1 << 4], h[1 << 4], p[1 << 4];

void dfs(int u, int F) {
    sum[u] = a[u];
    for (int v : g[u])
        if (v != F) {
            dfs(v, u);
            sum[u] ^= sum[v];
        }

    memset(f[u], 0, sizeof(int) * 16);
    f[u][0] = 1;
    for (int v : g[u])
        if (v != F) {
            for (int i = 0; i < 1 << k; ++ i ) h[i] = f[u][i], f[u][i] = 0;

            for (int i = 0; i < 1 << k; ++ i )
                for (int j = 0; j < 1 << k; ++ j ) {
                    add(f[u][i ^ j], 1ll * h[i] * f[v][j] % P);
                    int x = id(sum[v] ^ p[j]);
                    if (~x) add(f[u][i ^ j ^ (1 << x)], 1ll * h[i] * f[v][j] % P);
                }
        }
}

int solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; ++ i ) g[i].clear();
    for (int i = 1; i < n; ++ i ) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b), g[b].push_back(a);
    }
    for (int i = 1; i <= n; ++ i ) cin >> a[i];
    for (int i = 0; i < k; ++ i ) cin >> b[i];
    for (int s = 0; s < 1 << k; ++ s ) {
        p[s] = 0;
        for (int i = 0; i < k; ++ i ) {
            if (s >> i & 1) p[s] ^= b[i];
        }
    }

    dfs(1, 0);

    int res = 0;
    for (int s = 0; s < 1 << 4; ++ s ) {
        if (~id(sum[1] ^ p[s])) add(res, f[1][s]);
    }
    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T -- ) cout << solve() << '\n';
    return 0;
}

:::

Hard Version

这个转移与 FWT 极像。不过在第二种转移里,S,T 一起转移到了 S \oplus T \oplus \{i\}。而这个 i 可以只由 v,T 确定。于是,如果我们先更新 f_{v,T} \to f_{v,T \oplus \{i\}},那么剩下的就是裸的异或卷积了。

不过很可惜,一次 FWT 复杂度是 \mathcal O(2^k k) 的,总复杂度 \mathcal O(n2^k k) 依然无法接受。我们希望优化掉这个 k。

现在我们尝试直接维护 f' 表示 f 做完 FWT 后的结果。那么如果只有第一种转移,直接做 \mathcal O(2^k) 点乘即可。考虑如何处理第二种转移。

注意到第二种转移可以进行(即可以找到 \{i\})当且仅当 s_v 本身就可以被 b 的某个子集表示出来,但这个子集可能有多个,所以先把 b 放到线形基里。这样在做 f_{v,T} \to f_{v,T \oplus \{i\}} 时,T \oplus \{i\} 就是定值了。设这个定值是 A。具体来说,A 就是 s_v 在线形基中的那些位置。

那么现在要做的是对 f_{v,A} 单点加 \sum_{i=1}^{k} f_{v,A \oplus pos_i},其中 pos_i 为 b_i 在线形基中的那些位置。现在有两个问题,一是如何求出 \sum_{i=1}^{k} f_{v,A \oplus pos_i},二是求出这个值后,单点加这个操作会如何更新 f'_v。

第二个问题是容易的。考虑定义 FWT(a)_S = \sum_T (-1)^{|S \cap T|} a_T。那么当 a_T \to a_T + c 后,对每个位置 S 增加 (-1)^{|S \cap T|} c 即可。

那么 \sum_{i=1}^{k} f_{v,A \oplus pos_i} 怎么求呢?考虑一个神奇的构造。设 g 为只有下标 A 为 1 的集合幂级数。做 f_v \gets f_v \ast g(\ast 为异或卷积),现在问题变成求 \sum_{i=1}^{k} f_{v,pos_i}。再设 h 为只有所有下标 pos_i 为 1 的集合幂级数。做 f_v \gets f_v \ast h,现在问题变成求 f_{v,0}。

上面两次异或卷积,可以通过预处理 g,h 的 FWT 后的结果,直接做 \mathcal O(2^k) 的点乘。这样就得到了 F = f'_v \cdot FWT(g) \cdot FWT(h)。通过这个,求单点 f_{v,0} 的值是容易的。具体的 f_{v,0} = \dfrac 1{2^k} \sum_i (-1)^{|\varnothing\cap i|}F_i= \dfrac 1{2^k} \sum_i F_i,\mathcal O(2^k) 暴力求即可。

:::success[Code]

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10, P = 1e9 + 7, inv2 = P + 1 >> 1;

int n, k, s[N], pos[10], b[10], w[N];
vector<int> adj[N], vec;
int a[32], ppc[N], inv2m, m;

void insert(int x) {
    for (int i = 31; ~i; -- i ) {
        if (x >> i & 1) {
            if (a[i]) x ^= a[i];
            else {
                a[i] = x;
                break;
            }
        }
    }
}

int f[N][1 << 10];
int A[N], g[1 << 10], h[1 << 10];

void FWT(int *a) {
    for (int i = 1; i < 1 << m; i <<= 1)
        for (int j = 0; j < 1 << m; j += i + i)
            for (int u = 0; u < i; ++ u ) {
                const int x = a[j + u], y = a[j + u + i];
                a[j + u] = x + y, a[j + u + i] = x - y;
                if (a[j + u] >= P) a[j + u] -= P;
                if (a[j + u + i] < 0) a[j + u + i] += P;
            }
}

void IFWT(int *a) {
    for (int i = 1; i < 1 << m; i <<= 1)
        for (int j = 0; j < 1 << m; j += i + i)
            for (int u = 0; u < i; ++ u ) {
                const int x = a[j + u], y = a[j + u + i];
                a[j + u] = 1ll * (x + y) * inv2 % P;
                a[j + u + i] = 1ll * (x - y + P) * inv2 % P;
            }
}

void dfs(int u, int F) {
    for (int i = 0; i < 1 << m; ++ i ) {
        f[u][i] = 1;
    }

    s[u] = w[u];
    for (int v : adj[u])
        if (v != F) {
            dfs(v, u);
            if (~A[v]) {
                int sum = 0;
                for (int s = 0; s < 1 << m; ++ s ) {
                    sum = (sum + 1ll * f[v][s] * (ppc[s & A[v]] ? P - h[s] : h[s])) % P;
                }
                sum = 1ll * sum * inv2m % P;
                if (sum)
                for (int s = 0; s < 1 << m; ++ s ) {
                    f[v][s] += ppc[s & A[v]] ? P - sum : sum;
                    if (f[v][s] >= P) f[v][s] -= P;
                }
            }
            for (int s = 0; s < 1 << m; ++ s ) {
                f[u][s] = 1ll * f[u][s] * f[v][s] % P;
            }
            s[u] ^= s[v];
        }

    int x = s[u];
    A[u] = 0;
    for (int i = 0; i < m; ++ i )
        if (x >> vec[i] & 1) {
            A[u] |= 1 << i;
            x ^= a[vec[i]];
        }
    if (x) A[u] = -1;
}

int solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; ++ i ) adj[i].clear();
    for (int i = 1; i < n; ++ i ) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for (int i = 1; i <= n; ++ i ) {
        cin >> w[i];
    }

    memset(a, 0, sizeof a);
    for (int i = 0; i < k; ++ i ) {
        cin >> b[i];
        insert(b[i]);
    }

    vec.clear();
    for (int j = 31; ~j; -- j ) {
        if (a[j]) vec.push_back(j);
    }
    m = vec.size();

    inv2m = 1;
    for (int i = 0; i < m; ++ i ) inv2m = 1ll * inv2m * inv2 % P;

    memset(h, 0, sizeof h);
    for (int i = 0; i < k; ++ i ) {
        int res = 0;
        for (int j = 0; j < m; ++ j ) {
            if (b[i] >> vec[j] & 1) {
                res |= 1 << j;
                b[i] ^= a[vec[j]];
            }
        }
        pos[i] = res;
        h[pos[i]] = 1;
    }
    FWT(h);

    dfs(1, 0);

    IFWT(f[1]);
    if (A[1] == -1) return 0;
    int res = 0;
    for (int i = 0; i < k; ++ i ) {
        res = (res + f[1][A[1] ^ pos[i]]) % P;
    }
    return res;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    for (int i = 0; i < N; ++ i ) {
        ppc[i] = __builtin_popcount(i) & 1;
    }
    int T;
    cin >> T;
    while (T -- ) cout << solve() << '\n';
    return 0;
}

:::