Envious Exponents
问题 E: Envious Exponents
时间限制: 1 Sec 内存限制: 128 MB
提交: 321 解决: 53
[提交] [状态] [讨论版] [命题人:]
题目描述
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.
输入
输出
样例输入
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的更多相关文章
- BAPC2017
Benelux Algorithm Programming Contest 2017 参考资料: https://blog.csdn.net/sizaif/article/details/798586 ...
- 【论文阅读笔记】-针对RSA的短解密指数的密码学分析(Cryptanalysis of Short RSA Secret Exponents)
目录 1. 介绍 polynomially larger 2. 连分数背景知识 3. 连分数算法 4. 连分数算法在RSA中的应用 5. 例子 6. 对RSA连分数攻击的反制 7. 对于攻击的改进 8 ...
- 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 ...
- 1002. A+B for Polynomials (25)
题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...
- Python for Infomatics 第12章 网络编程二(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.3 用HTTP协议获取一张 ...
- Python for Infomatics 第12章 网络编程一(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 本书中的许多例子关注的是读取文件 ...
- words
conscious[英][ˈkɒnʃəs][美][ˈkɑnʃəs]consensus[英][kənˈsensəs][美][kənˈsɛnsəs] scious sensuswaterflood; de ...
- 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 ...
- PAT/简单模拟习题集(二)
B1018. 锤子剪刀布 (20) Discription: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负 ...
随机推荐
- redis 模糊查找keys
Redis入门教程可参考:超强.超详细Redis数据库入门教程 Redis操作命令可参考:Redis操作命令总结 redis可以通过命令Keys Match来进行键值的模糊匹配,借助StackExch ...
- 【51NOD】斜率最大
[题解]通过画图易得结论:最大斜率一定出现在相邻两点之间. #include<cstdio> #include<algorithm> #include<cstring&g ...
- VSCode Web Developement for Javascript. Must have plugins.
Es6 Javascript front-end web developemnt must have plugins Prettier - Code Formatter Javascript (ES6 ...
- 剖析 golang 的25个关键字
剖析 golang 的25个关键字 基本在所有语言当中,关键字都是不允许用于自定义的,在Golang中有25个关键字,图示如下: 下面我们逐个解析这25个关键字. var && con ...
- 计蒜客 Goldbach Miller_Rabin判别法(大素数判别法)
题目链接:https://nanti.jisuanke.com/t/25985 题目: Description: Goldbach's conjecture is one of the oldest ...
- Winform MD5
1:MD5 http://www.cmd5.com/ 字节数组----字符串 //将字节数组中每个元素按照指定的编码格式解析成字符串//直接将数组ToString()//将字节数组中的每个元素ToSt ...
- spring项目中web-inf下不能引用页面资源
1.spring项目结构 2.spring结构说明 web-inf目录是不对外开放的,外部没办法直接访问到(即通过url访问),只有通过映射来访问,如映射一个action或servlet通过服务器端跳 ...
- deepin安装metasploit
[1]安装metasploit 1.curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/tem ...
- sql server 学习笔记
1. 修改student表中sdept字段改为varchar类型,长度为30,并且不为空 ) not null 2. 删除student表中的address列 alter table student ...
- leetcode 之Remove Duplicates from Sorted List(17)
很简单的一题,需要注意的是如果某结点重复了记得将其删除. ListNode *deleteDuplicates(ListNode *head) { if (head == nullptr) retur ...