跳到正文

个人记录

3.20总结

Wonderful_Anser约 10 分钟
原文

这是 2026年3月24日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月24日
收录
2026年3月24日
更新
2026年11月14日
篇幅
约 10 分钟

版本

  1. v3当前
  2. v2
  3. v1

比赛情况

P7071 优秀的拆分P1461 海明码P13591 Kitchen TimerP11485 Non-breath ObligeP10307 「Cfz Round 2」Binary总分
35pts\footnotesize\color{orange}\textsf{\textbf{35pts}}AC\footnotesize\color{green}\textsf{\textbf{AC}}57pts\footnotesize\color{orange}\textsf{\textbf{57pts}}192pts

题目

P7071 [CSP-J 2020] 优秀的拆分

题意

题目要求将正整数 nn 拆分成若干个不同的 22 的正整数次幂之和。

思路

1. 奇偶性判断

所有 22 的正整数次幂都是偶数(2,4,8,…2, 4, 8, \dots)。任意多个偶数相加,结果仍然是偶数。因此,如果 nn 是奇数,一定无法拆分成若干个偶数的和,直接输出 -1。

2. 偶数情况的拆分

当 nn 为偶数时,一定存在优秀的拆分。实际上,任意偶数都可以写成二进制形式,例如:

  • 610=1102=4+26_{10} = 110_2 = 4 + 2
  • 1410=11102=8+4+214_{10} = 1110_2 = 8 + 4 + 2

由于 nn 是偶数,其二进制最低位为 00,所以二进制表示中不包含 202^0,恰好满足题目要求。

3. 贪心策略

从大到小枚举 22 的幂:

  • 从 2302^{30} 开始向下检查(因为 n≤107<230n \le 10^7 < 2^{30})
  • 如果当前幂 2i≤n2^i \le n,则选择它,并从 nn 中减去
  • 继续检查更小的幂

这样得到的拆分方案是从大到小排列的,且是唯一的。

代码实现

C++
#include <bits/stdc++.h>
using namespace std;

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    // 奇数无解
    if (n % 2 != 0)
    {
        cout << "-1";
        return 0;
    }

    // 从大到小枚举 2 的幂
    for (int i = 30; i >= 1; i--)
    {
        if ((1 << i) <= n)
        {
            n -= (1 << i);
            cout << (1 << i) << " ";
        }
    }

    return 0;
}

时间复杂度

循环固定 30 次,时间复杂度 O(1)O(1),可以轻松通过 n≤107n \le 10^7 的数据范围。

样例验证

输入输出说明
64 26=4+26 = 4 + 2
7-1奇数,无解
12664 32 16 8 4 2126=64+32+16+8+4+2126 = 64 + 32 + 16 + 8 + 4 + 2

P1461 [USACO2.1] 海明码 Hamming Codes

题意

题目要求找出 nn 个 bb 位的二进制编码,使得任意两个编码之间的汉明距离至少为 dd。汉明距离是指两个二进制串中不同位的个数。

需要输出字典序最小的解,即把编码看成二进制数转成十进制后,从小到大输出,每行最多 10 个数。

思路

1. 数据范围
  • n≤64n \le 64

  • b≤8b \le 8,因此编码范围是 00 到 2b−12^b - 1,最大为 255255

  • d≤7d \le 7

由于搜索空间很小(最多 256 个数),可以采用贪心。

2. 贪心策略

从 0 开始,从小到大依次检查每个数:

  • 如果当前数与已经选中的每个数的汉明距离都 ≥d\ge d,就把它加入答案
  • 否则跳过

这样得到的集合一定是字典序最小的,因为我们是按数值从小到大尝试的。

3. 海明距离计算

两个数 aa 和 bb 的汉明距离,就是它们二进制表示中不同位的个数。可以用异或运算:a⊕ba \oplus b,然后统计结果中 1 的个数。

代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, b, d, cnt, a[105];
// 检查两数汉明距离是否 >= d
bool hm(int a, int b)
{
    int cnt = 0;
    for (int i = 60; i >= 0; i--)
    {
        int t1 = (a >> i) & 1;
        int t2 = (b >> i) & 1;
        if (t1 != t2)
            cnt++;
    } 
    return cnt >= d;
}
signed main()
{
    cin >> n >> b >> d;
    a[++cnt] = 0; // 0必选
    int o = pow(2, b) - 1; // b位最大值
    for (int i = 1; i <= o; i++)
    {
        bool v = 0;
        for (int j = 1; j <= cnt; j++)
            if (hm(i, a[j]) == 0)
            {
                v = 1;
                break;
            }
        if (v == 0)
            a[++cnt] = i;
        if (cnt == n)
            break;
    }
    for (int i = 1; i <= n; i++)
    {
        cout << a[i] << " ";
        if (i % 10 == 0)
            cout << "\n";
    }
    return 0;
}

P11485 「Cfz Round 5」Non-breath Oblige 题解

题意

每次操作可以把 ss 改成 xx,要求:

  • x<2nx < 2^n
  • s∨x=2n−1s \lor x = 2^n - 1
  • 代价为 s⊕xs \oplus x

求 ss 变到 tt 的最小总代价。

分类讨论

设 F=2n−1F = 2^n - 1

1. s=ts = t

不需要操作,代价为0。

2. n=1n = 1

只有0和1两个数,只能互相变,代价固定为1。

3. s+t=Fs + t = F

此时 tt 是 ss 的按位取反,满足 s∨t=Fs \lor t = F,可以直接一步从 ss 到 tt,代价为 s⊕ts \oplus t。

4. s∨t=Fs \lor t = F

可以直接一步从 ss 到 tt,代价为 s⊕ts \oplus t。

5. 其他情况

无法一步到位,最优路径是先从 ss 到 FF,再从 FF 到 tt,总代价为 (s⊕F)+(F⊕t)(s \oplus F) + (F \oplus t)。

代码

C++
#include <bits/stdc++.h>
using namespace std;
long long T, n, s, t; 
int main()
{
    cin >> T;
    while (T--)
    {
        cin >> n >> s >> t;
        if (s == t) // 第一种情况
        {
            cout << "0\n";
        } else
        {
            if (n == 1) // 第二种情况
            {
                cout << "1\n";
            } else
            {
                if (s + t == (1LL << n) - 1) // 第三种情况
                {
                    cout << (s ^ t) << "\n";
                } else
                {
                    if ((s | t) != (1LL << n) - 1) // 第四种情况
                    {
                        cout << (s ^ ((1LL << n) - 1)) + (((1LL << n) - 1) ^ t) << "\n";
                    } else // 其他情况
                    {
                        cout << (s ^ t) << "\n";
                    }
                } 
            }
        }
    }
    return 0;
}