Codeforces Round #457 (Div. 2) B
2 seconds
256 megabytes
standard input
standard output
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.
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 "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].
23 5
Yes
3 3 2 1 0
13 2
No
1 2
Yes
-1 -1
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项 2^a 次幂相加的形式 n=2^a1+2^a2+...+2^ak 那么我们可以得到一个序列a,a中可以有相等的项。求最大值a(max)最小,同时,字典序最大的序列a
解析 用二进制来做,首先要满足最大值最小 如果把当前最大值全部分解为最下一项 时 项数小于等于k 那么就把它分解掉 直到不满足这一条件,因为最大值部分分解掉 a(max)并没有变小
这时候我们就要满足字典序最大,开始分解最小的那一项。
AC代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = 1e5+;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
int ans[maxn];
int main()
{
map<int,int> ma;
ll n,k;
cin>>n>>k;
priority_queue<int,vector<int>,greater<int> > q;
int cnt=,sum=;
while(n!=)
{
int s=n&;
if(s==)
ma[cnt]++,sum++;
cnt++;
n=n>>;
}
if(k<sum)
printf("No\n");
else
{
printf("Yes\n");
int i;
for(i=;k!=sum;i--)
{
if(ma[i]<=k-sum)
{
ma[i-]+=ma[i]*;
sum+=ma[i];
ma[i]=;
}
else
{
break;
}
}
for(int j=-;j<=;j++)
{
while(ma[j]--)
{
q.push(j);
}
}
if(sum!=k)
{
while(sum!=k)
{
int temp=q.top();
q.pop();
q.push(temp-),q.push(temp-);
sum++;
}
}
int cnt=;
while(!q.empty())
{
ans[cnt++]=q.top();
q.pop();
}
for(int i=cnt-;i>=;i--)
cout<<ans[i]<<" ";
cout<<ans[]<<endl;
}
}
Codeforces Round #457 (Div. 2) B的更多相关文章
- 【Codeforces Round #457 (Div. 2) C】Jamie and Interesting Graph
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找比n-1大的最小的素数x 1-2,2-3..(n-2)-(n-1)长度都为1 然后(n-1)-n长度为(x-(n-2)) 然后其他 ...
- 【Codeforces Round #457 (Div. 2) B】Jamie and Binary Sequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把n分解成二进制的形式. n=2^a0+2^a1+...+2^a[q-1] 则固定就是长度为q的序列. 要想扩展为长为k的序列. 可 ...
- 【Codeforces Round #457 (Div. 2) A】 Jamie and Alarm Snooze
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力往前走x分钟就好. 直到出现7为止. [代码] #include <bits/stdc++.h> using nam ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- Anaconda(miniconda)安装及使用--转
https://www.waitalone.cn/anaconda-install-error.html 3,224 1.Anaconda概述 Anaconda是一个用于科学计算的 ...
- php(三)使用PDO链接数据库
1.启动 mysql数据库,打开图形化控制界面 2.新建一个数据库 3.创建一个数据表 4.给数据表添加数据 id是数字类型的 类型选择int长度 11 username 等其他数据 会是字符串形 ...
- R in action读书笔记(15)第十一章 中级绘图 之二 折线图 相关图 马赛克图
第十一章 中级绘图 本节用到的函数有: plot legend corrgram mosaic 11.2折线图 如果将散点图上的点从左往右连接起来,那么就会得到一个折线图. 创建散点图和折线图: &g ...
- Node.js——Buffer
介绍 JavaScript没有读取和操作二进制数据流的机制,但是 node.js 引入了Buffer 类型,可以操作TCP流或者文件流 使用Buffer可以用来对临时数据(二进制数据)进行存储,当我们 ...
- PHP运算符考察点
PHP运算符优先级 运算符优先级指定了两个表达式绑定得有多"紧密".例如,表达式 1 + 5 * 3 的结果是 16 而不是 18 是因为乘号(*)的优先级比加号(+)高.必要时可 ...
- System.Web.Optimization找不到引用怎么办
新建Bootstap for MVC5出现的问题, 通过打开VS 工具->NUGET程序包管理器->控制台 输入以下命令进行完成,一切完成 Install-Package Microsof ...
- 物联网初学者智能家居必备迅为iTOP-4412开发板
更情点击了解:http://www.topeetboard.com 1. 手把手全视频教程: 第一部分:迅为电子开发板入门视频 第二部分:Linux系统编程 第三部分:Itop-4412开发板硬件设 ...
- (转)淘淘商城系列——SSM框架整合之Dao层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...
- pdf 使用模板下载
//根据模板下载模板 /** * * 政策5-8条的创建的pdf的模板 */public String createPdfCashTemplate(PdfCashParam pdfCashParam) ...
- android studio 调试安装
最新的手机无法debug安装调试. vivo x21 oppo r11等 解决办法: 1,关闭instant run Android Studio->Preferences->Build ...