跳到正文

算法·理论

3.13总结

Wonderful_Anser约 11 分钟
原文

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

文章信息与版本

文章信息

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

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

分数

『FLA - I』冲云霄[KenOI 2025] 异或题异或积[BCSP-X 2024 6 月初中组] 打孔纸带[CSP-S 2020] 动物园总分
100pts100pts0pts0pts40pts40pts60pts60pts0pts0pts200pts200pts

题目

[KenOI 2025] 异或题

题意

给定 TT 组询问,每组给出一个整数 nn。要求找到两个整数 a,ba,b,满足 0≤a,b≤n0\le a,b\le n 且 a⊕b=na\oplus b=n,并输出 a+ba+b 的最大可能值。

思路

正解困难,采用逆向构造:初始取 a=n,b=0a=n,b=0,满足 a⊕b=na\oplus b = n;从高位到低位贪心调整 a,ba,b,最大化异或和。

用 Ai,BiA_i,B_i 表示 a,ba,b 的二进制第 ii 位:

  1. 当前位 Ai=0A_i=0:若 a+2i≤na+2^i\le n 且 b+2i≤nb+2^i\le n,令 Ai=1,Bi=1A_i=1,B_i=1,答案累加 2i+12^{i+1}。
  2. 当前位 Ai=1A_i=1:将 (Ai=1,Bi=0)(A_i=1,B_i=0) 改为 (Ai=0,Bi=1)(A_i=0,B_i=1)(和不变);仅当 ∣(a−2i)−(b+2i)∣<∣a−b∣|(a-2^i)-(b+2^i)| < |a-b| 时执行调整,使 a,ba,b 差值最小。

正确性:二进制性质 2x>∑i=1x−12i2^x>\sum_{i=1}^{x-1}2^i,高位优先贪心最优;a,ba,b 越接近,越易满足低位扩容条件。

复杂度:拆分 nn 的二进制位,共 O(log⁡n)O(\log n) 位,总复杂度 O(Tlog⁡n)O(T\log n)。

AC代码:

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

int T, n;
int b[40], m;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--)
    {
        cin >> n;
        m = 0;
        int x = n, y = 0, t = n;
        while (n > 0)
        {
            b[++m] = n % 2;
            n /= 2;
        }
        n = t;
        for (int i = m; i >= 1; i--)
        {
            int p = (1 << (i - 1));
            if (b[i] == 0 && x + p <= t && y + p <= t)
            {
                y += p;
                x += p;
            }
            else if (b[i] == 1)
            {
                if (abs(x - y) > abs((x - p) - (y + p)))
                {
                    x -= p;
                    y += p;
                }
            }
        }
        cout << x + y << "\n";
    }
    return 0;
}

时间复杂度:O(Tlog⁡n)O(T \log n)

异或积

题意

给定长度为 nn 的数组 aa,定义异或积变换:将数组 aa 替换为数组 bb,其中

bi=⨁j≠iajb_i=\bigoplus\limits_{j\neq i}a_j

求对数组进行 kk 次异或积变换后的结果。

核心结论

设数组总异或和 s=a1⊕a2⊕⋯⊕ans = a_1\oplus a_2\oplus\dots\oplus a_n,则一次异或积变换的结果为:

bi=s⊕aib_i = s \oplus a_i

变换规律

  1. nn 为奇数:k>0k>0 时,结果恒为一次变换后的数组
  2. nn 为偶数:
    • kk 为奇数:结果为一次变换后的数组
    • kk 为偶数:结果为原数组

AC代码

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

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        long long k;
        cin >> n >> k;
        
        vector<unsigned int> a(n);
        unsigned int s = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            s ^= a[i];
        }
        
        if (n % 2 == 1 && k > 0)
        {
            for (int i = 0; i < n; i++)
                cout << (s ^ a[i]) << " \n"[i == n - 1];
        }
        else if (n % 2 == 0 && k % 2 == 1)
        {
            for (int i = 0; i < n; i++)
                cout << (s ^ a[i]) << " \n"[i == n - 1];
        }
        else
        {
            for (int i = 0; i < n; i++)
                cout << a[i] << " \n"[i == n - 1];
        }
    }
    
    return 0;
}

时间复杂度:O(T⋅n)O(T \cdot n)

[BCSP-X 2024 6 月初中组] 打孔纸带

题意简述

给定若干条 01 打孔纸带,机器对两条纸带的每一位进行 与、或、异或 其中一种运算。 求最少需要新制作多少条纸带,才能唯一确定机器的运算规则。

核心思路

每一位独立判断:

  • 要唯一区分三种运算,该位必须同时出现 0 和 1,且 至少两个 1。
  • 对每一位计算需要补充的最少纸带数,答案取所有位的最大值。

AC代码

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

const int N = 1555;
int n, ans;
int cnt0[N], cnt1[N];
string s;

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int T;
    cin >> T;
    while (T--)
    {
        cin >> s;
        int len = s.size();
        n = max(n, len);
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '0') cnt0[i]++;
            else cnt1[i]++;
        }
    }
    
    for (int i = 0; i < n; i++)
    {
        int res = 0;
        if (cnt0[i] == 0) res++;
        if (cnt1[i] < 2) res += 2 - cnt1[i];
        ans = max(ans, res);
    }
    
    cout << ans;
    return 0;
}

时间复杂度:O(T×L)O(T \times L)

[CSP-S 2020] 动物园

核心思路

  1. 本题本质是二进制位运算,cc 和饲料编号无意义,直接忽略
  2. 用按位或统计所有动物已存在的二进制位
  3. 标记必须为 00 的二进制位,剩余自由位数量为 tt
  4. 答案 = 总方案数 2t−n2^t - n(nn 为已有动物数量)
  5. 特殊情况:t=64t=64 且 n=0n=0 时,输出 2642^{64} 字符串

AC代码

C++
#include <bits/stdc++.h>
#define ULL unsigned long long
using namespace std;

int main() 
{
    int n, m, c, K;
    cin >> n >> m >> c >> K;

    ULL flag = 0, g = 0;
    for (int i = 0; i < n; i++)
    {
        ULL x;
        cin >> x;
        flag |= x;
    }
    
    while (m--)
    {
        int p;
        int tmp;
        cin >> p >> tmp;

        if ((flag >> p & 1) == 0)
            g |= 1ULL << p;
    }

    ULL ans = 1;
    for (int i = 0; i < K; i++)
    {
        if ((g >> i & 1) == 0)
            ans <<= 1;
    }

    if (ans == 0 && n == 0)
    {
        cout << "18446744073709551616" << "\n";
        return 0;
    }

    ans -= n;
    cout << ans << "\n";

    return 0;
}

时间复杂度:O(n+m)O(n + m)