题解:CF2194F1 Again Trees... (Easy Version)
Easy Version
暴力做法是设
复杂度
设
具体的,设
:::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 极像。不过在第二种转移里,
不过很可惜,一次 FWT 复杂度是
现在我们尝试直接维护
注意到第二种转移可以进行(即可以找到
那么现在要做的是对
第二个问题是容易的。考虑定义
那么
上面两次异或卷积,可以通过预处理
:::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;
}
:::