跳到正文

题解

构造笑传之 Cow Cow Bessie

clx201022约 19 分钟
原文
文章信息与版本

文章信息

收录
2026年2月8日
更新
2026年2月8日
篇幅
约 19 分钟
题目
P12025

版本

  1. v1当前

一道很神秘的构造题。

分析

注意到题目要我们构造的三个性质中,第三个性质最神秘。因此考虑先构造出满足第三个性质的序列 aa,再在不破坏序列 aa 的基础上修改,尝试使其满足所有性质。

初始的 aa

如何构造初始的 aa?因为后续修改 aa 的时候,我们肯定希望只需考虑增加元素而不必考虑减少元素,而又由于 ∑i=1na=M\sum_{i=1}^{n} a=M 的限制,所以我们同时希望 aa 的元素个数及其总和最小。

KK 的每一位 11,都至少需要一个 popcount⁡(ai)\operatorname{popcount}(a_i) 满足,因此至少需要 popcount⁡(K)\operatorname{popcount}(K) 个 aa。设 bib_i 为 KK 的二进制位,则形式化的定义为 ∑i=1popcount⁡(K)bi=K\sum^{\operatorname{popcount}(K)}_{i=1}b_i=K;∀bj∃x,bj=2x\forall b_j\exist x,b_j=2^x。而由于要求 aa 的总和尽可能小,popcount⁡(ai)\operatorname{popcount}(a_i) 的总和也应尽可能小。

若设 ∀i,popcount⁡(ai)=bi\forall i ,\operatorname{popcount}(a_i)=b_i,则 ∑popcount⁡(ai)=⨁popcount⁡(ai)\sum\operatorname{popcount}(a_i)=\bigoplus \operatorname{popcount}(a_i),即此时 popcount⁡(ai)\operatorname{popcount}(a_i) 的总和取最小值。

在此基础上,当 popcount⁡(ai)\operatorname{popcount}(a_i) 恒定时,aia_i 中零最少时 aia_i 最小,具体为 2popcount⁡(ai)−12^{\operatorname{popcount}(a_i)}-1。

证明

ai≠0a_i\ne 0 且其二进制表示(无前导零)存在 00 时,总可以通过将一位 00 变为 11 而将最高位变为 00 而使得其变小。

此时,其最小(不能再变小)等同于其二进制表示(无前导零)不存在 00。

构造出最小的 aa 后,若仍有 ∑popcount⁡(ai)>M\sum\operatorname{popcount}(a_i)>M,则不存在合法的 aa。

修改

注意到 popcount⁡(ai)\operatorname{popcount}(a_i) 在 ai←ai×2a_i\gets a_i\times 2 时不变;而异或有性质 x⊕0=x,x⊕x=0x\oplus 0=x,x\oplus x=0。就此,定义两种操作:

  • 选择 ii,使 ai←ai×2a_i\gets a_i\times 2。
  • 同时将两个 xx 加入 aa。

上述两个操作都不会影响 ⨁popcount⁡(ai)\bigoplus\operatorname{popcount}(a_i),但可以使 ∑popcount⁡(ai)\sum\operatorname{popcount}(a_i) 增加。

定义 δ=M−∑popcount⁡(ai)\delta=M-\sum\operatorname{popcount}(a_i),则问题转化为了依靠上述两种操作使 δ=0\delta= 0。

因为加法运算满足高位不会影响低位,考虑从最低位开始按位操作 δ\delta。设 δ\delta 的最低位 11 为 δlowbit\delta_{lowbit},不断重复 δ←δ−δlowbit\delta\gets \delta- \delta_{lowbit} 的操作,即可将 δ\delta 置 00。

如何使 δ←δ−δlowbit\delta\gets \delta- \delta_{lowbit}?

δlowbit=1\delta_{lowbit}=1 时,若 ∃ai=1\exist a_i=1,则 ai←ai×2a_i\gets a_i\times 2 即等同于 δ←δ−1\delta\gets \delta -1;∄ai=1\nexists a_i=1 ,当且仅当 δ≥3\delta\ge 3 时可以将两个 11 加入 aa 后再进行操作,否则无解。

