Codeforces Round #457 (Div. 2)——B. Jamie and Binary Sequence(贪心)

比赛刚a了这题就说标程错了ORZ,我还是太菜惹(⋟﹏⋞),现在应该是改好了吧???如有错误欢迎指正


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 
Note

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.



思路:先拆成二进制,要满足最高位最小,即最大的能拆完就拆完,否则从最小的数一个个开始拆,详见代码。


代码:

#include<stdio.h>

int ans[100005],v[100005];

int main(){
    long long n,k;
    scanf("%lld%lld",&n,&k);
    int i,index=0,cnt=0;//cnt记录已经拆了多少个
    //先拆成2进制
    while(n){
        if(n&1){
            v[index]=1;
            cnt++;
        }
        index++;
        n/=2;
    }
    
    //如果cnt已经大于k,cnt当前记录的是最少要拆几个
    if(cnt>k){printf("No\n");return 0;}
    printf("Yes\n");
    
    
    int minn;
    //二进制顺序反转,顺便找到最小的二进制为1的数minn,最大的不够拆了从minn开始拆
    for(i=0;i<index;i++){
        ans[i]=v[index-i-1];
        if(ans[i])minn=i;
    }    

    //从头开始拆
    for(i=0;;i++){
        if(cnt==k)break;
        if(k-cnt>=ans[i]){
            cnt+=ans[i];
            ans[i+1]+=ans[i]*2;
            ans[i]=0;
        }
        else break;
        if(i+1>minn)minn=i+1;   //更新最小值下标
    }
    
    //从最小值开始拆
    for(;;i++){
        if(cnt==k)break;
        cnt++;
        ans[minn]--;
        ans[minn+1]+=2;
        minn++;
    }
    
    //找到输出下标上限
    int a=index>i?index-1:i;
    a=minn>a?minn:a;
    
    //输出,末尾可以留空格
    for(i=0;i<=a;i++){
        while(ans[i]){
            ans[i]--;
            minn=index-i-1;
            printf("%d ",minn);
        }
    }
    printf("\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值