div2题目题解:I. Powers Of Two

从零开始使用CubeMX配置STM32使用lwip实现httpd服务器以及使用vscode编辑阅读keil代码(三) 使用stm32搭载lwip实现HTTPd服务器 阅读详情

题目:
I. Powers Of Two
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A positive integer x 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 n and k. Your task is to represent n as the sum of exactly k powers of two.

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

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

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

Examples
inputCopy
9 4
outputCopy
YES
1 2 2 4
inputCopy
8 1
outputCopy
YES
8
inputCopy
5 1
outputCopy
NO
inputCopy
3 7
outputCopy
NO

题意:
给n,k让你用k个二次幂数表示n;
我们知道9用二次幂数表示就是1 8,样例要用4个二次幂数来表示9就是1 2 2 4:
算是一个半水不水的手速题目:
首先要判断:最少要多少个二次幂数来表示n,如果k小于这个最小值,那就很尴尬了,所以要有特判,至于做法:
首先要用二进制表示n:
我们举个例子:19就是10011;(16+2+1)
这个10011放数组存着,然后就是记录1的个数,这个个数就是我们要用的表示出来n要用的最少的二次幂数的个数:

int i=n;
	int num=2;
	int time;
	int num_1=0;
	for(time=0;i>0;time++)
	{
		if(i%num){num_1++;all[time]=1;i-=(num/2);}
		else all[time]=0;
		num*=2;
	}
	int point=time;//二进制的位数
	//input finished;
	int goal;
	if(k<num_1||k>n)printf("NO\n");
	

代码如上,可以看到是这样获取的;
然后就是做法,得到了19变成10011这个数组,我们怎么做下一步?
假设:k为4;

10011如果变成02011会不会等于原来?显然相等,2个8和1个16是相等的,如果需要k个数,但是我只有num_1个1,如何表示呢,只能从高位到低位一直化,相当于什么?大面额的钱给他敞开了来花;上代码:
完整代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <queue>
#include <stack>
#include <map>
//鬼畜头文件
using namespace std;
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define LL long long
//鬼畜define
int n,k;
int all[40];
int main()
{
	scanf("%d %d",&n,&k);
	int i=n;
	int num=2;
	int time;
	int num_1=0;
	for(time=0;i>0;time++)
	{
		if(i%num){num_1++;all[time]=1;i-=(num/2);}
		else all[time]=0;
		num*=2;
	}
	int point=time;//二进制的位数
	//input finished;
	int goal;
	if(k<num_1||k>n)printf("NO\n");
	else
	{
		printf("YES\n");
		for(int time1=point-1;time1>=0;time1--)
		{
			int P=k-num_1;
			if(P==0){break;}
			if(all[time1]<=P)
			{
				num_1+=all[time1];
				all[time1-1]+=all[time1]*2;
				all[time1]=0;
				goal=time1;
			}
			else
			{
				num_1+=P;
				all[time1-1]+=P*2;
				all[time1]-=P;
				goal=time1;
			}
		}
		int num=1;
		for(int time=0;time<=goal;time++)
		{
			for(int time2=0;time2<all[time];time2++)
			{
				printf("%d ",num);
			}
			num*=2;
		}
		printf("\n");
	}
 
 
}
Python 爬虫实战:医学临床指南爬取(含 PDF 解析与结构化存储) 在当今数字化时代,医学临床指南作为医生进行科学诊疗的重要参考资料,包含大量经过临床验证的诊疗路径和建议。这些指南通常发布在权威医学网站上,数量庞大且更新频繁。如何自动化地抓取并存储这些指南,成为医疗数据分析、医学研究以及医疗 AI 模型训练等工作的关键第一步。本文将系统讲解如何利用 Python 爬虫技术从权威医学网站爬取临床指南内容,并实现 PDF 解析与结构化存储。 阅读详情

相关推荐

数学建模 种群竞争模型 学习笔记

模型背景 应用场景 种群竞争模型 例如 s1=0.5,则表示对于供养甲的资源来讲,单位数量的乙的消耗为单位数量的甲的0.5倍,即表示乙对于该资源的占有能力是甲的0.5倍(即竞争力弱) 仿真结果及分析 (1)设置自然增长率 r1=r2=1 (2)改变自然增长率 r1=r2=0.3 (3)修改甲和乙的最大容量 n1=10000, n2=100 (4)修改甲和乙的初始数量 x1=10, x...

Test_tju的博客 2028

(笔记)LeetCode231 Power of Two题解

