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
  • 相关阅读:
    基于live555的视频直播 DM368IPNC RTSP分析
    C语言中的二级指针(双指针)
    SQL中的IF ELSE(CASE语句的使用)
    Ubuntu下Postgres安装与配置
    虚拟机+ubuntu 图形界面和终端界面的切换
    中科院 2014年工程硕士入学专业课笔试考场安排
    中科院 2014年GCT考前辅导课程安排
    中科院 工程硕士专业课 复试考试前的辅导安排
    中国科学院大学工程管理与信息技术学院 2014年招收以下八个领域在职工程硕
    中国科学院大 工程管理与信息技术学院 在职工程硕士资格审查和复试重要通知
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9535929.html
Copyright © 2011-2022 走看看