当前位置: 首页 > news >正文

“[GESP202509 五级] 有趣的数字和”分块做法

这个题看到第一眼不是暴力数位 dp 创过去吗?
换以前,我虽然忘了数位 dp ,但是可能接着这个机会重新学一遍数位 dp 。
但是最近工作任务和学校任务都很重,根本不想重新学一遍数位 DP 。
反而让我发现了一个更通用更好更简单的做法。

洛谷链接:https://www.luogu.com.cn/problem/P14074

核心问题:问题容易被转化成计算 \(x \in [0, N]\) 中,所有满足 \(popcount(x)\) 是奇数的数字之和。
为了不引起人类语言上的歧义,形式化表示即要求计算:

\[\sum_{i = 0}^{N} [popcount(i) \bmod 2 = 1] \]

其中 [p]\(p\) 为真返回 \(1\) ,若 \(p\) 为假返回 \(0\)

关于 \(0 \sim N\) 内所有数在二进制下的加法贡献问题,常见的最无脑最简单方法是把指数折半。
根据经验,如果有 \(L(a^{x + y}) = L(a^{x}) \times L(a^{y})\) ,那么我们可以把 \(O(a^{x + y})\) 级问题转化为 \(O(max(a^{x}, a^{y}))\) 级问题。

以上是一些经验主义的东西。

接下来介绍实际的解法。


考虑带余整除法 \(v = 2^{k} \cdot x + r \ (0 \leq r < 2^{k})\) 。其中 \(2^k \leq N\)\(k\) 为常数。

每个组(每个块)余数取满 \(0 \sim 2^{k} - 1\) ,则 \(0 \sim N\) 可以分成 \(\lceil N / 2^{k} \rceil\) 个块。

根据 \(x = 0, 1, 2, \cdots, \lceil N / 2^{k} \rceil - 1\) 对每个块编号,分布如下:

\[\begin{aligned} &[0 \cdot 2^{k} + 0, 0 \cdot 2^{k} + 1, \cdots, 0 \cdot 2^{k} + 2^{k} - 1] \\ &[1 \cdot 2^{k} + 0, 1 \cdot 2^{k} + 1, \cdots, 1 \cdot 2^{k} + 2^{k} - 1] \\ &[2 \cdot 2^{k} + 0, 2 \cdot 2^{k} + 1, \cdots, 2 \cdot 2^{k} + 2^{k} - 1] \\ &\vdots \\ &[m \cdot 2^{k} + 0, m \cdot 2^{k} + 1, \cdots, m \cdot 2^{k} + q = N \ (0 \leq q < 2^{k})] \\ \end{aligned} \]

\(x = 0, 1, 2, \sim \lceil N / 2^{k} \rceil - 2\) 的块一定都完整。
\(x = \lceil N / 2^{k} \rceil - 1\) 块可能不完整,即 \(r\) 不能取满 \(0, 1, \cdots, 2^{k} - 1\)

  • 考虑某个完整的块,即 \(0 \leq x < \lceil N / 2^{k} \rceil - 1\)

    由于 \(r\) 不超过 \(k\) 位,则 \(popcount(v = 2^{k} \cdot x + r) = popcount(v = (x << k) + r) = popcount(x) + popcount(r)\)

    一个块对应唯一 \(x\) ,则 \(popcount(x)\) 已知。这个块的贡献是:

    \[\sum_{ \substack{ r = 0, \\ popcount(x) + popcount(r) \equiv 1 (\bmod 2) } }^{2^{k} - 1} x \cdot 2^{k} + r \]

    考虑 \(k\) 个位置,选择一些位置为 \(1\) ,否则为 \(0\) ,容易证明(并且早就知道):

    \[\begin{aligned} \binom{k}{0} + \binom{k}{2} + \binom{k}{4} + \cdots \binom{k}{2 \lfloor k / 2 \rfloor} + \binom{k}{1} + \binom{k}{3} + \binom{k}{5} + \cdots \binom{k}{2 \lceil k / 2 \rceil - 1} = \sum_{i = 0}^{k} \binom{k}{i} = 2^{k} \\ \\ \binom{k}{0} + \binom{k}{2} + \binom{k}{4} + \cdots \binom{k}{2 \lfloor k / 2 \rfloor} = \binom{k}{1} + \binom{k}{3} + \binom{k}{5} + \cdots \binom{k}{2 \lceil k / 2 \rceil - 1} = 2^{k - 1} \\ \end{aligned} \]

    于是这个块的贡献是:

    \[\begin{aligned} &x \cdot 2^{k} \cdot 2^{k - 1} + \sum_{ \substack{ r = 0, \\ popcount(x) + popcount(r) \equiv 1 (\bmod 2) } }^{2^{k} - 1} r \\ &= x \cdot 2^{k} \cdot 2^{k - 1} + \begin{cases} \sum_{ \substack{ r = 0, \\ popcount(r) \equiv 0 (\bmod 2) } }^{2^{k} - 1} r,\ popcount(x) \equiv 1 (\bmod 2); \\ \sum_{ \substack{ r = 0, \\ popcount(r) \equiv 1 (\bmod 2) } }^{2^{k} - 1} r,\ popcount(x) \equiv 0 (\bmod 2). \\ \end{cases} \end{aligned} \]

    这里我们已经可以 \(O(2^{k})\) 预处理

    \[\begin{aligned} S_{even} = \sum_{ \substack{ r = 0, \\ popcount(r) \equiv 0 (\bmod 2) } }^{2^{k} - 1} r \\ S_{odd} = \sum_{ \substack{ r = 0, \\ popcount(r) \equiv 1 (\bmod 2) } }^{2^{k} - 1} r \\ \end{aligned} \]

    总共有 \(\lceil N / 2^{k} \rceil - 1\) 个块。

  • 考虑最后一个块,即 \(x = \lceil N / 2^{k} \rceil - 1\)
    可以暴力遍历 \(r \in [x \cdot 2^{k} + 0, N]\) ,若 \(popcount(x)\)\(popcount(r)\) 的奇偶性不同,则对答案贡献 \(x \cdot 2^{k} + r\)

