问题 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[],tail,cnt;
void init(){
fill(bit,);
cin>>n>>k;
}
void solve(){
while(cin>>n>>k) {
fill(bit,);cnt=;tail=;
while (n) {
bit[++tail] = (n & );
cnt += (n & );
n >>= ;
}
if (cnt >= k) {
int tmp, pos = ;
for (tmp = ; tmp < cnt and bit[tail - tmp]; ++tmp);
while (cnt > k) {
if (!bit[++pos]) continue;
bit[pos] = ;
--cnt;
}
if (tmp >= cnt) {
fill(bit, );
bit[++tail] = ;
cnt = ;
} else {
range(i, pos+, tail-)
if(bit[i]) {
if (not bit[i + ]) {
bit[i + ] = ;
bit[i] = ;
break;
}
else{
bit[i]=;
--cnt;
}
}
}
}
int pos = ;
while (cnt < k)
if (not bit[++pos]) {
bit[pos] = ;
++cnt;
}
LL ans = , add = ;
range(i, , ) {
ans += bit[i] * add;
add <<= ;
}
P(ans, );
}
}
int main() {
//init();
solve();
return ;
}

Envious Exponents的更多相关文章

  1. BAPC2017

    Benelux Algorithm Programming Contest 2017 参考资料: https://blog.csdn.net/sizaif/article/details/798586 ...

  2. 【论文阅读笔记】-针对RSA的短解密指数的密码学分析(Cryptanalysis of Short RSA Secret Exponents)

    目录 1. 介绍 polynomially larger 2. 连分数背景知识 3. 连分数算法 4. 连分数算法在RSA中的应用 5. 例子 6. 对RSA连分数攻击的反制 7. 对于攻击的改进 8 ...

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

    Rastas's has been given a number n. Being weak at mathematics, she has to consider all the numbers f ...

  4. 1002. A+B for Polynomials (25)

    题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...

  5. Python for Infomatics 第12章 网络编程二(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.3 用HTTP协议获取一张 ...

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

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 本书中的许多例子关注的是读取文件 ...

  7. words

    conscious[英][ˈkɒnʃəs][美][ˈkɑnʃəs]consensus[英][kənˈsensəs][美][kənˈsɛnsəs] scious sensuswaterflood; de ...

  8. PAT (Advanced Level) Practise:1002. A+B for Polynomials

    [题目链接] This time, you are supposed to find A+B where A and B are two polynomials. Input Each input f ...

  9. PAT/简单模拟习题集(二)

    B1018. 锤子剪刀布 (20) Discription: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负 ...

随机推荐

  1. Tomcat 7下如何利用 catalina.properties 部署公用类

    Tomcat 有很多配置文件,其中一个是  catalina.properties ,本文介绍catalina.properties 中的设置项. 一.组成   catalina.properties ...

  2. 【51NOD-0】1058 N的阶乘的长度

    [算法]数学 [题解]n!的位数相当于ans=log10(n!)上取整,然后就可以拆出来加了. 可以用log10(i)或log(i)/log(10) 阶乘好像有个斯特林公式…… #include< ...

  3. 【51NOD】1096 距离之和最小

    [算法]数学 [题解] 其实就是求中位数,奇数个点就是最中间的点,偶数个点就是最中间两个点和它们之间的区域皆可(所以偶数不必取到两点正中央,取两点任意一点即可). 我们可以想象现在x轴上有n个点,我们 ...

  4. Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...

  5. Berland National Library

    题目链接:http://codeforces.com/problemset/problem/567/B 题目描述: Berland National Library has recently been ...

  6. 单从软件层面来说,Maya 和 Blender 差别在哪?

    单从软件层面来说,Maya 和 Blender 差别在哪? https://www.zhihu.com/question/21975571

  7. 尽量用const,enum,inline代替define

    在读<Effective C++>之前,我确实不知道const,enum,inline会和define扯上什么关系,看完感觉收获很大,记录之. define: 宏定义. 在编译预处理时,对 ...

  8. HDU-1083

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  9. POJ-2594

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7035   Accepted: 2 ...

  10. Spring Boot with Docker

    翻译自:https://spring.io/guides/gs/spring-boot-docker/ Spring Boot with Docker 这篇教程带你一步步构建一个Docker镜像用来运 ...