跳到正文

题解

solution - AT_arc182_b

littlebug约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年8月17日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 3 分钟
获赞
2

版本

  1. v1当前

Solution

我们可以想到对于 nn 个数分开考虑,可以先在 k=3k=3 时打个表,发现每个数可以构成的数好像有关联。

设 AA 为选择的数,BB 为可以构成的数。

设对 xx 的一次转移为 ⌊x2⌋\lfloor \frac{x}{2} \rfloor,xx 的一次逆转移为 2x2x 或 2x+12x+1。那么一个数的 BB 就为 x,⌊x2⌋,⌊x4⌋,…,0x , \lfloor \frac{x}{2} \rfloor , \lfloor \frac{x}{4} \rfloor , \dots , 0。

所以 2x2x 与 2x+12x+1 进行一次转移后所形成的数都为 xx,那么它们在继续转移后构成的数就都是相同的了,这很显然是不优的,因为在 AA 中增加一个数,在 BB 中也只增加了一个数。

显然,每个数在进行好多次转移最后都会变成 00,所以我们不妨倒着想,使得每个数在进行 kk 次逆转移后不同,且 kk 尽可能小。

这里可以对逆转移打个表找规律,但我们注意力惊人可以发现,逆转移不就是一个完全二叉树上的一个节点和转移到它的两个子节点吗?!又因为限制 Ai≤2K−1A_i \le 2^K - 1,提示我们这颗完全二叉树的深度为 K−1K-1(第一层深度为 00)。

于是问题就转化成了,在深度为 K−1K-1 的完全二叉树上选 nn 条链(可以相交、重复),使得经过的节点最多。

bfs 贪心即可,这一部分就不作详细解释了。

Code

C++
#include<iostream>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#define int long long
#define il inline
#define pb emplace_back
using namespace std;

const int MAXN=2e5+5;
int n,k;
int cnt=0;
vector <int> ans;
struct qwq{int v,dep;};
queue <qwq> q;

il void bfs()
{
	q.push((qwq){1,0});
	int cnt=1;
	qwq ck;
	while(!q.empty())
	{
		ck=q.front(); q.pop();
		if(ck.dep==k-1)
		{
			ans.pb(ck.v);
			continue;
		}
		q.push((qwq){ck.v<<1,ck.dep+1});
		if(cnt<n) q.push((qwq){ck.v<<1|1,ck.dep+1}),++cnt;
	}
	for(int i=cnt;i<n;++i) ans.pb((1<<k)-1);
}

il void solve()
{
	cin>>n>>k;
	vector<int>().swap(ans);
	queue<qwq>().swap(q);
	
	bfs();
	
	for(int i:ans) cout<<i<<' ';
	cout<<'\n';
}

signed main()
{
	ios::sync_with_stdio(0);
	ios_base::sync_with_stdio(0);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T;
	cin>>T;
	while(T--)
		solve();
	return 0;
}
原文