综上,时间复杂度 \(O(max(N / 2^{k}, 2^{k}))\) 。如果把 \(k\) 取成 \(N\) 的二进制表示的一半,由 \(2^{a} \cdot 2^{b} = 2^{a + b}\) ,时间复杂度为 \(O(\sqrt{N})\)

Code
#include<iostream>
#include<cassert>const int32_t K = 16;
const int64_t BLOCK = int64_t(1) << K;int64_t S_even, S_odd;int64_t S(int64_t N) {if (N == 0) return 0;int64_t M = (N + BLOCK - 1) / BLOCK;int64_t rem = N % BLOCK;int64_t ret = 0;assert(M >= 1);for (size_t x = 0; x < M - 1; x++) {ret += int64_t(x) * (1 << K) * (1 << K - 1) + (__builtin_popcount(x) == 0 ? S_odd : S_even);}int32_t x = M - 1;for (size_t r = 0; r <= rem; r++) if ((__builtin_popcount(r) + __builtin_popcount(x) ) % 2 == 1) {ret += x * BLOCK + r;}return ret;
}int main(){std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);int64_t D, U;std::cin >> D >> U;for (size_t i = 0; i < BLOCK; i++) {if (__builtin_popcount(i) % 2 == 0) S_even += i;if (__builtin_popcount(i) % 2 == 1) S_odd += i;}std::cout << S(U) - S(D - 1) << "\n";return 0;
}

\(k \geq 2\) ,可以证明(有时间再说,有简单但是很长的构造,也有很短但是用生成函数构造)

\[\sum_{ \substack{i = 0, \\ popcount(i) \equiv 0 (\bmod 2) } }^{2^{k} - 1 } i = \sum_{ \substack{ i = 0, \\ popcount(i) \equiv 0 (\bmod 2) } }^{2^{k} - 1} i = \binom{2^{k}}{2} / 2 = 2^{k - 2} \cdot (2^{k} - 1) \]

那么代码可以优化成:

Code
#include<iostream>
#include<cassert>const int32_t K = 16;
const int64_t BLOCK = int64_t(1) << K;int64_t S(int64_t N) {if (N == 0) return 0;int64_t M = (N + BLOCK - 1) / BLOCK;int64_t rem = N % BLOCK;int64_t ret = 0;assert(M >= 1);for (size_t x = 0; x < M - 1; x++) {ret += int64_t(x) * (1 << K) * (1 << K - 1) + int64_t(1) * (1 << K - 2) * ((1 << K) - 1);}int32_t x = M - 1;for (size_t r = 0; r <= rem; r++) if ((__builtin_popcount(r) + __builtin_popcount(x) ) % 2 == 1) {ret += x * BLOCK + r;}return ret;
}int main(){std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);int64_t D, U;std::cin >> D >> U;std::cout << S(U) - S(D - 1) << "\n";return 0;
}

最后我突然发现好像能从 \(O(\sqrt{N})\) 优化到 \(O(1)\) …… 后面再说。

http://www.hskmm.com/?act=detail&tid=31257

相关文章:

  • 精确率
  • 前端知识图谱
  • 2025年振动电机厂家最新权威推荐榜:覆盖新型/高频/MV卧式/防爆/低噪声/三段式/直流/节能/侧板式振动电机的专业选购指南
  • 适度的情绪波动可以增加生活的丰富性和体验的深度
  • FBAM 论文浅析
  • 2025年上海律师服务最新权威推荐榜:经侦律师,民事纠纷律师,刑事律师,经济律师,婚姻律师,法务律师,负债律师事务所专业实力与口碑深度解析
  • 2025年冲压件厂家最新权威推荐榜:新能源/光伏/精密/异形/五金/铝/汽配/不锈钢/家具冲压件优质供应商精选
  • 10.14总结
  • UVa(紫书)做题记录
  • MyBatis 延迟加载使用及原理 - Higurashi
  • ADC-过零检测详解
  • 今日小雨
  • 内网穿透进阶:让 frpc 只代理「真正在线」的端口
  • 规则逻辑与人文逻辑的统一:AI元人文构想的演进之路
  • 2023 ICPC Jinan
  • 二叉树中和为目标值的路径
  • 动态库的调用方式
  • 校招面试官揭秘:我们到底在寻找什么样的技术人才?
  • day011
  • nginx
  • 打造一个比人类更懂 Python 的 AI 编程助手
  • 【黑马python】基础 5.Python 函数:参数 返回值 嵌套
  • linux 命令
  • 一试模拟试题(十七)problem 7 另(数竞相关)
  • PaddleOCR源码安装+centos7.6+python3.10
  • 以后尽量多更新
  • 10/14
  • 算法模版
  • newDay10
  • C#/.NET/.NET Core技术前沿周刊 | 第 57 期(2025年10.1-10.12)