• 二进制 + 模拟


    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 long
    
    ll 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<ll, ll>mp;
    
    int main() {
        cin >> n >> k;
        ll N = floor(log(n)/log(2));
        //printf("%lld
    ", 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
    "); return 0;}
        else printf("Yes
    "); 
        map<ll, ll>::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
    ", 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("---
    ");
        }
        it = mp.begin();
        ll fff = ip->second;
        for(ll i = 1; i <= ip->second; i++){
            printf("%lld%c", it->first, i==ip->second?'
    ':' '); 
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    技术的极限(8): 集成与分离
    心智与认知(1): 反馈循环(Feedback loop)
    证明与计算(6): 身份认证与授权
    证明与计算(5): 从加密哈希函数到一致性哈希
    技术的极限(6): 密码朋克精神(Cypherpunk Spirit)
    翻译(3): NULL-计算机科学上最糟糕的失误
    工具(5): 极简开发文档编写(How-to)
    证明与计算(3): 二分决策图(Binary Decision Diagram, BDD)
    证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)
    翻译(2): How to Write a 21st Century Proof
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8467970.html
Copyright © 2020-2023  润新知