solution - AT_arc182_b

· · 题解

Solution

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

设 A 为选择的数,B 为可以构成的数。

设对 x 的一次转移为 \lfloor \frac{x}{2} \rfloor,x 的一次逆转移为 2x 或 2x+1。那么一个数的 B 就为 x , \lfloor \frac{x}{2} \rfloor , \lfloor \frac{x}{4} \rfloor , \dots , 0。

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

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

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

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

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;
}