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: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负 ...
随机推荐
- AndroidStudio创建jinLibs文件夹
在文件中的buildTypes节点下添加 sourceSets.main { jniLibs.srcDir 'libs' } 如图
- 关于shutdown和close
示例代码: void str_cli(FILE *fp, int sockfd) { pid_t pid; char sendline[MAXLINE], recvline[MAXLINE]; ) { ...
- 命令行创建KVM虚拟机
qemu命令创建虚拟机: qemu-img create -f qcow2 /home/ubuntu.img 20G qemu-system-x86_64 -m 2048 -enable-kvm ...
- Python2.7.3 Tkinter Entry(文本框) 说明
Python学习记录--关于Tkinter Entry(文本框)的选项.方法说明,以及一些示例. 属性(Options) background(bg) borderwidth(bd) cursor ...
- visual studio 个性化设置
尼马visual studio 的注释建设的真垃圾 Ctrl+K+C Ctrl+K+U, 通过工具->选项->环境->键盘->命令包含中搜索“注释选定内容”,分配成 Ctrl+ ...
- C中级 数据序列化简单使用和讨论 (二)
引言 - 一种更好的方式 其实不管什么语言, 开发框架都会遇到序列化问题. 序列化可以理解为A 和 B 交互的一种协议. 很久以前利用 printf 和 scanf 的协议实现过一套序列化问题. C ...
- css设置div等标签背景半透明
三种方式: 1. background-color: transparent; 直接设置背景为透明 2.这种是子元素也会跟着变成半透明 /* 背景半透明,1为不透明 */ opacity: 0.5; ...
- 阿里云Centos等更新源设置成阿里源方法。
https://help.aliyun.com/knowledge_detail/5974184.html ---------------------------------------------- ...
- AtomicReference 和 volatile 的区别
顾名思义,就是不会被打断!!!!!! https://www.cnblogs.com/lpthread/p/3909231.html java.util.concurrent.atomic工具包,支持 ...
- Single Number I&& II——还没看,倒过头来再看
Single Number I Given an array of integers, every element appears twice except for one. Find that si ...