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 ...
随机推荐
- zTree树形控件讲解
由于截图时间距离有些长,找不到原文出处,如有侵权请联系删除.
- applicationContext.getBean(“loginEntity”)
<!-- 指定Spring需要扫描的包,并将所有是别的类放到容器中,便于识别被注解的受托管bean --> <context:component-scan base-package= ...
- Sql Server 2012 事务复制遇到的问题及解决方式
1.订阅服务器提示:作业失败.无法确定所有者 WIN-01Q6JB46CHV\Administrator(拥有作业XXX)是否有服务器访问权限(原因:无法获取有关 Windows NT 组/用户'WI ...
- Cygwin, MinGW/MSYS, MinGW-W64/MSYS2
1. Cygwin http://www.cygwin.com/ Cygwin is a large collection of GNU and Open Source tools which pro ...
- 迅为嵌入式4418/6818开发板QT-HDMI显示
本文转自迅为论坛:http://www.topeetboard.com 平台:迅为4418/6818开发平台 1.首先请确认下光盘资料的日期(只有20171120及以后更新的光盘支持QT HDMI显示 ...
- AIX6.1平台11.2.0.3RAC 实施手册
1 前言 此文档详细描述了Oracle 11gR2 数据库在AIX6.1上的安装RAC的检查及安装步骤.文档中#表示root用户执行,$表示grid或oracle用户执行. 2 系统环境 操作系统环境 ...
- inux 软件编译、安装、删除
640?wx_fmt=otherimage.png 本文学习内容 手动安装软件 手动安装下载源码的软件 源码编译3步骤 deb包-包依赖管理 dekg -l 查看所以安装deb的包 apt-get仓库 ...
- CREATE INDEX - 定义一个新索引
SYNOPSIS CREATE [ UNIQUE ] INDEX name ON table [ USING method ] ( { column | ( expression ) } [ opcl ...
- Java集合(二)--Iterator和Iterable
Iterable: public interface Iterable<T> { Iterator<T> iterator(); } 上面是Iterable源码,只有一个ite ...
- B4. Concurrent JVM 锁机制(synchronized)
[概述] JVM 通过 synchronized 关键字提供锁,用于在线程同步中保证线程安全. [synchronized 实现原理] synchronized 可以用于代码块或者方法中,产生同步代码 ...