传送门: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. ThinkPHP- 3.1

    基础: 1. 基础概念 LAMP LAMP是基于Linux,Apache,MySQL和PHP的开放资源网络开发平台.这个术语来自欧洲,在那里这些程序常用来作为一种标准开发环境.名字来源于每个程序的第一 ...

  2. 【心得】asp.net 异常:正在中止线程 引发的问题

    asp.net做的一个同步程序,同步的方法是通过JQuery的Ajax调用,同步过程所需要的时间大概有几个小时吧. 当在本机运行的时候,无论是debug模式还是release模式,都能正常运行. 发布 ...

  3. Java中的while循环——通过示例学习Java编程(10)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=20 在上一个教程中,我们讨论了for循环的用法.在本 ...

  4. hibernate课程 初探单表映射2-7 hbm配置文件常用设置

    本节主要简介hbm配置文件以下内容: 1 mapping标签 2 class标签 3 id标签 1 hibbernate-mapping标签 schema 模式名称 catalog 目录名称 defa ...

  5. SVN Working copy '***' locked

    问题描述: 用svn在项目文件夹下commit或者update时会出现错误提示“working copy locked” 解决方法: 1.在项目文件夹下,单击鼠标右键,选择tortoisesvn-&g ...

  6. GitLab关于SSH的使用

    SSH Git是分布式版本控制系统,这意味着您可以在本地工作,但您也可以将更改共享或“推送”到其他服务器.在将更改推送到GitLab服务器之前,您需要一个用于共享信息的安全通信通道. SSH协议提供此 ...

  7. C# 获取当前文件、文件夹的路径及操作环境变量

    一.获取当前文件的路径 1.   System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName      获取模块的完整路径,包 ...

  8. linux 命令——56 ss(转)

    ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...

  9. http请求头和相应头的作用

    请求头(Request Headers) Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate Accept-L ...

  10. 安装git 配置邮箱和用户名

    git 查看用户名和邮箱地址 $ git config user.email $ git config user.name 运行命令来配置你的用户名和邮箱 $ git config --global ...