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

本文介绍了一道Codeforces竞赛题目B.Jamie and Binary Sequence的解决思路及实现代码。任务要求找到k个整数,使得这些整数的2的幂次之和等于给定的数n,并且最大的整数尽可能小,同时要求输出的序列字典序最大。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值