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 .
分析:题目有点难懂(-_-#),先把n转换成二进制,如果总长度lenth大于k则不能构成。
因为2x=2x-1+2x-1,即每一位的一个可以转换为低一位的两个,题目要求最高位最小,
那么如果最高位所有的个数转换为下一位后的长度仍<=k,就把最高位全部转为下一位;
否则就不能转换最高位,因为题目要求字典序最大,所以只能转换最低位,每次把最低位
的一个转换为下一位的两个,直到总长度lenth==k。(-_-; )
#include#include using namespace std;int a[2000000],num[2000000]; const int M=100000;int main(){ long long N,temp; int k; scanf("%lld%d",&N,&k); //M表示0 int len=0,lenth=0; temp=N; while(temp) { a[len+M]=temp%2;len++; if(temp%2) lenth++; temp/=2; } if(lenth>k) {printf("No\n");return 0;} int Max=len+M-1,Min;//最大值下标 for(int i=M;i<=Max;i++) if(a[i]>0) {Min=i;break;} while(true) { if(lenth==k) break; if(a[Max]+lenth>k)//不能再转换 break; a[Max-1]+=a[Max]*2; lenth+=a[Max]; a[Max]=0; Max--; if(Min>Max) Min=Max; } if(lenth =Min;i--) { while(a[i]) { printf("%d ",i-M); a[i]--; } } printf("\n"); return 0;}