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?'\n':' ')
#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
相關文章
相關標籤/搜索