跳到正文

个人记录

Keep XOR Low 题解

yh2022mayu约 10 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月2日
篇幅
约 10 分钟

版本

  1. v1当前

题目链接

solution

妙妙题。

一看异或,就知道是trietrie,一看问方案数,就知道是计数。

先将所有数建成trietrie,考虑在trietrie上dpdp。

先想当然的设dpudp_u表示用且只用uu子树内的数有多少种情况,设uu对应的二进制位为dd,那么分两种情况讨论。

(1).(1). xx的dd位上为00,那么只能所有数均在uu的一个子树里才可以,根据加法原理所以就是dplu+dprudp_{l_u}+dp_{r_u}(其中lul_u表示uu的左儿子)。

(2).(2). xx的dd位上为00,那么只要是说有数在同一个子树内就一定可以,所以答案先加上2szlu+2szru−22^{sz_{l_u}}+2^{sz_{r_u}}-2(szusz_u表示uu子树中数的个数,减二是因为不能不选),然后答案还要加上既包含lul_u中的数,又包含rur_u中的数的答案。

考虑如何维护上面那个互相影响的点对的答案。我们在开一个数组cdpu,vcdp_{u,v}表示既包含uu中的数,又包含vv中的数,且只包含这两个点子树内的数的答案,其中uu,vv满足深度相等(设为dd)且uu中的数异或上vv中的数前d−1d-1位与xx的前d−1d-1位相同,还是分两种情况考虑。

(1).(1). xx的dd位上为00,那么只能所有数均在uu和vv的一侧子树里才可以,根据加法原理所以就是cdplu,lv+cdpru,rvcdp_{l_u,l_v}+cdp_{r_u,r_v}。

(2).(2). xx的dd位上为00,那么限制只有当所选出的数集同时包含lul_u和rvr_v中的数或者同时包含rur_u和lvl_v中的数才会有限制,所以我们将答案拆为一下55部分。

1.1. 只包含lul_u和lvl_v的答案,由于这部分互相没有限制,所以是(2szlu−1)(2szlv−1)(2^{sz_{l_u}}-1)(2^{sz_{l_v}}-1)。

2.2. 只包含rur_u和rvr_v的答案,由于这部分互相没有限制,所以是(2szru−1)(2szrv−1)(2^{sz_{r_u}}-1)(2^{sz_{r_v}}-1)。

3.3. 限制的只有rur_u和lvl_v,那么对于只包含rur_u和lvl_v的答案我们可以递归cdpru,lvcdp_{r_u,l_v}的结果,而lul_u和rvr_v都与这两部分数不冲突,所以可以任选,只要不同时包含lul_u和rvr_v即可,所以答案是cdpru,lv(2szrv−1+2szlu−1+1)cdp_{r_u,l_v}(2^{sz_{r_v}}-1+2^{sz_{l_u}}-1+1)(解释一下,其中2szrv−12^{sz_{r_v}}-1系数表示包含rur_u,lvl_v,rvr_v三部分的数,2szlu−12^{sz_{l_u}}-1同理,11系数表示只包含rur_u和lvl_v的答案)。

4.4. 限制的只有rvr_v和lul_u,同理,为cdprv,lu(2szru−1+2szlv−1+1)cdp_{r_v,l_u}(2^{sz_{r_u}}-1+2^{sz_{l_v}}-1+1)。

5.5. 四个子节点都包含,由于每个点除了与之限制的点以外都没有限制,所以是cdprv,lucdpru,lvcdp_{r_v,l_u}cdp_{r_u,l_v}。

最终答案是dp1dp_1。

最后看一下时间复杂度,容易发现每一个节点不论是dpdp还是cdpcdp都最多只会被当做下标一次,所以是o(nlogV)o(nlogV)(VV为值域)。

实现上甚至可以不用开dpdp数组,直接用dfsdfs返回值即可。

