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.
题意:构造出一个有k个数字的序列,k个数字乘积为n,要求构造出来的序列中最大值尽量小,同时字典序最大
解题思路:先算出最少需要多少数字,分别为2的几次方,然后把最大值尽量地减小,数量不够则把最小值拿出一个分成两个数字,再拿出一个新生成的数字,也分为两个,这样不断循环下去,直到个数够了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
LL n, cnt[1000009];
int k;
int main()
{
while (~scanf("%lld%d", &n, &k))
{
memset(cnt, 0, sizeof cnt);
int pos = 100019;
LL sum = 0;
while (n)
{
if (n & 1)
{
n--;
cnt[pos]++;
sum++;
}
else
{
n /= 2;
pos++;
}
}
if (k < sum) { printf("No\n"); continue; }
printf("Yes\n");
while (pos >= 1)
{
if (sum == k) break;
if (sum + cnt[pos] <= k)
{
cnt[pos - 1] += 2 * cnt[pos];
sum += cnt[pos];
cnt[pos] = 0;
pos--;
}
else break;
}
for (int i = 0;; i++)
{
if (sum == k) break;
if (!cnt[i]) continue;
int tmp = i, x = k - sum;
cnt[tmp]--;
for (int j = tmp - 1; j >= i - x + 1; j--) cnt[j] = 1;
cnt[i - x] = 2;
break;
}
int flag = 0;
for (int i = 110000; i >= 0; i--)
{
for (int j = 0; j < cnt[i]; j++)
{
if (!flag) printf("%d", i - 100019);
else printf(" %d", i - 100019);
flag = 1;
}
}
printf("\n");
}
return 0;
}