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
  • 相关阅读:
    一个弹框表单复用create,edit表单操作
    数组去重,防抖节流,渲染几万条数据不卡页面,32个手撕JS,彻底摆脱初级前端
    解决vue表单回显数据无法修改的问题
    正则
    将UTC时间转换为当地的时间
    Vue页面上实时显示当前时间,每秒更新
    互联网协议
    记录:批量重命名文件夹下的文件
    IdentityServer4 无法登录,一直跳转登录界面
    同解与等价
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9535929.html
Copyright © 2011-2022 走看看