zoukankan      html  css  js  c++  java
  • Envious Exponents

    问题 E: Envious Exponents

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 321  解决: 53
    [提交] [状态] [讨论版] [命题人:]

    题目描述

    Alice and Bob have an integer N. Alice and Bob are not happy with their integer. Last night they went to a cocktail party and found that another couple had the exact same integer! Because of that they are getting a new integer.

    Bob wants to impress the other couple and therefore he thinks their new integer should be strictly larger than N.
    Alice herself is actually fond of some specific integer k. Therefore, Alice thinks that whatever integer they pick, it should be possible to write it as a sum of k distinct powers of 2.

    Bob is also a cheapskate, therefore he wants to spend as little money as possible. Since the cost of an integer is proportional to its size, he wants to get an integer that is as small as possible.

    输入

    • A single line containing two integers N and k, with 1 ≤ N ≤ 1018 and 1 ≤ k ≤ 60.

    输出

    Output M, the smallest integer larger than N that can be written as the sum of exactly k distinct powers of 2.

    样例输入

    1 2
    

    样例输出

    3
    
    题意:找出大于n且二进制中恰好有k个1的最小整数。
    代码:
    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <map>
    #include <unordered_map>
    #include <set>
    #include <time.h>
    #define P(n,f) cout<<n<<(f?'
    ':' ')
    #define range(i,a,b) for(auto i=a;i<=b;++i)
    #define LL long long
    #define ULL unsigned long long
    #define elif else if
    #define itrange(i,a,b) for(auto i=a;i!=b;++i)
    #define rerange(i,a,b) for(auto i=a;i>=b;--i)
    #define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
    #define IOS ios::sync_with_stdio(false);cin.tie(0)
    using namespace std;
    LL n,k;
    LL bit[65],tail,cnt;
    void init(){
        fill(bit,0);
        cin>>n>>k;
    }
    void solve(){
        while(cin>>n>>k) {
            fill(bit,0);cnt=0;tail=0;
            while (n) {
                bit[++tail] = (n & 1);
                cnt += (n & 1);
                n >>= 1;
            }
            if (cnt >= k) {
                int tmp, pos = 0;
                for (tmp = 0; tmp < cnt and bit[tail - tmp]; ++tmp);
                while (cnt > k) {
                    if (!bit[++pos]) continue;
                    bit[pos] = 0;
                    --cnt;
                }
                if (tmp >= cnt) {
                    fill(bit, 0);
                    bit[++tail] = 1;
                    cnt = 1;
                } else {
                    range(i, pos+1, tail-1)
                        if(bit[i]) {
                            if (not bit[i + 1]) {
                                bit[i + 1] = 1;
                                bit[i] = 0;
                                break;
                            }
                            else{
                                bit[i]=0;
                                --cnt;
                            }
                        }
                }
            }
            int pos = 0;
            while (cnt < k)
                if (not bit[++pos]) {
                    bit[pos] = 1;
                    ++cnt;
                }
            LL ans = 0, add = 1;
            range(i, 1, 64) {
                ans += bit[i] * add;
                add <<= 1;
            }
            P(ans, 1);
        }
    }
    int main() {
        //init();
        solve();
        return 0;
    }
    View Code
  • 相关阅读:
    ADB 多设备中指定设备的启动
    vuex 绑定表单,多个输入框的解决办法
    vue 自动触发点击事件
    js 打印dom的所有属性
    邮件分享
    table表格 td设置固定宽度
    Echarts--Y坐标标题显示不全
    ExtJS5.0 菜鸟的第一天
    字体设置
    短信验证码+倒计时
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9535929.html
Copyright © 2011-2022 走看看