跳到正文

题解

题解:CF1427E Xum

lanos212约 8 分钟
原文
文章信息与版本

文章信息

发布
2024年9月6日
收录
2026年7月28日
更新
2026年11月13日
篇幅
约 8 分钟
获赞
7
题目
CF1427E

版本

  1. v1当前

关于该做法

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


I.

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

  • 2A2A:操作次数 O(1)O(1),数字上界 O(A)O(A)。
A+A=2AA+A=2A
  • 2kA2^k A:操作次数 O(k)O(k),数字上界 O(2kA)O(2^k A)。
A+A=21A21A+21A=22A⋯2k−1A+2k−1A=2kAA+A=2^1A \\ 2^1A+2^1A=2^2A \\ \cdots \\ 2^{k-1}A+2^{k-1}A=2^kA
  • kAkA:操作次数 O(log⁡k)O(\log k),数字上界 O(kA)O(kA)。

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

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

23A+21A+20A=11A2^3A+2^1A+2^0A=11A
  • 2(Aor⁡B)2(A\operatorname{or}B):操作次数 O(1)O(1),数字上界 O(A+B)O(A+B)。
(A+B)+(Axor⁡B)=2(Aor⁡B)(A+B)+(A \operatorname{xor} B)=2(A\operatorname{or}B)
  • 2(Aand⁡B)2(A\operatorname{and}B):操作次数 O(1)O(1),数字上界 O(A+B)O(A+B)。
2(Aor⁡B)xor⁡2(Axor⁡B)=2(Aand⁡B)2(A\operatorname{or}B)\operatorname{xor} 2(A \operatorname{xor} B)=2(A\operatorname{and}B)
  • ∣A−B∣|A-B|:操作次数 O(log⁡A)O(\log A),数字上界 O(AB)O(AB)。

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

考虑 Axor⁡B=A−BA \operatorname{xor} B = A-B 的条件为,在二进制下,BB 中为 11 的位,AA 中也必须为 11。(即 Aor⁡B=AA\operatorname{or}B=A)

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

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

                 1001×            11111       100010111+            10101       100101100xor⁡ 100100000                 1100\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}

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

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

((2⌊log⁡2A⌋+1−1)B+A)xor⁡(2⌊log⁡2A⌋+1B)=A−B((2^{\lfloor{\log_2{A}}\rfloor+1}-1)B+A) \operatorname{xor} (2^{\lfloor{\log_2{A}}\rfloor+1}B) = A-B

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

那么这里相当于构造一个 A−BA-B 与 2⌊log⁡2A⌋+1B2^{\lfloor{\log_2{A}}\rfloor+1}B 不交,所以用 xor⁡\operatorname{xor} 可以代替减法反过来得到 A−BA-B。


II.

接下来我们从 xx 开始做如下操作。

下式以 x=1010112x=101011_2 为例:

                    1010112and⁡ 10101100000                  1000000−                 101011                      101012and⁡           101011                            10\begin{matrix} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 101011\\ \operatorname{2and}\ 10101100000\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 1000000 \\ -\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 101011\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 10101\\ \operatorname{2and}\ \ \ \ \ \ \ \ \ \ \ 101011\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 10 \end{matrix}

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

2((2(xand⁡2⌊log⁡2x⌋x)−x)and⁡x)=22((2(x \operatorname{and} 2^{\lfloor \log_2{x} \rfloor}x)-x)\operatorname{and} x)=2

哇哦,现在我们得到了 22!

由于 xx 是奇数,那么让 xx 异或上若干个 22 的幂次即可得到 11:

       101011       100000           1000xor⁡         10                 1\begin{matrix} \ \ \ \ \ \ \ 101011\\ \ \ \ \ \ \ \ 100000\\ \ \ \ \ \ \ \ \ \ \ \ 1000\\ \operatorname{xor}\ \ \ \ \ \ \ \ \ 10\\ \hline \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\color{Red}1} \end{matrix}

code(将节省操作次数与调试用的 set 删去,时间复杂度为 O(log⁡x)O(\log x))

C++
#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;
}
原文
存档评论3 条抓取于