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

相关文章

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)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

poj 3190 优先队列+贪心

题意: 有n头牛,分别给他们挤奶的时间。 然后每头牛挤奶的时候都要在一个stall里面,并且每个stall每次只能占用一头牛。 问最少需要多少个stall,并输出每头牛所在的stall。 e.g 样例: INPUT: 51 102 43 65 84 7 OUTPUT: 412324 HINT: Explanation of the s

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m