CF 1095C Powers Of Two
题目连接: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的更多相关文章
- 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 ...
- CF 702B Powers of Two(暴力)
题目链接: 传送门 Devu and Partitioning of the Array time limit per test:3 second memory limit per test: ...
- 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 ...
- CF 317D Game with Powers
题解: 将一个数的指数看着一个堆,问题变成这些堆的异或值 分析一个堆的情况,打SG表. #include<stdio.h> #include<string.h> ]; char ...
- 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 ...
- 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 ...
- 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 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
随机推荐
- 解决RecyclerView瀑布流效果结合Glide使用时图片变形的问题
问题描述:使用Glide加载RecyclerView的Item中的图片,RecyclerView使用了瀑布流展示图片,但是滚动时图片会不断的加载,并且大小位置都会改变,造成显示错乱. 解决方法:使用瀑 ...
- x++ 与 ++x的区别
相信在很多编程语言中都会遇见这个问题,这对于刚入编程的人来说可能是相当懵逼了. 老师的官方说法是:操作符在前面先进行自身运算,再进行其他运算:操作符在后面,先进行其他运算再进行自身运算. 反正我这段话 ...
- Nginx系列p2:重载,热部署,日志分割
今天我们来学习 nginx 的 重载.热部署.日志分割功能 重载:当我们需要修改配置文件中的一些值,我们可以直接修改该配置文件,然后重新启动 nginx 服务,就可以实现在 nginx 不停止服务的情 ...
- springboot 启动时加载数据库数据到本地Map
InitDataConfig.java import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Dict; impo ...
- Palette 的使用
Palette有什么用? Palette主要功能就是可以从图片中提取各种与颜色有关的元素.通过使用 Palette ,我们可以很轻松的实现界面风格的统一. Palette的使用很简单,首先你可以从gi ...
- 将list等分成n份
public static <T> Map<Integer, List<T>> spiltList(List<T> list, int num) { M ...
- 学生选课系统(Java语言期末前测试)
测试具体要求: 2.系统要求与功能设计 2.1 页面要求 (1)能够在Tomcat服务器中正确部署,并通过浏览器查看: (2)网站页面整体风格统一: (3)首页(登录页)要求实现不同用户登录后,进 ...
- 2020 年最流行的 Java 开发技术
不知不觉间,2020 年即将于十几天之后到来,作为技术圈中你,准备好迎接最新的变化了吗?在本文中,我们将以编程界最常用的编程语言 Java 为例,分享最为主流的技术与工具. 作者 | divyesh. ...
- 注册网站 captcha reCHAPTCHA 错误
原因 出现这个错误,是因为注册和提交时候,没有正确输出验证码导致的.网站可能会为了防止恶意注册,而使用验证码.如果验证码没有被正确加载或验证,就会出现相关错误. 解决方案 如果是访问类似kaggle, ...
- github 新建库,提交命令
Command line instructions You can also upload existing files from your computer using the instructio ...