Codeforces Round #529 (Div. 3) CPowers Of Two

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36294146/article/details/85345485
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#include<cctype>
using namespace std;

typedef long long ll;
const int Maxn=410000;

int n,k,a[Maxn],tot,cnt;

int main() {
    scanf("%d%d",&n,&k);
    if(n<k) {
        puts("NO");
        return 0;
    }
    int temp=1;
    while(n) {
        if(n&1) a[++tot]=temp;/*按位与,二进制相关,n转换为二进制与1进行按位与运算,最低位为1,n&1=1,最低位为0,n&1=0;*/
        temp<<=1,n>>=1;
    }
    if(tot>k) {
        puts("NO");
        return 0;
    }
    k-=tot;
    while(k) {
        int temp=a[tot];
        tot--;
        if(temp==1)
            cnt++;
        else {
            temp>>=1;
            a[++tot]=temp;
            a[++tot]=temp;
            k--;
        }
    }
    puts("YES");
    for(int i=1;i<=cnt;i++) printf("1 ");
    for(int i=1;i<=tot;i++) printf("%d ",a[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36294146/article/details/85345485