δlowbit>1\delta_{lowbit}>1,同时将两个 δlowbit2\frac{\delta_{lowbit}}{2} 加入 aa 即可完成操作。

δlowbit=0\delta_{lowbit}=0 时,得到答案。

1≤N≤1001\le N\le 100 限制无需担心,初始 aa 长度为 log⁡K≤31\log K\le 31,后续增加的 ΔN≤2log⁡δ+ϵ\Delta N\le 2\log \delta+\epsilon,其中 ϵ\epsilon 为一个 δlowbit=1\delta_{lowbit}=1 时所用的额外长度。而题目数据限制使其不可能超过 NN。

实现

使用了优先队列维护是否有 11,不过似乎没有必要。

AC
C++
#include <assert.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <queue>
#include <iostream>
#include <cctype>
#include <cstdint>
#include <vector>
#include <bitset>
#include <map>
#include <cassert>
#include <iostream>
#include <cctype>
#include <cstdint>
namespace fast_IO
{
    constexpr uint32_t IOSIZE = 100000;
    uint32_t precision = 6, POW[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; // ����ȡ����С������ precision λ
    char obuf[IOSIZE], *p3 = obuf;
#if defined(ONLINE_JUDGE) or defined(FastIO)
    char ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf;
#define getchar() ((p1 == p2) and (p2 = (p1 = ibuf) + fread(ibuf, 1, IOSIZE, stdin), p1 == p2) ? (EOF) : (*p1++))
#define putchar(x) ((p3 == obuf + IOSIZE) && (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf), *p3++ = x)
#endif
    template <typename T>
    inline bool read(T &s)
    {
        s = 0;
        int w = 1;
        char ch;
        while (ch = getchar(), !(ch > 47 and ch < 58) and (ch != EOF))
            if (ch == '-')
                w = -1;
        if (ch == EOF)
            return false;
        while ((ch > 47 and ch < 58))
            s = s * 10 + (ch - 48), ch = getchar();
        return s *= w, true;
    }
    template <typename T>
    inline void print(T x)
    {
        if (x < 0)
            putchar('-'), x = -x;
        if (x > 9)
            print(x / 10);
        putchar(x % 10 + 48);
    }
    inline bool read(char &s)
    {
        while (s = getchar(), isspace(s) and s != EOF)
            ;
        return s != EOF;
    }
    inline void print(char x) { putchar(x); }
    inline bool read(char *s)
    {
        char ch;
        while (ch = getchar(), (ch < 33) && ch != EOF)
            ;
        if (ch == EOF)
            return false;
        while (!(ch < 33) and (ch != EOF))
            *s++ = ch, ch = getchar();
        *s = (char)0;
        return true;
    }
    inline void print(char *x)
    {
        while (*x)
            putchar(*x++);
    }
    inline void print(const char *x)
    {
        for (int i = 0; x[i]; i++)
            putchar(x[i]);
    }
    inline bool read(std::string &s)
    {
        s = "";
        char ch;
        while (ch = getchar(), (ch < 33) and ch != EOF)
            ;
        if (ch == EOF)
            return false;
        while (!(ch < 33))
            s += ch, ch = getchar();
        return true;
    }
    inline void print(std::string x)
    {
        for (int i = 0, n = x.size(); i < n; i++)
            putchar(x[i]);
    }
    inline bool read(bool &b)
    {
        char ch;
        while (ch = getchar(), (ch < 33) and ch != EOF)
            ;
        return ch == EOF ? false : (b = ch ^ 48, true);
    }
    inline void print(bool b) { putchar(b + 48); }
    inline bool read(double &x)
    {
        char ch = getchar();
        int f = 1;
        for (x = 0; (ch < 48 or 57 < ch) and (ch != EOF); ch = getchar())
            if (ch == '-')
                f = -1;
        if (ch == EOF)
            return false;
        for (x = 0; 47 < ch and ch < 58; ch = getchar())
            x = x * 10 + (ch ^ 48);
        if (ch != '.')
            return x *= f, true;
        double y = 0.1;
        for (ch = getchar(); 47 < ch and ch < 58; ch = getchar())
            x += y * (ch ^ 48), y /= 10;
        return x *= f, true;
    }
    inline void print(double x)
    {
        if (x < 0)
            putchar('-'), x = -x;
        if (!precision)
            print((unsigned long long)(x - (unsigned long long)(x) >= 0.5 ? x + 1 : x));
        else
        {
            unsigned long long xx = x;
            double y = ((x - xx) * POW[precision]);
            unsigned long long yy = (y - (unsigned long long)(y) >= 0.5 ? y + 1 : y);
            if (yy == POW[precision])
                xx++, yy = 0;
            print(xx), putchar('.');
            for (int j = precision - 1; ~j; j--)
                putchar(48 + yy / POW[j] % 10);
        }
    }
    template <typename T, typename... T1>
    inline int read(T &a, T1 &...other) { return read(a) + read(other...); }
    template <typename T, typename... T1>
    inline void print(T a, T1... other) { print(a), print(other...); }
    template <typename T>
    inline T read()
    {
        T a;
        return read(a), a;
    }
    struct Fast_IO
    {
        bool flag = 1;
        inline ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); }
        inline void SetPrecision(int x)
        {
            if (x > 9)
                throw std::runtime_error("Precision too high!");
            else if (x < 0)
                throw std::runtime_error("Precision too low!");
            else
                precision = x;
        } // ������������Ϊ x������ȷ��С����� x λ��
        inline operator bool() { return flag; }
    } io;
    Fast_IO &operator>>(Fast_IO &io, char *b) { return io.flag &= read(b), io; }
    template <typename T>
    Fast_IO &operator>>(Fast_IO &io, T &b) { return io.flag &= read(b), io; }
    template <typename T>
    Fast_IO &operator<<(Fast_IO &io, T b) { return print(b), io; }
    constexpr char endl = 10;
}
using fast_IO::endl;
using fast_IO::io;
using ll = long long;
using ull = unsigned long long;
const std::size_t maxn = 100;
ull M, K;
ull lowbit(ull x)
{
    return x & -x;
}
ull popcount(ull x)
{
    ull ret = 0;
    while (x)
    {
        ret += (x & 1);
        x >>= 1;
    }
    return ret;
} // log x
int main()
{
#ifndef ONLINE_JUDGE
    freopen("d.in", "r", stdin);
    freopen("d.out", "w", stdout);
#endif
    int T;
    io >> T;
    while (T--)
    {
        io >> M >> K;
        std::priority_queue<ull, std::vector<ull>, std::greater<ull>> a;
        while (K)
        {
            ull z = (1 << lowbit(K)) - 1;
            if (M < z)
                break;
            a.push(z);
            M -= z;
            K ^= lowbit(K);
        } // popcount 已满足
        if (K)
            goto impossible; // popcount 不可能满足
        // M is what need_plus
        while (M)
        {
            if (lowbit(M) == 1)
            {
                if (!a.empty() && a.top() == 1)
                {
                    a.pop();
                    a.push(2);
                    M -= 1;
                } // 不能靠提升之前的 1
                else if (M >= 3)
                {
                    a.push(2);
                    a.push(1);
                    M -= 3;
                }
                else
                    goto impossible;
            }
            a.push(lowbit(M) >> 1);
            a.push(lowbit(M) >> 1);
            M -= lowbit(M);
        }
        while (a.top() == 0)
            a.pop();
        io << a.size() << endl;
        while (!a.empty())
        {
            io << a.top();
            a.pop();
            if (a.empty())
                io << endl;
            else
                io << ' ';
        }
        continue;
    impossible:
        io << -1 << endl;
        continue;
    }
    return 0;
}

::anti-ai[本文作者是 clx201022,洛谷 UID 为 552688,原文:<https://www.luogu.com.cn/article/aawya8d4> 如果您能直接看到这段文字而在本页没有转载提示,或以“原创”名义发出,说明您访问的是侵权内容,请联系管理员删除文章并处罚侵权人,十分感谢您的见义勇为之举。]

创作声明

本文遵循 CC BY-SA 4.0 协议。

保证本文未使用 AI 工具辅助创作。

原文