Envious Exponents

Posted rhythm-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 speci?c 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

 

 

以上是关于Envious Exponents的主要内容,如果未能解决你的问题,请参考以下文章

SPOJ:Bits. Exponents and Gcd(组合数+GCD)

you`remine啥意思

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?