跳到正文

题解

Solution P3791

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

文章信息

发布
2022年8月11日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 6 分钟
题目
P3791

版本

  1. v1当前

晕,位运算尽然要写 1ll << s。

  • 题意:

给定 nn,mm,xx,求 ∑i=0n∑j=0md(i⨁j⨁x)\sum_{i=0}^n \sum_{j=0}^m d(i\bigoplus j\bigoplus x)。

  • 分析

考虑换个枚举顺序,变成对于每个不同的 c=i⨁j⨁xc=i\bigoplus j\bigoplus x,求其出现次数 cntcnt 再乘上对应的 d(c)d(c),总贡献 cnt×d(c)cnt \times d(c)。

因为涉及到位运算,所以考虑拆位,然后发现很想数位 dp,但偏偏 dp 不了。于是我们来拆分一下数位 dp 的具体过程,其中很关键的参量就是 limlim 判断是否达上限和 leadlead 是否为前导零。前导零在这道题中是没地位的,直接扔了,而是否达上线,我们可以发现,是统计 cntcnt 的关键。

于是我们枚举一个 ii 和 jj,不妨假设 j≤ij \le i,分别代表 nn 和 mm 在什么位置打破上限,即接下来的数可以随便选。而我们发现由于进行的是位运算,所以区间 [1,j−1][1,j-1] 显然可以覆盖所以可能的值,共 2j−12^{j-1},nn 和 mm 的取值共 4j−14^{j-1} 种组合,所以每种值有 2j−12^{j-1} 个。同理区间 [j,i−1][j,i-1] 因为 mm 这边定死了,所以每种值只有 11 个,但仍然能覆盖到所有可能的数。

于是我们得出了一种可行的做法:枚举 ii 和 jj,满足 ni=1n_i=1,mi=1m_i=1,不妨假设 j≤ij \le i,可以发现从第 ii 位开始全部定死,前面可以选任何数,每种不同的数的 cntcnt 为 2j−12^{j-1}。只需求 2j∑i=LRd(i)2^j\sum\limits_{i=L}^R d(i)(LL、RR 具体见代码),显然可以差分然后整除分块,应为本质不同的数量很少所以可以记忆化。

设 sufisuf_i 为后 ii 位三个数异或起来的后缀和,那么 LL 就是 sufisuf_i,RR 则是 sufi+2i−1−1suf_i + 2^{i-1}-1,因为显然区间 [1,i−1][1,i-1] 全取 11 就这么大,注意因为在 ni=1n_i=1 且 mi=1m_i=1,又在此处打破限制,所以实际上计算时 ni=0n_i=0,mi=0m_i=0,要从后缀和中去掉。

复杂度 O(log⁡2n+nlog⁡n)O(\log^2 n+\sqrt n \log n)。

  • code
C++
#include <bits/stdc++.h>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define int long long
using namespace std;

namespace IO{
	char ibuf[(1 << 20) + 1], *iS, *iT;
	#if ONLINE_JUDGE
	#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
 	#else
	#define gh() getchar()
	#endif
	#define reg register
	inline long long read () {
		reg char ch = gh();
		reg long long x = 0;
		reg char t = 0;
		while (ch < '0' || ch > '9')   t |= ch == '-', ch = gh();
		while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gh();
		return t ? -x : x;
	}
	inline void write(long long x) {
		if (x < 0) {
			x = ~(x - 1);
			putchar('-');
		}
		if (x > 9)
			write(x / 10);
		putchar(x % 10 + '0');
	}
}

using IO::read;
using IO::write;

const int N = 80, mod = 998244353;
int n, m, ct, A, B, X, a[N], b[N], x[N], mi[N], h[N], ans;
__gnu_pbds::gp_hash_table <int, int> sd;

int D (int x) {
	if (!(~x) || !x) return 0;
	if (sd.find(x) != sd.end()) return sd[x];
	int res = 0;
	for (int l = 1, r; l <= x; l = r + 1) {
		r = x / (x / l);
		res = (res + (r - l + 1) % mod * (x / l) % mod) % mod;
	}
	return sd[x] = res;
}

signed main () {
	A = read() + 1, B = read() + 1, X = read();
	for (; A; A >>= 1) a[++n] = A & 1;
	for (; B; B >>= 1) b[++m] = B & 1;
	for (; X; X >>= 1) x[++ct] = X & 1;
	mi[0] = 1; for (int i = 1; i < N; i++) mi[i] = mi[i - 1] * 2 % mod;
	for (int i = max(max(n, m), ct); i; i--) h[i] = h[i + 1] + ((long long)(a[i] ^ b[i] ^ x[i]) << (i - 1));
	for (int i = 1; i <= n; i++) {
		if (!a[i]) continue;
		for (int j = 1; j <= m; j++) {
			if (!b[j]) continue;
			int s = max(i, j), t = min(i, j), H = h[s] ^ (1ll << (s - 1));
			if (s == t) H ^= (1ll << (s - 1));
			ans = (ans + (D(H + (1ll << (s - 1)) - 1) - D(H - 1) + mod) % mod * mi[t - 1] % mod) % mod;
		}
	}
	write(ans);
}