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?‘\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;
}

原文地址:https://www.cnblogs.com/Rhythm-/p/9535929.html

时间: 2024-11-06 22:36:38

Envious Exponents的相关文章

2018年第四阶段组队训练赛第三场(BAPC2017 Preliminaries)

D.Disastrous Doubling 题目描述 A scientist, E. Collie, is going to do some experiments with bacteria.Right now, she has one bacterium. She already knows that this species of bacteria doubles itself every hour. Hence, after one hour there will be 2 bacter

Python Networked programs (网络编程)

HyperText Transport Protocol - HTTPThe HyperText Transport Protocol is described in the following document: http://www.w3.org/Protocols/rfc2616/rfc2616.txt  This is a long and complex 176-page document with a lot of detail. If you find it interesting

[SinGuLaRiTy] COCI 2011~2012 #2

[SinGuLaRiTy-1007] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 测试题目 对于所有的题目:Time Limit:1s  |  Memory:256 MB 第一题:考试得分(score)[najboljih] [题目描述] 某单位举办了一次考试,考试有8道题,每道题的得分不一样.选手可以随便做,但最后只统计5道题的得分.现在给出选手每道题的得分,求出他最后的得分最大是多少? [输入] 8行,每行一个正整数X(0<=X<

spice model parameter

===================BSIM4.3.0 Model Selectors/Controllers============ LEVEL SPICE3 model selector VERSION Model version BINUNIT Binning unitr PARAMCHK Switch for parameter value check MOBMOD Mobility model RDSMOD Bias-dependent source/drain resistance

Python for Infomatics 第12章 网络编程一(译)

注:以下文章原文来自于Dr Charles Severance 的 <Python for Informatics> 本书中的许多例子关注的是从读取文件并查找数据,但在互联网中还有许多不同信息源. 本章我们将伪装成浏览器用超文本传送协议(HTTP)从网站获取网页,通读并分析它. 12.1 超文本传送协议-HTTP 支撑网页运转的网络协议实际非常简单,在Python中内置了套接字(socket)模块,使得使用套接字创建网络连接并获取数据变得非常容易. 套接字最很像文件,可以对它进行读写,但它还提

学好数学能让程序员的水平更高

I've been working for the past 15 months on repairing my rusty math skills, ever since I read a biography of Johnny von Neumann. I've read a huge stack of math books, and I have an even bigger stack of unread math books. And it's starting to come tog

同源基因查找软件OrthoMCL的使用

OrthoMCL (http://orthomcl.org/orthomcl/)主要用来找直系同源基因以及旁系同源基因.它主要在比较完整的基因组之间找直系同源基因.OrthoMCL的使用主要13步,可以参考doc/OrthoMCLEngine/Main/UserGuide.txt.为了方便运行OrthoMCL,可以建立一个工作目录"my_orthomcl_dir". 1>配置OrthoMCL程序 将orthomcl.config.template拷贝到你工作目录下(my_orth

【转帖】You and Your Research

Richard Hamming ``You and Your Research'' Transcription of the Bell Communications Research Colloquium Seminar 7 March 1986 J. F. Kaiser Bell Communications Research 445 South Street Morristown, NJ 07962-1910 [email protected] At a seminar in the Bel

python--运算符

+ plus 加号-----------------------------------• - minus 减号-------------------------------• / slash 整除----商-------------------------(乘除取余的优先级高于加减)• * asterisk 乘号----------------------------• % percent 取余----余数----------------- • < less-than 小于号• > grea