题解:CF1957B A BIT of a Construction
· 阅读需 2 分钟
原题链接
题意简述
构造长度为 的数列 (),满足 ,且让数列 按位或结果的二进制 的个数最多。
解题思路
把第 位变成 的代价为 ,显然越低位代价越小,所以要让低位尽可能变成 。
从低到高遍历第 位,如果 能变成 ,就让 加上 。
剩下 没用了,不妨全都放到 上,然后让其他 ()。
特别地,当 时, 只能等于 。
参考代码
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=200005;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
while(T--)
{
int n,k;
cin>>n>>k;
if(n==1)
{
cout<<k<<' ';
continue;
}
int a1=0;
for(int i=0;i<=30;i++)
{
int x=1<<i;
if(a1+x<=k)a1+=x;
}
cout<<a1<<' '<<k-a1<<' ';
for(int i=3;i<=n;i++)cout<<0<<' ';
cout<<'\n';
}
return 0;
}