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: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负 ...
随机推荐
- python实现备份gitlab版本库并更改文件名
脚本的功能是实现备份gitlab版本库,并修改备份后的文件名,成功后发送邮件至相关负责人,脚本如下: #!/usr/bin/env python # -*- coding:utf-8 -*- impo ...
- jsp 内置对象二
1.什么是session ? (1)session 表示客户端与服务器的一次回话. 2)Web中的session指的是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网 ...
- 我要AFO啦好伤感啊
我要AFO啦 虽然一直很垃圾 但是也很开心 接下来我要去学物理啦 原因是今天早上没有吃早餐?! 就这样把~ 白白
- 关于反序列化时抛出java.io.EOFException异常
https://www.cnblogs.com/ouhaitao/p/7683568.html https://blog.csdn.net/mym43210/article/details/40081 ...
- Maven整体认识——详细介绍
前言 本文可以帮助你加深对Maven的整体认识,不是一篇基础文章.如果你现在还没有用 Maven 跑过 HelloWorld,那么本文可能不适合你. 一.Maven简介 Maven 官网:https: ...
- bzoj 1197 DP
我们可以将这个问题转化为在n维空间中一共放m个n维球,求这m个球最多将这个空间分为不同的几个部分. 那么我们设w[i][j]代表i为空间放j个球分为的部分,那么w[i][j]=w[i][j-1]+w[ ...
- bzoj 1079 DP
比较容易看出来是DP,但是如果我们记录每一种颜色还剩多少种的话,消耗的转移的时间复杂度5^15,但是我们考虑到每一种颜色,如果数量相同的话,其实是等效的,所以我们用w[a][b][c][d][e][l ...
- C# 获取一段日期内的工作日
/// <summary> /// 根据指定时间段计算工作日天数 /// </summary> /// <param name="firstDay"& ...
- 八大疯狂的HTML5 Canvas及WebGL动画效果——8 CRAZY ANIMATIONS WITH WEBGL AND HTML5 CANVAS【收藏】
HTML5, WebGL and Javascript have changed the way animation used to be. Past few years, we can only a ...
- <script>标签的属性
1.async 表示立即下载资源,但不妨碍下载其他资源或等待加载其他脚本,脚本相对于页面的其它部分异步执行,只对外部脚本文件有效 2.defer 表示脚本可以延迟到文档完全被解析和显示之后再执行,只对 ...