Powers Of Two 题解
考虑对 进行二进制拆分,最终可以表示成 ,。
当 或 时,显然无解。
当 时,输出即为这 个数,即 。
当 时,显然有 ,将每一个 的 变成两个 。即每次添加一个。
最终一定能变至 。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5e5 + 5;
ll n, k, cnt;
deque<ll> v;
int main()
{
scanf("%lld%lld", &n, &k);
ll rn = n, cnt = 0;
while (rn)
{
if (rn & 1)
{
v.push_front(cnt);
}
rn >>= 1;
cnt++;
}
if (v.size() > k || k > n)
{
printf("NO\n");
return 0;
}
printf("YES\n");
while (v.size() < k)
{
int u = v.front();
v.pop_front();
if (u == 1)
{
v.push_back(u - 1);
v.push_back(u - 1);
}
else
{
v.push_front(u - 1);
v.push_front(u - 1);
}
}
while (v.size())
{
int u = v.front();
printf("%lld ", 1ll << u);
v.pop_front();
}
printf("\n");
return 0;
}

浙公网安备 33010602011771号