/** * 判断n是否是2的幂次方 * * 一、 * 1 是 2的0次幂 * 2 是 2的1次幂 * 4 是 2的2次幂 * 8 是 2的3次幂 * 16是 2的4次幂 * * 二、 * 2/2 商1 余0 * 4/2 商2 余0 * 8/2 商3 余0 * 16/2 商4 余..

u011046452的博客 455

python爬取淘宝数据

python爬取淘宝数据淘宝评论 淘宝评论 打开淘宝,选择打开需要爬取的商品的评论。在谷歌浏览器选择开发者工具,选择network,找到list_detail_rate.htm?itemId=624466733121,点击headers、previews,可以看见相应的评论数据。 爬取一页屏评论代码如下 import re import requests headers = { 'Referer': '在headers中', 'User-Agent': '在

qq_36333776的博客 5797

2020 GDUT Winter Personal Training Contest I (Div. 2) I - Powers Of Two 题解

原题 题目大意 给定一个整数n,要求用k个2的幂次方表示 题目分析 啊啊啊啊啊啊啊啊没来得及交啊 想知道一个数怎么拆成二进制的话上网查查快速幂就行,这个不难的 到了这道题的话,因为要凑够k个数,那么简单的分离是不足够的,不过我们可以先来两个剪枝 当分离出来的数(也就是指数二进制中1)的个数比k还多的时候,那就不能再用k个数去表示n了(见in3) 当全部数被分成了1还不小于k个数的时候,也不能凑...

juseice的博客 193

BEVFormer与TensorRT的完美结合:如何实现4倍推理加速?

本文详细解析了如何通过TensorRT的量化技术和自定义插件实现BEVFormer模型的4倍推理加速,同时保持精度损失小于1%。文章从模型架构与部署挑战入手,深入探讨了计算图优化、自定义插件开发和量化校准策略,并提供了完整的部署流程和性能调优技巧,为自动驾驶领域的实时3D感知提供了高效解决方案。

bean的博客 221

QT快速配置MSVC2017编译环境(3分钟极简指南)

本文提供了一份3分钟极简指南,帮助开发者快速配置QT的MSVC2017编译环境。通过断网安装、精准勾选组件和配置QT Creator编译器路径等步骤,大幅提升QT开发效率。MSVC2017作为Windows平台高性能C++编译器,特别适合处理大型QT项目,编译速度优势明显。

weixin_30399821的博客 503

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

A positive integerxxis called apower of twoif it can be represented asx=2yx=2y, whereyyis a non-negative integer. So, thepowers of twoare1,2,4,8,16,…1,2,4,8,16,…. You are given two positive...

点点滴滴 388

手把手教你解决Keil5安装时xerces-c_3_0.dll缺失和0xc000007b报错(附文件下载与路径设置)

本文详细解析了Keil5安装过程中常见的xerces-c_3_0.dll缺失和0xc000007b报错问题,提供了从文件获取、路径设置到系统配置的完整解决方案。同时,文章还涵盖了STM32开发环境的搭建技巧和最佳实践,帮助开发者快速构建稳定的嵌入式开发环境。

weixin_29164497的博客 600

div2 c题专项训练

对于构造的反例的第一位,使用k个字母中在s中第一次出现位置下标最大的那个一定是最优的。这个字母加入到反例中,将s的相应前缀删除,重复上述过程。如果最后构造出的反例长度大于等于n,那么输出YES;否则输出NO,一个没有出现的子串可以构造为当前构造出的反例加上若干个在剩下的s中没有出现的字母,将反例长度扩充为n输出即可。c题普遍不考察高深算法,而考察对问题的分析能力和简单的数学推导。发现k个字母能form的长度为n的串数量是非常多的,如果去检验每一个串是否在s中是否出现复杂度过大且代码不好实现。

2301_82345937的博客 491

LeetCode --- 937. Reorder Data in Log Files 解题报告

You are given an array oflogs. Each log is a space-delimited string of words, where the first word is theidentifier. There are two types of logs: Letter-logs: All words (except the identifier) consist of lowercase English letters. Digit-logs: All word..

杨鑫newlife的专栏 426

基于STM32L4XX 、HAL库的TMC2209-LA-T步进电机驱动器驱动程序应用设计

/ R/W=0 表示写。uint32_t gconf_value = (0 << 0) // I_scale_analog: 禁用,使用UART控制电流。uint32_t ihold_irun = (8 << 0) // 保持电流 8/31 最大值。

PP_Cottage的博客 449

Codeforces#640div4解题报告

Codeforces #640div4 A 原题 A positive (strictly greater than zero) integer is called round if it is of the form d00…0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all

Jeremy写字的地方 460

Qt应用软件【协议篇】自定义协议处理串口半包贴包

Qt串口数据,半包,贴包,丢包数据处理

小鱼酱的博客 1211

Codeforces Round #529 (Div. 3) C. Powers Of Two题解及思路

         time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Ivan wants to play a game with you. He picked some string ss of length nn consis...

qq_42426141的博客 291

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Codeforces Round #486 (Div. 3) D. Points and Powers of Two

D. Points and Powers of Twotime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn distinct points on a coordinate line, the coordinate of ii...

qq_36603180的博客 264

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Codeforces Round #529 (Div. 3) 题解

刷了一套题散散心,Div 3,全部是 1 A,感觉比以前慢了好多好多啊。 这几天也整理了一下自己要做的事情,工作上要努力... ... 晚上还是要认认真真背英语的。 找了几个同学问了一下,有点想自己做点 project 了,再学学机器学习,深度学习之类的,弄点小 AI project 玩玩吧... ...没事看点各种科技新闻开开眼界。 【题目链接】 A -Repeating ...

weixin_33720078的博客 185

Codeforces Round #529 (Div. 3) C. Powers Of Two(数学????)

传送门 题意:   给出一个整数 n ,问能否将 n 分解成 k 个数之和,且这 k 个数必须是2的幂。   如果可以,输出"YES",并打印出任意一组解,反之输出"NO"; 题解:   预备知识补充:   如何求出 num 最少需要多少个2的幂之和?   例如 :     num = 3 = 20+21至少需要两个     num = 4 = 22 至少需要一个    ...

weixin_30454481的博客 181

Codeforces Round #486 (Div. 3) D. Points and Powers of Two【贪心】

D. Points and Powers of Twotime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn distinct points on a coordinate line, the coordinate of ii...

哇-WA 的博客 265

Codeforces Round #486(Div. 2) D. Points and Powers of Two

传送门:点击打开链接题意:找最长的序列,使得该序列的任意两个值的差是2的倍数. 输出长度,并输出元素。分析:可以证明最长长度为三。对stl不熟啊,代码写的搓。学习了一下大佬的代码。我的代码:#include &lt;bits/stdc++.h&gt; using namespace std; int main() { #ifdef _DEBUG #endif int n; cin &gt;...

常胜将军的博客 207

Educational Codeforces Round 83 (Rated for Div. 2) A - C

A.Two Regular Polygons 题意:在一个凸的正多边形中,选择任意多顶点,能否再构造一个凸正多边形。 思路:水题,只要构造出来的正多边形的边数是原正多边形边数的因数即可。 Code: #include<bits/stdc++.h> using namespace std; int main(int argc,char *argv[]){ int t; i...

QiPianZuo的博客 1778

Codeforces Round #529 (Div. 3) ABCD题解

A. Repeating Cipher 简单思维题 题目大意:给你一个经过加密的字符串S,S = S1S2S3.....;而Si意味着这个字母他会书写i次;列如baabbb,这是加密后的样子加密前就是bab;题目需要你输出加密前的字符串; 思路:用一个中间变量t充当i,每解密一个字母后,就t++;然后我循环输出的s[i+=t],每次就输出加密后Si的最后一个字母。就是需要的答案了。 #i...

qq_42129242的博客 358

Codeforces 988D 题解

&lt;988D&gt;题意:给定若干个数,问在其中挑选几个数组成一个集合,使得集合中任意以两个数a, b(a &gt; b)的差值均为2^n。思路:这题要先找规律,这个规律就是,集合中的元素最多有3个,证明如下:假设集合内目前只有三个元素:x,y,z 且 x &lt; y &lt; z则根据题意,有如下关系:z - y = 2^a (1)y - x = 2^b (2)z - x = 2^c (3...

FrozenAllen的博客 577

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...

black_hole6的博客 898

Codeforces 1095C. Powers Of Two——————multiset

C. Powers Of Two A positive integer x is called a power of two if it can be represented as x=2yx=2^yx=2y, where y is a non-negative integer. So, the powers of two are 1,2,4,8,16,…. You are given two p...

鸿杰 的博客 1166

div2题目题解:J. Circular Dance

题目: J. Circular Dance time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output There are n kids, numbered from 1 to n, dancing in a circle around the C...

DevourPower的博客 505

Codeforces529 -C Round #- Powers Of Two(二进制拆分)

A positive integerxxis called apower of twoif it can be represented asx=2^y , whereyyis a non-negative integer. So, thepowers of twoare1,2,4,8,16,…1,2,4,8,16,…. You are given two positive i...

qq_40293424的博客 259

「CF#554 div2」题解

A 水题一道。 题目的大致意思就是:给你两个集合,求集合间有多少数对和是奇数。 题解,开444个桶后,求一个minminmin就可以了。 #include <bits/stdc++.h> using namespace std; int n, m; int v1[4], v2[4]; int main() { scanf("%d%d", &n, &m); for (...

chhokmah的博客 362
上一篇: div2题目题解:J. Circular Dance
下一篇: GDUT_寒假训练题解报告_数论专题_个人题解报告——题目:D - Beautiful Numbers (CodeForces - 300C)【逆元、组合数公式】
DevourPower
博客等级 码龄7年 52粉丝 78原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值