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

题解:AtCoder ARC208C Mod of XOR

题意

给定 \(C,X\),构造一个 \(n(1\leq n<2^{60})\) 使得 \((n\oplus C)\bmod{n}=X\),或报告无解。多测,\(1\leq T\leq 2\times 10^5\)\(1\leq C,X<2^{30}\)

题解

神人构造题。

显然要有 \(n>X\)。不妨设 \(n\oplus C=qn+X\),分类讨论 \(q\) 的取值。

\(q=0\)

此时 \(n\oplus C=X\Leftrightarrow n=C\oplus X\),于是如果 \((C\oplus X)>X\) 成立,这就是一个合法解。

\(q=1\)

此时 \(n\oplus C=n+X\)。考虑把 \(n\oplus C\) 拆成 \(n+C-2(n\operatorname{and} C)\),这样就变成了 \(C-2(n\operatorname{and} C)=X\)。设 \(d=C-X\)

  • \(d\) 是奇数或 \(d<0\),则 \(q=1\) 的 case 无解。
  • \(d\) 是非负偶数,但是 \(\left(\dfrac{d}{2}\operatorname{and} C\right)\neq C\),则同样无解。
  • \(d\) 是非负偶数,且 \(\left(\dfrac{d}{2}\operatorname{and} C\right)=C\),我们令 \(n=\left(\dfrac{d}{2}\operatorname{and} C\right)+2^{30}\) 即可。

\(q\geq 2\)

我们指出,若上面两种 case 都无解,则这种 case 也无解。

证明:反证法,假设 \(0\leq q\leq 1\) 的 case 都无解,且 \(q=2\) 的 case 有解。很显然 \(qn+X=(n\oplus C)\leq n+C\),因此 \(C\geq (q-1)n+X>2X\)。而注意到 \(q=0\) 的 case 无解等价于 \((C\oplus X)\leq X\),这和 \(C>2X\) 矛盾。\(\Box\)


按照上述分类讨论直接实现即可,时间复杂度 \(\mathcal{O}(T)\)

代码

#include <bits/stdc++.h>using namespace std;#define lowbit(x) ((x) & -(x))
typedef long long ll;
typedef pair<int, int> pii;template<typename T> inline void chk_min(T &x, T y) { x = min(x, y); }
template<typename T> inline void chk_max(T &x, T y) { x = max(x, y); }int T, c, x;int main() {ios::sync_with_stdio(0), cin.tie(0);cin >> T;while (T--) {cin >> c >> x;int n = c ^ x;if (n > x) { cout << n << '\n'; continue; }int d = c - x;if (d >= 0 && (~d & 1)) {d >>= 1;if ((d & c) == d) cout << (d | (1ll << 30)) << '\n';else cout << "-1\n";} else cout << "-1\n";}return 0;
}
http://www.jsqmd.com/news/17853/

相关文章:

  • 题解:Luogu P6898 [ICPC 2014 WF] Metal Processing Plant
  • 32-腾讯IM接入资料和定价
  • 题解:AtCoder ARC207A Affinity for Artifacts
  • 题解:Luogu P9260 [PA 2022] Miny
  • 题解:Luogu P13544 [OOI 2022] Serious Business
  • 题解:Luogu P14254 分割(divide)
  • 31_创蓝短信接入资料和定价
  • 02.Python百行代码实现抽奖系统
  • CSP-S 35
  • 题解:P11662 [JOI 2025 Final] 方格染色 / Grid Coloring
  • CSP-S 32 多校5
  • CSP-S 33
  • CSP-S 29
  • 10.20每日总结
  • CSP-S 31
  • 2025网络安全振兴杯wp
  • 后缀树
  • ES原理、zookeeper、kafka
  • CF1606E Arena 题解(动态规划)
  • 服务器CPU市场概况2025
  • CSP-S 24
  • 正睿 2025 NOIP20 连测 Day5 做题记录
  • 29-腾讯云COS接入指南与价格说明
  • LLM学习记录DAY7
  • CSP-S 23
  • Recall
  • CSP-S 20
  • Flutter应用设置插件 - 轻松打开iOS和Android系统设置
  • CSP-S 22
  • /usr/bin/sudo 二进制文件的权限有问题,导致所有用户都无法使用 sudo