UPD on 2026/04/12
代码实际逻辑与分析部分构造方法不一致,已重构。望审核给过。
定义 highbit(x) 为正整数 x 二进制最高位 1 对应的数值 (别搜了,我对着 lowbit 硬造的)。
注意到若正整数 x,y 满足 highbit(x)=highbit(y),则 highbit(x) 在按位异或时会被异或掉,而在按位与时会保留,即:
xxory<highbit(x)≤xandy
于是我们可以得到当 n≤229 时的一组合法解为 229,229+1,229+2,⋯,229+n−1(显然以上数的 highbit 均为 229)。由于输入保证 n≤106,我们可以肯定不存在无解的情况。代码实现的复杂度为 O(T+∑n)。
AC 代码(请勿抄袭)
C++#include <iostream>
using namespace std;
int t, n;
int main(){
cin >> t;
while(t--){
cin >> n;
int x = (1 << 29);
while(n--)
cout << x++ << ' ';
cout << '\n';
}
return 0;
}