CodeForces - 916B(思维+位运算)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenquanwei_/article/details/79112338

链接:CodeForces - 916B

题意:

B. Jamie and Binary Sequence (changed after round)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 1018, 1 ≤ k ≤ 105) — 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 [ - 1018, 1018].

Examples
input
23 5
output
Yes
3 3 2 1 0 
input
13 2
output
No
input
1 2
output
Yes
-1 -1 

题解:

 先求出N的二进制,二进制的1所在的位即所求幂次,如果1的个数大于K,则肯定不行;如果小于K,则进行增加操作,可将一个2^n变成两个2^(n-1)。注意:由于要求n的最大值最小,且字典序最大,增加位的时候从最高位开始,整体降位,即将所有的n全部变成n-1。


#include <bits/stdc++.h>
using namespace std;

const int maxn = 64;
long long N, K;
int b[maxn*2];

int main()
{
    scanf("%I64d%I64d", &N, &K);

    for(int i = 0; i < maxn; i++){
        K -= (b[maxn + i] = N >> i & 1);
    }

    if(K < 0) return puts("No") & 0;

    for(int i = 2 * maxn - 1; i >= 1; i--){
        if(b[i] > K) break;
        K -= b[i];
        b[i - 1] += b[i] << 1;
        b[i] = 0;
    }
    int k = 0;
    while(!b[k]) k++; b[k]--;

    puts("Yes");
    for(int i = 2 * maxn - 1; i >= k; i--){
        while(b[i]--) printf("%d ", i - maxn);
    }
    for(int i = 1; i <= K; i++){
        printf("%d ", k - i - maxn);
    }
    printf("%d\n", k - K - maxn);

    return 0;
}
展开阅读全文