题目连接:http://codeforces.com/problemset/problem/1095/C

题目:

C. Powers Of Two

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

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

Input

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

Output

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

Otherwise, print YES, and then print kk positive integers b1,b2,…,bkb1,b2,…,bk such that each of bibi is a power of two, and ∑i=1kbi=n∑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

题意:给出n,k,求出一个长度为k的2的幂的数列,使得

思路:看到对于2的指数形式,首先要想到其中一个方法——将该数字换算成2进制。

该题正好可以。接着从最高位一步一步拆(03101001 sum=6 → 02301001 sum=7 → 01501001 sum=8

代码中有更为详细的注解。

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,k;
int s[105];//储存二进制数,且储存方式为低位在前,高位在后;
int main()
{
scanf("%d %d",&n,&k);
int x=n,sum=0;
int t;//记录转换为二进制数的位数
for(t=0;x!=0;)
{
s[++t]=x&1;
x/=2;
if(s[t])
sum+=1;//记录二进制数中1的个数
}
/*开始写的时候在考虑 转换成二进制要进行拆分几次?如何判断?
下面的循环体 就是判断条件 sum<k时肯定不符合题意,从最高位
一次一次拆分,直到1的个数等于或大于k跳出循环体*/
while(sum<k)
{
if(t==1)
break;
/*从最高位开始拆分,最高位-1,次高位加2,
如果一直不能跳出循环,则换算成的二进制经过拆分,
又回到了初始情况n,此时位数t为1,跳出循环*/
s[t]-=1; s[t-1]+=2;
if(!s[t])
t-=1;
sum+=1;
}
if(sum!=k)
{
puts("NO");return 0;
}
else
{
puts("YES");
int T=1;
for(int i=1;i<t;i++,T*=2)
{
for(int j=1;j<=s[i];j++)
printf("%d ",T);
}
for(int i=1;i<s[t];i++)
printf("%d ",T);
printf("%d\n",T);
return 0;
}
}

CF 1095C Powers Of Two的更多相关文章

  1. CF 1095C Powers Of Two(二进制拆分)

    A positive integer xx is called a power of two if it can be represented as x=2y, where y is a non-ne ...

  2. CF 702B Powers of Two(暴力)

    题目链接: 传送门 Devu and Partitioning of the Array time limit per test:3 second     memory limit per test: ...

  3. CF 305C ——Ivan and Powers of Two——————【数学】

    Ivan and Powers of Two time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. CF 317D Game with Powers

    题解: 将一个数的指数看着一个堆,问题变成这些堆的异或值 分析一个堆的情况,打SG表. #include<stdio.h> #include<string.h> ]; char ...

  5. CF 622F The Sum of the k-th Powers——拉格朗日插值

    题目:http://codeforces.com/problemset/problem/622/F 发现 sigma(i=1~n) i 是一个二次的多项式( (1+n)*n/2 ),sigma(i=1 ...

  6. CF 622 F The Sum of the k-th Powers —— 拉格朗日插值

    题目:http://codeforces.com/contest/622/problem/F 设 f(x) = 1^k + 2^k + ... + n^k 则 f(x) - f(x-1) = x^k ...

  7. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  8. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  9. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

随机推荐

  1. Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)

    Description Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as h ...

  2. 洛谷 P2004 领地选择

    题目传送门 解题思路: 二维前缀和. AC代码: #include<iostream> #include<cstdio> using namespace std; ][],an ...

  3. tableau中图形分析相关设置

    1.柱形堆叠图单元格顶部显示总计值(可通过参考线实现) 2.调节图形单元格的宽窄度 (ctrl + 右键/左键) 3.折线图预测区间 趋势区间线 分析中预测并不是针对所有的日期格式均其作用,比如日期格 ...

  4. Nginx、MySQL、PHP 编译安装

    RHEL 7.0 编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14运行环境 准备篇: RHEL 7.0系统安装配置图解教程 http://www.jb51.net/os/192 ...

  5. [题解] CF438E The Child and Binary Tree

    CF438E The Child and Binary Tree Description 给一个大小为\(n\)的序列\(C\),保证\(C\)中每个元素各不相同,现在你要统计点权全在\(C\)中,且 ...

  6. 如何通过C语言获取主机WLAN下的IPv4地址,MAC地址

    #include "stdio.h" #include "windows.h" void GetHostWLAN_IPv4_AND_MAC(char IPv4[ ...

  7. mybatis的插入数据后的主键获取

    为什么要在插入数据后获取主键:当有一个订单表和订单详情表,当插入订单表的数据后,需要在订单详情表插入该订单的具体购物情况,订单详情表需要的一个列是订单表的主键或者订单ID.(通俗讲:A表的主键是B表的 ...

  8. Fiddler 自动响应

    使用目的:提前设置接口返回规则 便于前端联调 不用每次断点修改数据 操作:

  9. C语言之结构体概述

    C语言之结构体概述1.结构体类型是一种自定义类型(1)C语言中有2种类型:原生类型和自定义类型.2.结构体使用时先定义结构体类型再用类型定义变量(1)结构体定义时需要先定义结构体类型,再用类型来定义变 ...

  10. js 循环与判断语句的几个练习

    <script type="text/javascript"> /*1.X3 * 6528 = 3X * 8256 X为一个数字 填入一个数字 使等式成立*/ for ...