Solution
我们可以想到对于 个数分开考虑,可以先在 时打个表,发现每个数可以构成的数好像有关联。
设 为选择的数, 为可以构成的数。
设对 的一次转移为 , 的一次逆转移为 或 。那么一个数的 就为 。
所以 与 进行一次转移后所形成的数都为 ,那么它们在继续转移后构成的数就都是相同的了,这很显然是不优的,因为在 中增加一个数,在 中也只增加了一个数。
显然,每个数在进行好多次转移最后都会变成 ,所以我们不妨倒着想,使得每个数在进行 次逆转移后不同,且 尽可能小。
这里可以对逆转移打个表找规律,但我们注意力惊人可以发现,逆转移不就是一个完全二叉树上的一个节点和转移到它的两个子节点吗?!又因为限制 ,提示我们这颗完全二叉树的深度为 (第一层深度为 )。
于是问题就转化成了,在深度为 的完全二叉树上选 条链(可以相交、重复),使得经过的节点最多。
bfs 贪心即可,这一部分就不作详细解释了。
Code
#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;
}