传送门:http://codeforces.com/contest/912/problem/B

B. New Year’s Eve

time limit per test1 second

memory limit per test256 megabytes

Problem Description

Since Grisha behaved well last year, at New Year’s Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains n sweet candies from the good ol’ bakery, each labeled from 1 to n corresponding to its tastiness. No two candies have the same tastiness.

The choice of candies has a direct effect on Grisha’s happiness. One can assume that he should take the tastiest ones — but no, the holiday magic turns things upside down. It is the xor-sum of tastinesses that matters, not the ordinary sum!

A xor-sum of a sequence of integers a1, a2, …, am is defined as the bitwise XOR of all its elements: , here denotes the bitwise XOR operation; more about bitwise XOR can be found here.

Ded Moroz warned Grisha he has more houses to visit, so Grisha can take no more than k candies from the bag. Help Grisha determine the largest xor-sum (largest xor-sum means maximum happiness!) he can obtain.

Input

The sole string contains two integers n and k (1 ≤ k ≤ n ≤ 1018).

Output

Output one number — the largest possible xor-sum.

Examples

input

4 3

output

7

input

6 6

output

7

Note

In the first sample case, one optimal answer is 1, 2 and 4, giving the xor-sum of 7.

In the second sample case, one can, for example, take all six candies and obtain the xor-sum of 7.


解题心得:

  1. 题意就是叫你从1到n中去最多选择k个数,使你选择的数异或起来最大。只要知道异或的运算法则这个题是很简单,直接的出n的二进制数位,然后最大的数就是二进制所有的位全是1就是答案了,如果k是1,那么最大的答案就是n。因为很明显的这些数是从1到n,所以直接选择两数异或起来二进制的各个位变为全1就是最大的了。
  2. 而且这个题的数据范围也给了很大的提示啊,n和k最大都要取到1e18,如果考算法O(n)也过不了啊。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans; ll get_w(ll n)
{
ll ans = 0;
while(n)
{
ans++;
n>>=1;
}
return ans;
} int main()
{
ans = 0;
ll k,n;
scanf("%lld%lld",&n,&k);
ll w = get_w(n);//得到n的二进制的位数
if(k >= 2)
ans = pow(2,w)-1;
else
ans = n;
printf("%lld",ans);
return 0;
}

Codeforces Round #456 (Div. 2) B. New Year's Eve的更多相关文章

  1. Codeforces Round #456 (Div. 2) B. New Year's Eve

    B. New Year's Eve time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. Codeforces Round #456 (Div. 2)

    Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...

  3. Codeforces Round #456 (Div. 2) A. Tricky Alchemy

    传送门:http://codeforces.com/contest/912/problem/A A. Tricky Alchemy time limit per test1 second memory ...

  4. Codeforces Round #456 (Div. 2) 912E E. Prime Gift

    题 OvO http://codeforces.com/contest/912/problem/E 解 首先把这个数字拆成个子集,各自生成所有大小1e18及以下的积 对于最坏情况,即如下数据 16 2 ...

  5. Codeforces Round #456 (Div. 2) 912D D. Fishes

    题: OvO http://codeforces.com/contest/912/problem/D 解: 枚举每一条鱼,每放一条鱼,必然放到最优的位置,而最优位置即使钓上的概率最大的位置,即最多的r ...

  6. 【Codeforces Round #456 (Div. 2) A】Tricky Alchemy

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 统计需要的个数. 不够了,就买. [代码] #include <bits/stdc++.h> #define ll lo ...

  7. 【Codeforces Round #456 (Div. 2) B】New Year's Eve

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然10000..取到之后 再取一个01111..就能异或成最大的数字了. [代码] /* 1.Shoud it use long ...

  8. 【Codeforces Round #456 (Div. 2) C】Perun, Ult!

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] set1 < pair < int,int > > set1;记录关键点->某个人怪物永远打不死了,第 ...

  9. Codeforces Round #456 (Div. 2) B题

    B. New Year's Evetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputout ...

随机推荐

  1. Maven的学习资料收集--(八) 构建MyBatis项目

    在这里,写一下,怎么使用Maven构建MyBatis项目. 1. 新建一个Web项目 可以参考前面的博客 2. 修改pom.xml,添加MyBatis依赖 <project xmlns=&quo ...

  2. mysql 链接时报错:1251-Client does not support authentication protocol requested by server

    一 原因是mysql服务器要求的认证插件版本与客户端不一致造成的. 二 由于我是最新的mysql和破解版的navicat,那么就是mysql太高级了. 解决方法有两个,我毫不犹豫的选择mysql降级. ...

  3. Android入门:Activity生命周期

    一.Activity生命周期介绍 我们在学Java Web时会学到Servlet的生命周期,因此对生命周期的概念已经有一定了解,简单地说就是某个事物从出生到死亡的过程. Activity也存在声明周期 ...

  4. SSM整合笔记

    SSM整合笔记 1,创建maven项目 创建maven项目过程省略 ps:如果创建完maven项目之后项目报错,可能是没有配置Tomcat 2,在pom.xml里面导入相应的jar的依赖 <pr ...

  5. webpake-node-sass 报错

    问题描述: npm run dev 就报错,在安装node-sass错误 解决方法 : 找到node_modules下的node-sass文件,进入,如果没有vendor文件夹,就创建一个空文件夹,命 ...

  6. CentOs7 修复 引导启动

    一.修复MBR: MBR(Master Boot Record主引导记录): 硬盘的0柱面.0磁头.1扇区称为主引导扇区.其中446Byte是bootloader,64Byte为Partition t ...

  7. 远程链接mongoDB robomongo

    墙裂推荐一个软件robomongo 下载地址:https://robomongo.org/download 最初不用这个软件的时候需要shell链接mongoDB,折腾了半天结果版本不匹配 用robo ...

  8. python基础一 day16 匿名函数

    def add(x,y): return x+y add = lambda x,y:x+yprint(add(1,2)) dic={'k1':10,'k2':100,'k3':30}def func( ...

  9. Python求列表中某个元素的下标

    一.求列表中某个元素的下标 def findindex(org, x, pos=-1): counts = org.count(x) #先求出org中包含x的个数 if counts == 0: #个 ...

  10. javaweb基础(31)_国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...