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.
The first line consists of two integers n and k (1 ≤ n ≤ 1018, 1 ≤ k ≤ 105) — the required sum and the length of the sequence.
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 [ - 1018, 1018].
23 5
Yes
3 3 2 1 0
13 2
No
1 2
Yes
-1 -1
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.
Sample 2:
It can be shown there does not exist a sequence with length 2.
Sample 3:
Powers of 2:
If x > 0, then 2x = 2·2·2·...·2 (x times).
If x = 0, then 2x = 1.
If x < 0, then .
Lexicographical order:
Given two different sequences of the same length, (a1, a2, ... , ak) and (b1, b2, ... , bk), the first one is smaller than the second one for the lexicographical order, if and only if ai < bi, for the first i where ai and bi differ.
題意:
就是把一個數分解成一些2的ki次方,(i=1,2,3....),可以重復,要求最大的ki要盡可能的小,但是要求字典序最大,不超過k個,輸出這些次方數
主要想明白字典序最小時是什么情況,就能解出來了。
先把n轉成二進制形式,統計其含1的個數sum,每個1代表一個2的ki次方。
如果sum>k,就直接輸出No,因為無法通過合並不同次的2的ki次方來得到一個2的ki次方
因為最大的次方一定要盡可能小,所以可以利用2^k=2*^(k-1),通過增加組成n的k的個數來減少ki的值,但是轉換的時候要注意,
如果sum加上最高為個數大於k,就不要轉化了,因為因為轉化后字典序虧損大於轉化最低位的。舉個例子
n=35,k=11
35
的二進制100011
sum=3;
32可以轉化為兩個16
依此類推
4 4 4 4 4 4 4 4 2 1
sum=10
如果繼續轉化
好好想想,在
4 4 4 4 4 4 4 4 2 1
的基礎上把ki轉化為2*k(i-1),不過以下幾種情況
4 4 4 4 4 4 4 3 3 2 1
4 4 4 4 4 4 4 4 1 1 1
4 4 4 4 4 4 4 4 2 -1 -1
可以發現轉化最低位的1可以最小的損失字典序,但是要注意到轉化后最低位就變了,故一位自能轉化一次
而在減少最大次方時,要么全轉化,要么全不轉化。
代碼:
#include<iostream>
#include<map>
#include<algorithm>
#include<string>
#include<cstdio>
#include<vector>
#include<functional>
#include<set>
#include<cstring>
using namespace std;
#define ll long long
#define kg " "
int main()
{
ll a;
ll b;
cin>>a>>b;
ll aa=a;
ll bb=b;
while(1){
if(aa>bb){
bb+=b;
}
else if(aa==bb){
cout<<aa<<endl;
return 0;
}
else{
aa+=a;
}
if(aa-bb==1){
cout<<aa<<endl;
return 0;
}
if(bb-aa==1){
cout<<bb<<endl;
return 0;
}
}
return 0;
}