跳到正文

题解

题解:AT_arc222_e [ARC222E] XOR Matching

P2441M约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年6月14日
收录
2026年6月14日
更新
2026年11月18日
篇幅
约 6 分钟
获赞
1

版本

  1. v1当前

赛时做法,和官解不太一样。

设 xx 的出现次数为 cxc_x。

不难推出

f(0)=∑v=02m−1⌊cv2⌋f(x)=∑v=02m−1[v<v⊕x]min⁡(cv,cv⊕x) (x>0)\begin{align*} f(0)&=\sum_{v=0}^{2^m-1}\left\lfloor\frac{c_v}{2}\right\rfloor\\ f(x)&=\sum_{v=0}^{2^m-1}[v<v\oplus x]\min(c_v,c_{v\oplus x})\ (x>0) \end{align*}

于是可以转化为求

∑v=02m−1⌊cv2⌋+∑i=02m−1∑j=i+12m−1min⁡(ci,cj)10i⊕j\sum_{v=0}^{2^m-1}\left\lfloor\frac{c_v}{2}\right\rfloor+\sum_{i=0}^{2^m-1}\sum_{j=i+1}^{2^m-1}\min(c_i,c_j)10^{i\oplus j}

将 0∼2m−10\sim 2^m-1 内的数按 cic_i 从大到小扫,设前面处理过的数集为 SS,那每次扫到一个数 xx 的增量就是

cx∑y∈S10x⊕yc_x\sum_{y\in S}10^{x\oplus y}

于是问题转化为维护一个集合 SS,支持插入一个数、查询 ∑y∈S10x⊕y\sum\limits_{y\in S}10^{x\oplus y}。

折半成低 LL 位和高 HH 位,设 x=xl+xh2Lx=x_l+x_h2^L,y=yl+yh2Ly=y_l+y_h2^L,则

10x⊕y=10xl⊕yl×10(xh⊕yh)2L10^{x\oplus y}=10^{x_l\oplus y_l}\times 10^{(x_h\oplus y_h)2^L}

维护

fh,l=∑y∈S[yh=h]10l⊕ylf_{h,l}=\sum_{y\in S}[y_h=h]10^{l\oplus y_l}

那么对于一个固定的 xx,查询的答案就是

∑h=02H−110(xh⊕h)2L×fh,xl\sum_{h=0}^{2^H-1}10^{(x_h\oplus h)2^L}\times f_{h,x_l}

插入一个数 xx 时,枚举 l=0∼2L−1l=0\sim 2^L-1,令 fxh,l←fxh,l+10l⊕xlf_{x_h,l}\gets f_{x_h,l}+10^{l\oplus x_l} 即可。

对于每个 0≤i<2L0\leq i<2^L,预处理 10i10^i,对于每个 0≤i<2H0\leq i<2^H,预处理 10i2L10^{i2^L}。时间复杂度为 O(2m+n2⌈m/2⌉)\mathcal{O}(2^m+n2^{\lceil m/2\rceil})。

代码
C++
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using i128 = __int128;
using ui = unsigned int;
using ull = unsigned long long;
using u128 = unsigned __int128;
using ld = long double;
using pii = pair<int, int>;
const int MAXN = 2e5 + 5, MAXV = 1 << 20, MAXV2 = 1 << 10;
const int mod = 998244353;

template<typename T> T lowbit(T x) { return x & -x; }
template<typename T> void chkMin(T &x, T y) { x = y < x ? y : x; }
template<typename T> void chkMax(T &x, T y) { x = x < y ? y : x; }
template<typename T> T add(T x, T y) { return (x += y) >= mod ? x - mod : x; }
template<typename T> T sub(T x, T y) { return (x -= y) < 0 ? x + mod : x; }
template<typename T> void cadd(T &x, T y) { (x += y) >= mod ? (x -= mod) : x; }
template<typename T> void csub(T &x, T y) { (x -= y) < 0 ? (x += mod) : x; }

int n, m, ans, cnt[MAXV];
int pw1[MAXV2], pw2[MAXV2];
int f[MAXV2][MAXV2];

int qpow(int a, int b) {
	int res = 1;
	for (; b; b >>= 1) {
		if (b & 1) res = (ll)res * a % mod;
		a = (ll)a * a % mod;
	}
	return res;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		int x;
		cin >> x;
		++cnt[x];
	}
	int low = m + 1 >> 1, high = m - low;
	pw1[0] = 1;
	for (int i = 1; i < (1 << low); ++i) pw1[i] = (ull)pw1[i - 1] * 10 % mod;
	pw2[0] = 1;
	int mul = (ull)pw1[(1 << low) - 1] * 10 % mod;
	for (int i = 1; i < (1 << high); ++i) pw2[i] = (ull)pw2[i - 1] * mul % mod;
	vector<pii> vec;
	for (int i = 0; i < (1 << m); ++i) {
		if (!cnt[i]) continue;
		ans += cnt[i] >> 1;
		vec.emplace_back(cnt[i], i);
	}
	sort(vec.begin(), vec.end(), greater<>());
	for (auto [c, v] : vec) {
		int lowV = v & ((1 << low) - 1), highV = v >> low;
		int sum = 0;
		for (int h = 0; h < (1 << high); ++h)
			cadd<int>(sum, (ull)pw2[highV ^ h] * f[h][lowV] % mod);
		cadd<int>(ans, (ull)sum * c % mod);
		for (int l = 0; l < (1 << low); ++l)
			cadd(f[highV][l], pw1[lowV ^ l]);
	}
	cout << ans;
	return 0;
}
原文
存档评论2 条抓取于