分析
注意到题目要我们构造的三个性质中,第三个性质最神秘。因此考虑先构造出满足第三个性质的序列 ,再在不破坏序列 的基础上修改,尝试使其满足所有性质。
初始的
如何构造初始的 ?因为后续修改 的时候,我们肯定希望只需考虑增加元素而不必考虑减少元素,而又由于 的限制,所以我们同时希望 的元素个数及其总和最小。
的每一位 ,都至少需要一个 满足,因此至少需要 个 。设 为 的二进制位,则形式化的定义为 ;。而由于要求 的总和尽可能小, 的总和也应尽可能小。
若设 ,则 ,即此时 的总和取最小值。
在此基础上,当 恒定时, 中零最少时 最小,具体为 。
证明
且其二进制表示(无前导零)存在 时,总可以通过将一位 变为 而将最高位变为 而使得其变小。
此时,其最小(不能再变小)等同于其二进制表示(无前导零)不存在 。
构造出最小的 后,若仍有 ,则不存在合法的 。
修改
注意到 在 时不变;而异或有性质 。就此,定义两种操作:
- 选择 ,使 。
- 同时将两个 加入 。
上述两个操作都不会影响 ,但可以使 增加。
定义 ,则问题转化为了依靠上述两种操作使 。
因为加法运算满足高位不会影响低位,考虑从最低位开始按位操作 。设 的最低位 为 ,不断重复 的操作,即可将 置 。
如何使 ?
时,若 ,则 即等同于 ; ,当且仅当 时可以将两个 加入 后再进行操作,否则无解。
,同时将两个 加入 即可完成操作。
时,得到答案。
限制无需担心,初始 长度为 ,后续增加的 ,其中 为一个 时所用的额外长度。而题目数据限制使其不可能超过 。
实现
使用了优先队列维护是否有 ,不过似乎没有必要。
AC
#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 工具辅助创作。