Codeforces Round #457 (Div. 2) B. Jamie and Binary Sequence(二进制,思路,贪心)

2023-10-05 23:30

本文主要是介绍Codeforces Round #457 (Div. 2) B. Jamie and Binary Sequence(二进制,思路,贪心),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

描述

Jamie is preparing a Codeforces round. He has got an idea for a
problem, but does not know how to solve it. Help him write a solution
to the following problem:

Find k integers such that the sum of two to the power of each number
equals to the number n and the largest integer in the answer is as
small as possible. As there may be multiple answers, you are asked to
output the lexicographically largest one.

To be more clear, consider all integer sequence with length k
(a1, a2, …, ak) with . Give a value to each sequence. Among all
sequence(s) that have the minimum y value, output the one that is the
lexicographically largest.

For definitions of powers and lexicographical order see notes.

Input

The first line consists of two integers n and k
(1 ≤ n ≤ 1018, 1 ≤ k ≤ 105) — the required sum and the length of the
sequence.

Output

Output “No” (without quotes) in a single line if there does not exist
such sequence. Otherwise, output “Yes” (without quotes) in the first
line, and k numbers separated by space in the second line — the
required sequence.

It is guaranteed that the integers in the answer sequence fit the
range [ - 1018, 1018].

input

23 5

output

Yes
3 3 2 1 0 

input

13 2

output

No

input

1 2

output

Yes
-1 -1 

Note

Sample 1:

23 + 23 + 22 + 21 + 20 = 8 + 8 + 4 + 2 + 1 = 23

Answers like (3, 3, 2, 0, 1) or (0, 1, 2, 3, 3) are not
lexicographically largest.

Answers like (4, 1, 1, 1, 0) do not have the minimum y value.

Sample 2:

It can be shown there does not exist a sequence with length 2.

Sample 3:

Powers of 2:

If x > 0, then 2x = 2·2·2·…·2 (x times).

If x = 0, then 2x = 1.

If x < 0, then .

Lexicographical order:

Given two different sequences of the same length, (a1, a2, … , ak)
and (b1, b2, … , bk), the first one is smaller than the second one
for the lexicographical order, if and only if ai < bi, for the first i
where ai and bi differ.

思路

首先说题意,题目给了两个数 n k,把 n 拆成k个2的次幂相加的形式,并且把次幂输出

要求:

  1. 满足k个2的次幂相加
  2. 最高位次幂要最小
  3. 按照字典序排列

拆的方法是:

2n+2n=22n=2n+1

假设现在有一组样例:1 7

那么首先,1的二进制形式是:1

  1. 把1拆成两个-1
  2. 把两个-1拆成4个-2
  3. 当把4个-2拆成8个-2时发现,要求只有7个,那么我们就不拆,把4个-2中最后面的-2拆成两个-3,把两个-3后面的-3拆成两个-4,两个-4后面的-4拆成两个-5,到此现在有7个了,停止,答案输出为:-2 -2 -2 -3 -4 -5 -5

代码

#include<bits/stdc++.h>
using namespace std;
long long n,k;
int a[100200],*cnt=a+100000;
int main()
{cin>>n>>k;for (int i=0; i<60; i++){cnt[i]+=n>>i&1;k-=cnt[i];}//用k统计一下还剩下多少位if (k<0){puts("No");return 0;}for (int i=59; i>=-60; i--)if (k>=cnt[i]){k-=cnt[i];cnt[i-1]+=2*cnt[i];//2*2^n=2^(n+1)cnt[i]=0;}else{int Min=-60;while (!cnt[Min])//找到最右边最小的一位Min++;while (k--){cnt[Min]--;cnt[--Min]+=2;}break;}puts("Yes");for (int i=59; i>=-100000; i--)while (cnt[i]--) printf("%d ",i);puts("");return 0;
}

这篇关于Codeforces Round #457 (Div. 2) B. Jamie and Binary Sequence(二进制,思路,贪心)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/152283

相关文章

C语言中的常见进制转换详解(从二进制到十六进制)

《C语言中的常见进制转换详解(从二进制到十六进制)》进制转换是计算机编程中的一个常见任务,特别是在处理低级别的数据操作时,C语言作为一门底层编程语言,在进制转换方面提供了灵活的操作方式,今天,我们将深... 目录1、进制基础2、C语言中的进制转换2.1 从十进制转换为其他进制十进制转二进制十进制转八进制十进

PostgreSQL 序列(Sequence) 与 Oracle 序列对比差异分析

《PostgreSQL序列(Sequence)与Oracle序列对比差异分析》PostgreSQL和Oracle都提供了序列(Sequence)功能,但在实现细节和使用方式上存在一些重要差异,... 目录PostgreSQL 序列(Sequence) 与 oracle 序列对比一 基本语法对比1.1 创建序

Python MCPInspector调试思路详解

《PythonMCPInspector调试思路详解》:本文主要介绍PythonMCPInspector调试思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录python-MCPInspector调试1-核心知识点2-思路整理1-核心思路2-核心代码3-参考网址

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

如何将二进制文件流转化为MockMultipartFile文件

《如何将二进制文件流转化为MockMultipartFile文件》文章主要介绍了如何使用Spring框架中的MockMultipartFile类来模拟文件上传,并处理上传逻辑,包括获取二进制文件流、创... 目录一、名词解释及业务解释1.具体业务流程2.转换对象解释1. MockMultipartFile2

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验