Codeforces Round #529 -C- Powers Of Two(二进制拆分)
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 two are 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 sumof 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
题意:给定一个数,让你用1,2,4,8等2的倍数表示出来,并且要用指定的k个数,输出一种即可。
思路:他需要的最小的位数为这个数本身转化成2进制数,其中1的数量为最小需要的二进制数,最多需要多少,是n个,因为都可以用1来表示,然后大一位的数代替即可,故可以表示的范围为2进制1的个数到n,这是可以表示出来的,可以从1进行累加到满足,也可以从高位拆分,高位拆分在一般情况下可能更快
下面是代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
long long int a[505];
int m=0;
int tochange(int x) {
while(x) {
a[m++]=x%2;
x/=2;
}
}//转成2进制
int main() {
int n,k;
scanf("%d%d",&n,&k);
int ans=0;
tochange(n);
for(int t=m-1; t>=0; t--) {
if(a[t]==1) {
ans++;
}
}
if(k<ans||k>n) {
cout<<"NO"<<endl;
}//不满足的情况
else {
cout<<"YES"<<endl;
if(ans==k) {//如果直接等于就不用拆分了
for(int t=0; t<m; t++) {
if(a[t]==1) {
long long int s1=pow(2,t);
printf("%lld ",s1);
}
}
} else {//从高位拆分,高位-1,低位+2,ans+1
int p=m-1;
while(ans!=k) {
while(a[p]>=1) {
a[p]--;
a[p-1]+=2;
ans++;
if(ans==k)
{
break;
}
}
p--;
}//输出
for(int t=m-1; t>=0; t--) {
if(a[t]>=1) {
for(int j=0; j<a[t]; j++) {
long long int s2=pow(2,t);
printf("%lld ",s2);
}
}
}
}
}
return 0;
}
Codeforces Round #529 -C- Powers Of Two(二进制拆分)的更多相关文章
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- Codeforces Round #529 (Div. 3) C. Powers Of Two (二进制)
题意:给你一个数\(n\),问是否能有\(k\)个\(2\)次方的数构成,若满足,输出一种合法的情况. 题解:从高到低枚举二进制的每一位,求出\(n\)的二进制的\(1\)的位置放进优先队列中,因为\ ...
- Codeforces Round #529 (Div. 3) C. Powers Of Two(数学????)
传送门 题意: 给出一个整数 n ,问能否将 n 分解成 k 个数之和,且这 k 个数必须是2的幂. 如果可以,输出"YES",并打印出任意一组解,反之输出"NO&quo ...
- Codeforces Round #529 (Div. 3) C. Powers Of Two
http://codeforces.com/contest/1095/problem/C 题意:给n找出k个2的幂,加起来正好等于n.例如 9,4:9 = 1 + 2 + 2 + 4 思路:首先任何数 ...
- CodeForces Round #529 Div.3
http://codeforces.com/contest/1095 A. Repeating Cipher #include <bits/stdc++.h> using namespac ...
- Codeforces Round #529 (Div. 3) 题解
生病康复中,心情很不好,下午回苏州. 刷了一套题散散心,Div 3,全部是 1 A,感觉比以前慢了好多好多啊. 这几天也整理了一下自己要做的事情,工作上要努力... ... 晚上还是要认认真真背英语的 ...
- Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心
A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...
- Codeforces Round #529 (Div. 3) F.Make It Connected
传送门 题意: 有 n 个顶点,每个顶点有个花费 a[ i ],连接顶点 u,v 需要花费 a[v]+a[u]的代价. 有 m 个特殊边,每条边有三个参数 u,v,w 代表的意思是连接 u,v 的花费 ...
随机推荐
- Hadoop- Hadoop环境搭建
Windows下Hadoop的安装 准备工具:64位的JDK,Hadoop安装包(我使用的是2.6.1) JDK下载地址 官网: http://www.oracle.com/technetwork/j ...
- 苹果AppStore如何申请加急审核
登录iTunesconnect,点击右上角的“?”图标,选择“联系我们”. iTunes Connect首页 依次选择“App Review”.“App Store Review” .” Reques ...
- BZOJ 1232 [Usaco2008Nov]安慰奶牛cheer:最小生成树【树上dfs性质】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1232 题意: 给你一个无向图,n个点,m条边. 每条边有边权len[i][j],每个点有点 ...
- RQNOJ 95 多多看DVD(加强版):01背包
题目链接:https://www.rqnoj.cn/problem/95 题意: 叔叔要陪多多看动画片. 有n张DVD可以买,第i张碟的打分为w[i],播放时间为t[i]. 爷爷规定他们只能在一定的时 ...
- 转载h5问题总结
判断微信浏览器 function isWeixin(){ var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMessenger ...
- Linux_服务器_04_vim编辑器的使用
二.参考文档 1.linux系统中如何进入退出vim编辑器,方法及区别
- WEB攻击之 CSRF 攻击及防御策略
介绍 是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方法. 释义: 跨站请求攻击,简单地说,是攻击者通过一些技术手段欺骗用户的浏览器去访问一个自己曾经认证过的网站并运行一些操作(如 ...
- bjwc Day2 玄学
早晨起来很开心,因为昨天跟妹子聊天聊到很晚 然后看到了题,感觉:这tm才是冬令营呀! T1构造,并没有找到性质,暴力都懒得打 T2数位dp,状态比较麻烦,看来跟dmy想到一起了,然后搞一下搞完 T3放 ...
- Mysql MMM 高可用
一.Mysql MMM 高可用概况: mmm_mond 负责所有的监控工作的监控守护进程,决定节点的移除等: mmm_agentd 运行在mysql服务器上的代理守护进程,通过简单远程服务集提供给 ...
- javascript 中的深复制 和 其实现方法
首先,我们需要明白什么是深复制(侧重指对象方面)? 在javascript中,复制分为浅复制和深复制,个人理解,浅复制就是直接将引用复制,复制前后的两个对象指向同一个内存地址,对其中一个进行操作,另外 ...