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

CF2161G Editorial

Two language versions are available in order to serve both codeforces and luogu community.

English Version

Hints

  • What's the necessary condition for \(a_1^\prime\&a_2^\prime\cdots\&a_n^\prime=X\) to hold?

  • How can we ensure \(a_1^\prime\&a_2^\prime\cdots\&a_n^\prime\) is exactly equal to \(X\)?

  • Pay attention to some corner cases.

  • How can we optimize the procedure?

Solution

Step 1

We begin by considering a straightforward \(O(nq)\) approach.

A necessary condition for \(a_1^\prime\&a_2^\prime\cdots\&a_n^\prime=X\) to hold is that each modified value \(a_i^\prime\) must satisfy \(a_i^\prime\&X=X\).

So define \(up_X(a_i)\) is the minimum non-negative integer such that \((a_i+up_X(a_i))\&X=X\). Let \(b_i=a_i+up_X(a_i)\). To compute \(up_X(x)\),we locate the highest bit \(B\) (if any) where the binary representations of \(x\) and \(X\) differ.Then set it to \(1\) and keep the lower bits conform to \(X\).

After adjustment,let \(Y=b_1\&b_2\cdots\&b_n\). It satisfies that \(Y\&X=X\).

Next, we need to ensure that \(Y\) is exactly equal to \(X\). Similarly, locate the highest bit \(B\) (if any) and turn it to \(0\). If \(b_k\) is modified to turn \(B\) bit to \(0\). It can be shown that optimally, only one operation on a single element is sufficient to correct all lower differing bits as well.

When \(n\ge 2\), a potential issue arises: if we modify \(b_k\) on a bit where every element except \(b_k\) is \(1\) , \(Y\neq X\) again. To avoid this, it is generally optimal to operate on an element other than the one that would cause such a conflict.

Based on this, we can easily write a \(O(nq)\) code and submit.

Code for reference.

Step 2

After we get \(Y\), we try to speed up the procedure.

Let the bits of \(X\) be denoted as \(x_1,x_2\cdots x_k\) in descending order.

We process the bits from the highest to the lowest. At the \(i\)-th time,all \(a_i\) which contains \(x_1,x_2\cdots,x_{i-1}\) but not \(x_i\) are processed. It can be calculated by AND Fast Walsh-Hadamard Transform (FWT) and the Principle of Inclusion-Exclusion (PIE).

Code for reference.

Step 3

To make \(Y\) exactly \(X\), we identify the highest bit \(B\) where \(X\) and \(Y\) differ. We then look for an element \(a_k\) such that modifying it minimizes the number of operations required and it won't add new bits to \(Y\).

Let \(b\) be the highest bit changed in Step 2. We enumerate \([b+1,20]\). If at digit \(i\), \(0\) exists at least twice,find the number with
with the largest value in the lower \(i\) bits (i.e. \(a_k\&(2^i-1)\)) and compute it in \(O(1)\) time.

Note the special case where \(X=Y\).

Code for reference.

中文版本

Hint

• 要使 \(a_1^\prime\&a_2^\prime\cdots\&a_n^\prime=X\) 成立的必要条件是什么?

• 如何确保 \(a_1^\prime\&a_2^\prime\cdots\&a_n^\prime\) 恰好等于 \(X\)

• 注意一些边界情况。

• 如何加速操作过程?

Solution

Step 1

我们首先考虑一个直接的 \(O(nq)\) 方法。

要使 \(a_1^\prime\&a_2^\prime\cdots\&a_n^\prime=X\) 成立的一个必要条件是每个修改后的值 \(a_i^\prime\) 必须满足 \(a_i^\prime\&X=X\)
因此定义 \(up_X(a_i)\) 是满足 \((a_i+up_X(a_i))\&X=X\) 的最小非负整数。令 \(b_i=a_i+up_X(a_i)\)。为了计算 \(up_X(x)\),我们找到 \(x\)\(X\) 的二进制表示中不同的最高位 \(B\)(如果存在)。然后将其设置为 \(1\),并将低位调整为符合 \(X\)

调整后,令 \(Y=b_1\&b_2\cdots\&b_n\)。它满足 \(Y\&X=X\)

