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(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
随机推荐
- 使用kali中的Metasploit通过windows7的永恒之蓝漏洞攻击并控制win7系统(9.27 第十三天)
1.开启postgresql数据库 2.msfconsole 进入MSF中 3.search 17-010 搜索cve17-010相关的exp auxiliary/scanner/smb/smb_ms ...
- UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)
题意:问k小时后,第A~B行一共有多少个红气球. 分析:观察图可发现,k小时后,图中最下面cur行的红气球个数满足下式: (1)当cur <= POW[k - 1]时, dfs(k, cur) ...
- 每天一点点之vue框架开发 - 引入bootstrap
只使用css样式 如果在你的项目中只是使用css样式,那就不需要安装,直接全局引入样式就好 <link rel="stylesheet" href="https ...
- EE将于5月30日在英国推出首个5G网络
英国移动运营商 EE 宣布计划于周三在英国推出 5G,该网络将于 5 月 30 日开通,第一批 5G 手机可从今天开始预订.EE 最初的 5G 网络部署将集中在六个城市(伦敦,加的夫,爱丁堡,贝尔法斯 ...
- tkinter组件详解之Label
tkinter组件详解之Label Label组件用于在屏幕上显示文本或图像.最红呈现出的结果是由背景和前景叠加构成的. 函数定义:Label(master=None, cnf={}, **kw) 背 ...
- 发送邮件的几种方法(C#发邮件 和 js前台实现都有)C#后台自动发邮件 js发邮件
1.后台自动发邮件 1)首先设置邮件参数,这里写在configuration里面 <appSettings> <add key="SMTP" value=&quo ...
- Swift轮播控件快速入门——FSPagerView
2018年03月01日 19:17:42 https://blog.csdn.net/sinat_21886795/article/details/79416068 今天介绍一个IOS的轮播控件FSP ...
- IDEA的一些常用设置
一.给方法之间添加分割线 效果: 二.自动导包 三.字体以及大小和行间距 四.注释的字体颜色 五.项目编码 六.省点模式(开启省点模式后会取消代码检查和提示等,需要注意) 七.代码垂直或者水平分区显示 ...
- C++ DirectShow读取摄像头后然后保存图像数据
#include <stdio.h> #include "camerads.h" #include <highgui.h> const char *g_sz ...
- django的model字段在保存的时候做预处理怎么办?
django的model字段在保存的时候做预处理怎么办? 比如这个model: class Book(Model): publish_date = DateField() 但是在保存时,用户输入数据是 ...