B. Jamie and Binary Sequence (changed after round)
time limit per test 2 seconds memory limit per test 256 megabytes
Jamie is preparing a Codeforces round. He has got an idea for a problem, but does not know how to solve it. Help him write a solution to the following problem:
Find k integers such that the sum of two to the power of each number equals to the number n and the largest integer in the answer is as small as possible. As there may be multiple answers, you are asked to output the lexicographically largest one.
To be more clear, consider all integer sequence with length k (a1, a2, ..., ak) with . Give a value
to each sequence. Among all sequence(s) that have the minimum y value, output the one that is the lexicographically largest.
For definitions of powers and lexicographical order see notes.
Input
The first line consists of two integers n and k (1 ≤ n ≤ 1e18, 1 ≤ k ≤ 1e5) — the required sum and the length of the sequence.
Output
Output "No" (without quotes) in a single line if there does not exist such sequence. Otherwise, output "Yes" (without quotes) in the first line, and k numbers separated by space in the second line — the required sequence.
It is guaranteed that the integers in the answer sequence fit the range [ - 1e18, 1e18].
Examples
input
23 5
output
Yes 3 3 2 1 0
input
13 2
output
No
input
1 2
output
Yes -1 -1
Note
Sample 1:
23 + 23 + 22 + 21 + 20 = 8 + 8 + 4 + 2 + 1 = 23
Answers like (3, 3, 2, 0, 1) or (0, 1, 2, 3, 3) are not lexicographically largest.
Answers like (4, 1, 1, 1, 0) do not have the minimum y value.
题意: 给你一个数 n,然后让你求k个数 a1, a2, a3, ... ak ,2^a1 + 2^a2 + ... + 2^ak =n
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <cstring>
using namespace std;
typedef long long ll;
int main()
{
map<ll,ll> a,sum;
ll n,k;
cin>>n>>k;
ll m=n,num=0;
int i;
for(i=0;m>0;i++)
{
a[i]=m%2;
m/=2;
if(a[i]==1)
{
num++;
sum[i]=1;
}
}
ll len=i-1;
if(k<num)
cout<<"No"<<endl;
else if(k==num)
{
cout<<"Yes"<<endl;
for(int i=len;i>=0;i--)
if(a[i]==1)
cout<<i<<" ";
}
else
{
cout<<"Yes"<<endl;
ll u=k-num;
for(int i=len;u>0;i--)
{
if(u>0)
{
while(sum[i]>0&&u>0)
{
sum[i]--;
sum[i-1]+=2;
u--;
}
}
}
int mx=-999;
int mi=999;
for(int i=100;i>=-1000;i--)
{
if(sum[i]!=0)
{
mx=max(i,mx);
mi=min(i,mi);
}
}
for(int i=-100;i<=100;i++)
{
if(i!=mx)
{
while(sum[i]>=2)
{
sum[i]-=2;
sum[i+1]++;
if(sum[mi]-1<0)
{
sum[i+1]--;
sum[i]+=2;
i++;
break;
}
sum[mi]--;
sum[mi-1]+=2;
mi=mi-1;
}
}
}
for(int i=100;i>=-100000;i--)
{
while(sum[i]>0)
{
cout<<i<<" ";
sum[i]--;
}
}
}
return 0;
}