接下来,我们需要确保 \(Y\) 恰好等于 \(X\)。类似地,找到不同的最高位 \(B\)(如果存在)并将其变为 \(0\)。如果修改 \(b_k\)\(B\) 位变为 0。可以证明,最优情况下,只需对单个元素进行一次操作即可同时纠正所有较低的不同位。

\(n \ge 2\) 时,可能会出现一个问题:如果我们在某个位上修改 \(b_k\),而除了 \(b_k\) 之外的所有元素在该位上都是 1,那么 \(Y \neq X\) 会再次出现。为了避免这种情况,通常最优的操作是选择一个不会引起这种冲突的元素。

基于此,我们可以轻松编写 \(O(nq)\) 的代码并提交。

参考代码

Step 2

得到 \(Y\) 后,我们尝试加速该过程。

\(X\) 的位按降序表示为 \(x_1,x_2\cdots x_k\)

我们从高位到低位处理这些位。在第 \(i\) 次处理时,所有包含 \(x_1,x_2\cdots,x_{i-1}\) 但不包含 \(x_i\)\(a_i\) 将被处理。这可以通过卷积加容斥解决。

参考代码

Step 3

为了使 \(Y\) 恰好等于 \(X\),我们找到 \(X\)\(Y\) 不同的最高位 \(B\)。然后我们寻找一个元素 \(a_k\),使得修改它所需的操作次数最少,并且不会向 \(Y\) 添加新的位。

\(b\) 为第二步中改变的最高位。我们枚举 \([b+1,20]\)。如果在第 \(i\) 位,0 至少出现两次,则找到低 \(i\) 位中值最大的数(即 \(a_k\&(2^i-1)\))并在 \(O(1)\) 时间内计算。

注意 \(X=Y\) 的特殊情况。

参考代码

Reference

Pinely Round 5 editorial

http://www.jsqmd.com/news/55565/

相关文章:

  • 数组高阶方法:map、filter、reduce实战指南
  • 设备能力检测:自适应不同硬件环境
  • DuckDB:轻量级 OLAP 数据库的新星
  • 跨设备剪贴板数据:实现应用间内容共享
  • Text组件高级排版技巧:字体样式与文本布局深度优化
  • 通知与提醒系统:即时消息与日程管理实现
  • try/catch/finally:完善的错误处理策略
  • 4种XML解析方式详解
  • 20232415 2025-2026-1 《网络与系统攻防技术》实验七实验报告
  • 2025 Launch X431 PRO3 ACE: Online ECU Coding 38+ Services for Euro/Amer Vehicles with CANFD/DoIP
  • 2025-11-30 Expo Go(ios端)没有扫码的入口==》使用相机扫码,会自动弹出扫码入口,注意:开发环境和iphone连接的网络一定要是同一个!!
  • QtSingleapplication单实例-源码分析
  • 2025解决VS C# NUGET 安装System.Data.SQLite+SourceGear.SQLite3 不支持 AnyCPU 的系列问题
  • 102302156 李子贤 数据采集第四次作业
  • 东华萌新挑战赛 密室逃脱
  • 第40天(中等题 数据结构)
  • 核心功能详解
  • 2025-11-30-Nature Genetics | 本周最新文献速递
  • 效果-Element 3D
  • 2025苏州餐饮公司测评:苏州会议餐配送选这些,全区域超省心
  • 2025苏州承包食堂找哪家?苏州食堂承包优选清单
  • 2025不锈钢阀门厂家推荐:从口碑榜到销量榜清单在此
  • 2025脱酸脱盐设备公司有哪些:物料脱盐服务商优选指南
  • 2025隔音窗户哪个牌子好:广州隔音窗户哪家好盘点大测评
  • 截止阀厂家哪家好?2025截止阀品牌排行榜
  • 2025全自动吸吮式过滤器推荐厂家榜单
  • 旋片真空泵厂家有哪些2025真空系统厂家推荐
  • dotnet-dump安装、收集dump和崩溃自动收集dump
  • 虚拟机运行Vivado,部分界面显示不完全的问题
  • 《程序员修炼之道》笔记五