C++
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#define YES puts("YES")
#define Yes puts("Yes")
#define NO puts("NO")
#define No puts("No")
#define b_i __int128
#define ll long long
#define ull unsigned long long
#define x first
#define y second
#define pr pair
#define dl double
#define pll pr<ll, ll>
#define pii pr<int, int>
#define pdd pr<dl, dl>
#define ld long dl
#define vc vector
#define vci vc<int>
#define vcl vc<ll>
#define vcd vc<dl>
#define vcs vc<string>
#define vcp vc<pii >
#define ps push
#define pp pop
#define frt front
#define bck back
#define psf push_front
#define psb push_back
#define ppb pop_back
#define ppf pop_front
#define bgn begin
using namespace std;
const ll md = 998244353;
const ll mod = 1e9 + 7;
const dl esp = 1e-12;
const int inf = 2e9;
const ll INF = 2e18;
inline int read(){
	char c = getchar();
	int ans = 0, sz = 1;
	while(c < '0' || c > '9'){
		if(c == '-') sz = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9'){
		ans *= 10;
		ans += (c - '0');
		c = getchar();
	}
	return ans * sz;
}
inline void write(int x){
    if(x < 0){
        putchar('-');
        x = -x;
    }
    if(x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
ll ksm(ll a, ll b, ll md){
	ll ans = 1;
	a %= md;
	while(b){
		if(b % 2) ans = ans * a % md;
		a = a * a % md;
		b /= 2;
	}
	return ans;
}
ll rnd(ll l, ll r){
	return l + rand() % (r - l + 1);
}
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll jc[1000005], ny[1000005];
ll C(int a, int b, ll md){
	if(b > a || a < 0 || b < 0) return 0;
	return jc[a] * ny[b] % md * ny[a - b] % md;
}
ll A(int a, int b, ll md){
	if(b > a || a < 0 || b < 0) return 0;
	return jc[a] * ny[a - b] % md;
}
void initAC(ll md){
	jc[0] = ny[0] = 1;
	for(int i = 1; i <= 1000000; i++) jc[i] = jc[i - 1] * i % md, ny[i] = ksm(jc[i], md - 2, md);
}
ll n, tot = 1, x;
ll pw[300005];
struct Trie{
	ll s[2], sz;
} tr[10000005];
void ins(int x){
	int nw = 1;
	for(int i = 30; i >= 0; i--){
		int c = ((x >> i) & 1);
		tr[nw].sz++;
		if(!tr[nw].s[c]) tr[nw].s[c] = ++tot;
		nw = tr[nw].s[c];
	}
	tr[nw].sz++;
}
ll dfs(int u, int v, int d){
	if(!u || !v) return 0;
	if(u == v){
		if(d == -1) return pw[tr[u].sz] - 1;
		if((x >> d) & 1) return (pw[tr[tr[u].s[0]].sz] - 1 + pw[tr[tr[u].s[1]].sz] - 1 + dfs(tr[u].s[0], tr[u].s[1], d - 1)) % md;
		else return (dfs(tr[u].s[0], tr[u].s[0], d - 1) + dfs(tr[u].s[1], tr[u].s[1], d - 1)) % md;
	}
	else{
		if(d == -1) return (pw[tr[u].sz] - 1) * (pw[tr[v].sz] - 1) % md;
		if((x >> d) & 1){
			ll cnt1 = pw[tr[tr[u].s[0]].sz] - 1, cnt2 = pw[tr[tr[u].s[1]].sz] - 1, cnt3 = pw[tr[tr[v].s[0]].sz] - 1, cnt4 = pw[tr[tr[v].s[1]].sz] - 1;
			ll ans1 = dfs(tr[u].s[1], tr[v].s[0], d - 1), ans2 = dfs(tr[u].s[0], tr[v].s[1], d - 1);
			return (cnt1 * cnt3 % md + cnt2 * cnt4 % md + ans1 * (cnt1 + cnt4 + 1) % md + ans2 * (cnt2 + cnt3 + 1) % md + ans1 * ans2) % md;
		}
		else return (dfs(tr[u].s[0], tr[v].s[0], d - 1) + dfs(tr[u].s[1], tr[v].s[1], d - 1)) % md;
	}
}
signed main(){
	//ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
//    freopen("xor.in", "r", stdin);
    //freopen(".out", "w", stdout);
	srand(time(0));
	pw[0] = 1;
	for(int i = 1; i <= 200000; i++) pw[i] = pw[i - 1] * 2 % md;
	cin >> n >> x;
	for(int i = 1, a; i <= n; i++) cin >> a, ins(a);
	cout << dfs(1, 1, 30);
	return 0;
}
原文