博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二进制 + 模拟
阅读量:6693 次
发布时间:2019-06-25

本文共 4183 字,大约阅读时间需要 13 分钟。

B. Jamie and Binary Sequence (changed after round)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

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].

Examples
Input
Copy
23 5
Output
Yes 3 3 2 1 0
Input
Copy
13 2
Output
No
Input
Copy
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 分成 m 个2的幂次方的数,问是否存在这样的分法,如果存在,输出 yes , 并输出次幂,且保证其中的最大值最小,且字典序最大,首先我们的想法就是先采用最贪的取法,判断是否能够用 m 个数凑出来 n ,不能的话就是 no, 如果能为了让最大值最小,我们每次就将序列中最大数分成两份,知道分出 m 份,但是这样的分法只能确保出序列中的最大值最小,但是字典序却并不是最大的,因为 比如你输入 1 7 会得到 -2 -3 -3..全是 -3, 但是显然这样的字典序不是最大的,因此,我们最后再加一步对序列的合并,这里要怎么操作?首先序列如果只有一个值,我们是不用合并的,因为最初我们分序列是采用最贪的分法,所以除了最大值和第二大值,其他的值有且只会出现一次,我们合并就将第二大值合并,再将序列中最小的值一分为二即可。

代码示例 :

#define ll long longll n, k;ll fun(ll x){    ll res = 1;    ll base = 2;        while(x > 0) {        if (x & 1) res *= base;        base = base * base;        x >>= 1;     }        return res;}map
mp;int main() { cin >> n >> k; ll N = floor(log(n)/log(2)); //printf("%lld\n", len); ll sum = 0; ll cnt = 0; for(ll i = N; i >= 0; i--){ ll p = fun(i); if (sum + p < n) { sum += p; mp[i]++; cnt++; } else if (sum + p == n) {mp[i]++; cnt++; break;} } if (cnt > k) {printf("No\n"); return 0;} else printf("Yes\n"); map
::iterator it, ip; while (cnt < k) { it = --mp.end(); ll f = it->first; mp[f-1] += 2; mp[f]--; if (mp[f] == 0) mp.erase(f); cnt++; //printf("*** %lld %lld %lld\n", f, cnt, n); } it = mp.end(); it--; int maxx = it->first; it--; ip = mp.begin(); if (mp.size() != 1 && (it->second > 2 || (it->second == 2 && ip->first != it->first))) { while(it->second >= 2) { mp[maxx]++; mp[it->first] -= 2; ip = mp.begin(); mp[ip->first]--; mp[ip->first-1] += 2; if (mp[it->first] == 0) mp.erase(it->first); if (mp[ip->first] == 0) mp.erase(ip->first); } } it = --mp.end(); for(ip = it; ip != mp.begin(); ip--){ for(ll j = 1; j <= ip->second; j++) printf("%lld ", ip->first); //printf("---\n"); } it = mp.begin(); ll fff = ip->second; for(ll i = 1; i <= ip->second; i++){ printf("%lld%c", it->first, i==ip->second?'\n':' '); } return 0;}

 

转载于:https://www.cnblogs.com/ccut-ry/p/8467970.html

你可能感兴趣的文章
数据库读写分离
查看>>
社交是微信营销
查看>>
2008 R2 证书服务器应用详解
查看>>
hive 动态分区太多问题
查看>>
Windows Server 2008 RemoteApp(二)---部署激活远程桌面授权服务器
查看>>
读取日志文件开发总结
查看>>
IOS --React Native
查看>>
Linux CPU
查看>>
Linux/Centos ntp时间同步,联网情况和无网情况配置
查看>>
初级网络运维工程师比赛题目
查看>>
跨交换机实现vlan实验报告
查看>>
jquery easyui滚动条部分设置介绍
查看>>
cannot find -lxxx问题
查看>>
预防云端开源项目打包 Redis Labs再更改模块
查看>>
超惊人!去年发生的身分外泄安全事件是2017的4倍
查看>>
oracle sqlplus免安装的配置instantclient-basiclite
查看>>
Java开发GUI之选择列表
查看>>
一、分布式商城架构逻辑图
查看>>
机器人是如何完成避障的?机器人避障解决方案解读
查看>>
通过错误堆栈信息和源码分析错误来源
查看>>