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 ...
随机推荐
- 机器学习-Probabilistic interpretation
Probabilistic interpretation,概率解释 解释为何线性回归的损失函数会选择最小二乘 表示误差,表示unmodeled因素或随机噪声,真实的y和预测出来的值之间是会有误差的, ...
- js 和 java 将时间倒计时显示
需求:用户购买产品,创建了一个订单,但是还么有支付,此时给用户三十分钟剩余支付时间,前台页面做倒计时功能,时间到达时,更改订单状态 ,输出的格式是:29:23 java 实现 package com. ...
- linux下php访问sql server设置
安装freeIDS 官网下载地址: wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.18.tar.gz 1.1.到下载目录解压 t ...
- swift 使用计算属性+结构管理内存
class GooClass { deinit { print("aaaaaaaa") } var str = "gooClass" } struct GooS ...
- php+nginx 限制上传文件大小
问题:在后台上传8M大小的图片,上传不成功 nginx返回413,如下图所示: 分析:nginx配置文件或者php中,可上传的大小设置太小了 解决办法------检查nginx和php的配置文件里面的 ...
- Zend Studio 服务器根目录设置
在 Apache 服务器根目录里查找 \conf\httpd.conf 例如:C:\AppServ\Apache24\conf\httpd.conf 打开后查找 DocumentRoot 标记 修改调 ...
- vue面试相关
(1)什么是mvvm? MVVM是Model-View-ViewModel的缩写.mvvm是一种设计思想.Model 层代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑:View ...
- webstorm 设置ES6语法支持以及添加vuejs开发配置
参考文章:https://blog.csdn.net/diligentkong/article/details/75040651
- 0.ssm web项目中的遇到的坑
1.自定义的菜单,href为项目的相对路径,即: : 点击一个菜单,后再点击另一个菜单,然后发现浏览器地址栏的链接是在上一个链接后面拼接的,也就报错了. 解决办法: 每一个菜单的href前增加&quo ...
- python compare with other language
java http://dirtsimple.org/2004/12/python-is-not-java.htmlhttp://twistedmatrix.com/users/glyph/rant/ ...