题解:CF1427E Xum

· · 题解

关于该做法

总操作次数 O(\log x),数字大小上界 O(x^2),时间复杂度 O(\log x),目前各指标最优解。

I.

我们先凑出一些基本的运算操作。

A+A=2A A+A=2^1A \\ 2^1A+2^1A=2^2A \\ \cdots \\ 2^{k-1}A+2^{k-1}A=2^kA

这一部分类似快速幂,对二进制位进行拆分即可。

例如,可以用下式得到 11A:

2^3A+2^1A+2^0A=11A (A+B)+(A \operatorname{xor} B)=2(A\operatorname{or}B) 2(A\operatorname{or}B)\operatorname{xor} 2(A \operatorname{xor} B)=2(A\operatorname{and}B)

假设 A\ge B,不满足交换它们即可。

考虑 A \operatorname{xor} B = A-B 的条件为,在二进制下,B 中为 1 的位,A 中也必须为 1。(即 A\operatorname{or}B=A)

故我们可以通过大幅度的移位来得到 A-B。

下式以 A=10101_2,B=1001_2 为例:

\begin{matrix} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\color{Red}1001}\\ \times\ \ \ \ \ \ \ \ \ \ \ \ 11111\\ \hline \ \ \ \ \ \ \ {\color{Red}100}{\color{Orange}0}10111\\ +\ \ \ \ \ \ \ \ \ \ \ \ 10101\\ \hline \ \ \ \ \ \ \ {\color{Red}1001}01100\\ \operatorname{xor}\ {\color{Red}1001}00000\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1100 \end{matrix}

这样最后一步的 \operatorname{xor} 运算就与 - 运算等价了。

用一般的式子写就是下式:

((2^{\lfloor{\log_2{A}}\rfloor+1}-1)B+A) \operatorname{xor} (2^{\lfloor{\log_2{A}}\rfloor+1}B) = A-B

其实用到的是 \operatorname{xor} 的性质,当运算的两个数没有 1 的数位相交时,它们的 \operatorname{xor} 和它们相加的值是一样的。

那么这里相当于构造一个 A-B 与 2^{\lfloor{\log_2{A}}\rfloor+1}B 不交,所以用

--- ### II. 接下来我们从 $x$ 开始做如下操作。 下式以 $x=101011_2$ 为例: $$ \begin{matrix} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 101011\\ \operatorname{2and}\ 10101100000\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1000000 \\ -\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 101011\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 10101\\ \operatorname{2and}\ \ \ \ \ \ \ \ \ \ \ 101011\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 10 \end{matrix} $$ 用一般的式子写就是下式: $$ 2((2(x \operatorname{and} 2^{\lfloor \log_2{x} \rfloor}x)-x)\operatorname{and} x)=2 $$ 哇哦,现在我们得到了 $2$! 由于 $x$ 是奇数,那么让 $x$ 异或上若干个 $2$ 的幂次即可得到 $1$: $$ \begin{matrix} \ \ \ \ \ \ \ 101011\\ \ \ \ \ \ \ \ 100000\\ \ \ \ \ \ \ \ \ \ \ \ 1000\\ \operatorname{xor}\ \ \ \ \ \ \ \ \ 10\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\color{Red}1} \end{matrix} $$ code(将节省操作次数与调试用的 set 删去,时间复杂度为 $O(\log x)$) ```cpp #include <bits/stdc++.h> #define int long long using namespace std; set<int> S; int n; vector<array<int, 3>> ans; inline bool find(int x) { return (S.find(x) != S.end()); } inline int ADD(int x, int y) { assert(find(x) && find(y)); if (!find(x + y)) ans.push_back({x, 0, y}), S.insert(x + y); return x + y; } inline int XOR(int x, int y) { assert(find(x) && find(y)); if (!find(x ^ y)) ans.push_back({x, 1, y}), S.insert(x ^ y); return x ^ y; } inline int SUB(int x, int y) { if (x < y) swap(x, y); int lim = 0; while (((y << lim) & (-(y << lim))) <= x) ADD(y << lim, y << lim), ++lim; int na = x; for (int i = 0; i < lim; ++i) na = ADD(na, y << i); return XOR(na, y << lim); } inline int OR_2(int x, int y) { return ADD(ADD(x, y), XOR(x, y)); } inline int AND_2(int x, int y) { return XOR(ADD(XOR(x, y), XOR(x, y)), OR_2(x, y)); } signed main() { ios::sync_with_stdio(false), cin.tie(0); cin >> n; S.insert(n); int np = n; while ((np & -np) * 2 <= n) np = ADD(np, np); int P = AND_2(n, np); int m = SUB(P, n); int two = AND_2(n, m); for (int i = 1; i < 20; ++i) ADD((1ll << i), (1ll << i)); for (int i = 19; i; --i) if (n >> i & 1) n = XOR(n, (1ll << i)); assert(n == 1); cout << ans.size() << '\n'; for (auto [x, op, y] : ans) cout << x << ' ' << (op ? '^' : '+') << ' ' << y << '\n'; return 0; } ```