A positive integer xx is called a power of two if it can be represented as x=2y, where y is a non-negative integer. So, the powers of two are 1,2,4,8,16,…

You are given two positive integers nn and k. Your task is to represent nn as the sumof exactly k powers of two.

Input

The only line of the input contains two integers nn and k (1≤n≤109, 1≤k≤2⋅105).

Output

If it is impossible to represent nn as the sum of k powers of two, print NO.

Otherwise, print YES, and then print kk positive integers b1,b2,…,bk such that each of bibi is a power of two, and ∑i=1kbi=n. If there are multiple answers, you may print any of them.

Examples

Input

9 4

Output

YES
1 2 2 4

Input

8 1

Output

YES
8

Input

5 1

Output

NO

Input

3 7

Output

NO
题目意思:给定一个数,让你用1,2,4,8等2的倍数表示出来,并且要用指定的k个数,输出一种即可。

解题思路:我们知道任意一个数都可以通过2的倍数组成,这其中的原因可以通过十进制转换二进制来说明。

如果该数的二进制对应数中1的个数大于k,那么很显然不会有k个2的倍数组成该数;如果恰好相等,那么只需要将二进制各个位上的1转换为对应的十进制即可;如果该数的二进制对应数中1的个数小于k,那么就需要拆分二进制了,这里我从高位来开始拆分,每当高位-1时,对应的下一位将会+2,这里存放二进制的数组里将不再仅仅存放0和1了,这时候存放的是对应二进制位数的个数了,只要拆分到恰够k个即可。同时要注意pow函数的使用,这个函数返回值是double类型的,注意转换。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int n,k,counts,i,j,x,y,a,ans;
int b[];
scanf("%d%d",&n,&k);
counts=;
x=;
a=n;
while(a)
{
if(a%)
{
counts++;
}
b[x]=a%;
x++;
a/=;
}
//printf("%d\n",counts);
//printf("%d\n",x);
if(k<counts||k>n)///不满足条件的情况
{
printf("NO\n");
}
else
{
printf("YES\n");
if(counts==k)///直接按照进制,不需要拆分
{
for(i=; i<x; i++)
{
if(b[i])
{
ans=int(pow(,i));
printf("%d ",ans);
}
}
}
else///即k>counts,从高位开始拆分,高位-1,相当于低位+2,counts+1
{
y=x-;///最高位
while(counts!=k)///终止条件
{
while(b[y]>=)
{
b[y]--;
b[y-]+=;
counts++;
if(counts==k)
{
break;
}
}
y--;
}
for(i=x-; i>=; i--)///输出
{
if(b[i]>=)///注意这里b[i]上的值代表的是该位上对应2^i的个数
{
for(j=; j<b[i]; j++)
{
ans=int(pow(,i));
printf("%d ",ans);
}
}
}
}
}
return ;
}

CF 1095C Powers Of Two(二进制拆分)的更多相关文章

  1. CF 1095C Powers Of Two

    题目连接:http://codeforces.com/problemset/problem/1095/C 题目: C. Powers Of Two time limit per test 4 seco ...

  2. HDU1059 二进制拆分优化多重背包

    /*问你能不能将给出的资源平分成两半,那么我们就以一半为背包,运行多重背包模版 但是注意了,由于个数过大,直接运行会超时,所以要用二进制拆分每种的个数*/ #include<stdio.h> ...

  3. BZOJ 2069: [POI2004]ZAW(Dijkstra + 二进制拆分)

    题意 给定一个有 \(N\) 个点 \(M\) 条边的无向图, 每条无向边 最多只能经过一次 . 对于边 \((u, v)\) , 从 \(u\) 到 \(v\) 的代价为 \(a\) , 从 \(v ...

  4. hdu 2844 coins(多重背包 二进制拆分法)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  5. HDU 4135:Co-prime(容斥+二进制拆分)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. 2018.09.08 bzoj1531: [POI2005]Bank notes(二进制拆分优化背包)

    传送门 显然不能直接写多重背包. 这题可以用二进制拆分/单调队列优化(感觉二进制好写). 所谓二进制优化,就是把1~c[i]拆分成20,21,...2t,c[i]−2t+1+1" role= ...

  7. 【最短路】【dijkstra】【二进制拆分】hdu6166 Senior Pan

    题意:给你一张带权有向图,问你某个点集中,两两结点之间的最短路的最小值是多少. 其实就是dijkstra,只不过往堆里塞边的时候,要注意塞进去它是从集合中的哪个起始点过来的,然后在更新某个点的答案的时 ...

  8. 【找规律】【二进制拆分】hdu6129 Just do it

    给你数列a,问你对它作m次求前缀异或和之后的新数列是什么. 考虑a1对最终生成的数列的每一位的贡献,仅仅考虑奇偶性, 当m为2的幂次的时候,恰好是这样的 2^0 1 1 1 1 1 ... 2^1 1 ...

  9. 【题解】Coins(二进制拆分+bitset)

    [题解]Coins(二进制拆分+bitset) [vj] 俗话说得好,bitset大法吼啊 这道题要不是他多组数据卡死了我复杂度算出来等于九千多万的选手我还不会想这种好办法233 考虑转移的实质是怎样 ...

随机推荐

  1. ZOJ 2476 Total Amount 字符串模拟

    - Total Amount Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit ...

  2. 高并发面试必问:分布式消息系统Kafka简介

    转载:https://blog.csdn.net/caisini_vc/article/details/48007297 Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成 ...

  3. 版本管理工具git与svn简介

    版本管理工具 版本管理工具简介 常见版本管理工具 cvs(Concurrent Versions System) vss(Visual SourceSafe) svn 常用的版本管理工具 git 流行 ...

  4. 函数的返回值是void

    #include <stdio.h> void sub(int x,int y,int z){ z=y-x; } void main() { int a=1,b=2,c=3; sub(10 ...

  5. C语言——常用标准输入输出函数 scanf(), printf(), gets(), puts(), getchar(), putchar(); 字符串拷贝函数 strcpy(), strncpy(), strchr(), strstr()函数用法特点

    1 首先介绍几个常用到的转义符 (1)     换行符“\n”, ASCII值为10: (2)     回车符“\r”, ASCII值为13: (3)     水平制表符“\t”, ASCII值为 9 ...

  6. 【commons】Bean工具类——commons-beanutils之BeanUtils

    一.起步 引入依赖: <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> < ...

  7. Java集合——TreeMap源码详解

    )TreeMap 是一个有序的key-value集合,它是通过红黑树实现的.因为红黑树是平衡的二叉搜索树,所以其put(包含update操作).get.remove的时间复杂度都为log(n). (2 ...

  8. PostgreSQL参数学习:deadlock_timeout

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@g ...

  9. 【转载】完成C++不能做到的事 - Visitor模式

    原文: 完成C++不能做到的事 - Visitor模式 拿着刚磨好的热咖啡,我坐在了显示器前.“美好的一天又开始了”,我想. 昨晚做完了一个非常困难的任务并送给美国同事Review,因此今天只需要根据 ...

  10. [并发并行]_[线程模型]_[Pthread线程使用模型之三 客户端/服务端模型(Client/Server]

    Pthread线程使用模型之三 客户端/服务端模型(Client/Server) 场景 1.在客户端/服务端模型时,客户端向服务端请求一些数据集的操作. 服务端执行执行操作独立的(多进程或跨网络)– ...